⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mxml.html

📁 minixml2.5最新的版本。 嵌入式xml 解析、查找、生成、遍历 功能,全部实现是标准c,移植很容易。 最新的2.5
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<P>The second argument is the stdio file to write to, as opened by <TT>fopen()</TT> or <TT>popen()</TT>. You can also use <TT>stdout</TT> if you are implementing an XML filter program.</P><P>The third argument is the whitespace callback to use when saving the file. Whitespace callbacks are covered in detail in <A href="SAVE_CALLBACKS">Chapter 3</A>. The previous example code uses the <TT>MXML_NO_CALLBACK</TT> constant to specify that no special whitespace handling is required.</P><P>The <A href="#mxmlSaveAllocString"><TT>mxmlSaveAllocString</TT></A>, and <A href="#mxmlSaveString"><TT>mxmlSaveString</TT></A> functions save XML node trees to strings:</P><PRE>    char buffer[8192];    char *ptr;    mxml_node_t *tree;    ...    mxmlSaveString(tree, buffer, sizeof(buffer),                   MXML_NO_CALLBACK);    ...    ptr = mxmlSaveAllocString(tree, MXML_NO_CALLBACK);</PRE><P>The first and last arguments are the same as used for <TT>mxmlSaveFile()</TT>. The <TT>mxmlSaveString</TT> function takes pointer and size arguments for saving the XML document to a fixed-size buffer, while <TT>mxmlSaveAllocString()</TT> returns a string buffer that was allocated using <TT>malloc()</TT>.</P><H3><A NAME="3_5_1">Controlling Line Wrapping</A></H3><P>When saving XML documents, Mini-XML normally wraps output lines at column 75 so that the text is readable in terminal windows. The <A href="#mxmlSetWrapMargin"><TT>mxmlSetWrapMargin</TT></A> function overrides the default wrap margin:</P><PRE>    /* Set the margin to 132 columns */    mxmlSetWrapMargin(132);    /* Disable wrapping */    mxmlSetWrapMargin(0);</PRE><!-- NEW PAGE--><H2><A NAME="3_6">Finding and Iterating Nodes</A></H2><P>The <A href="#mxmlWalkPrev"><TT>mxmlWalkPrev</TT></A> and <A href="#mxmlWalkNext"><TT>mxmlWalkNext</TT></A>functions can be used to iterate through the XML node tree:</P><PRE>    mxml_node_t *node;        node = mxmlWalkPrev(current, tree,                        MXML_DESCEND);    node = mxmlWalkNext(current, tree,                        MXML_DESCEND);</PRE><P>In addition, you can find a named element/node using the <A href="#mxmlFindElement"><TT>mxmlFindElement</TT></A> function:</P><PRE>    mxml_node_t *node;        node = mxmlFindElement(tree, tree, &quot;name&quot;,                           &quot;attr&quot;, &quot;value&quot;,                           MXML_DESCEND);</PRE><P>The <TT>name</TT>, <TT>attr</TT>, and <TT>value</TT> arguments can be passed as <TT>NULL</TT> to act as wildcards, e.g.:</P><!-- NEED 4 --><PRE>    /* Find the first &quot;a&quot; element */    node = mxmlFindElement(tree, tree, &quot;a&quot;,                           NULL, NULL,                           MXML_DESCEND);</PRE><!-- NEED 5 --><PRE>    /* Find the first &quot;a&quot; element with &quot;href&quot;       attribute */    node = mxmlFindElement(tree, tree, &quot;a&quot;,                           &quot;href&quot;, NULL,                           MXML_DESCEND);</PRE><!-- NEED 6 --><PRE>    /* Find the first &quot;a&quot; element with &quot;href&quot;       to a URL */    node = mxmlFindElement(tree, tree, &quot;a&quot;,                           &quot;href&quot;,                           &quot;http://www.easysw.com/&quot;,                           MXML_DESCEND);</PRE><!-- NEED 5 --><PRE>    /* Find the first element with a &quot;src&quot;       attribute */    node = mxmlFindElement(tree, tree, NULL,                           &quot;src&quot;, NULL,                           MXML_DESCEND);</PRE><!-- NEED 5 --><PRE>    /* Find the first element with a &quot;src&quot;       = &quot;foo.jpg&quot; */    node = mxmlFindElement(tree, tree, NULL,                           &quot;src&quot;, &quot;foo.jpg&quot;,                           MXML_DESCEND);</PRE><P>You can also iterate with the same function:</P><PRE>    mxml_node_t *node;    for (node = mxmlFindElement(tree, tree,                                &quot;name&quot;,                                NULL, NULL,                                MXML_DESCEND);         node != NULL;         node = mxmlFindElement(node, tree,                                &quot;name&quot;,                                NULL, NULL,                                MXML_DESCEND))    {      ... do something ...    }</PRE><!-- NEED 10 --><P>The <TT>MXML_DESCEND</TT> argument can actually be one of three constants:</P><UL><LI><TT>MXML_NO_DESCEND</TT> means to not to look at any child nodes in the element hierarchy, just look at siblings at the same level or parent nodes until the top node or top-of-tree is reached.<P>The previous node from &quot;group&quot; would be the &quot;node&quot; element to the left, while the next node from &quot;group&quot; would be the &quot;node&quot; element to the right.<BR><BR></P></LI><LI><TT>MXML_DESCEND_FIRST</TT> means that it is OK to descend to the first child of a node, but not to descend further when searching. You'll normally use this when iterating through direct children of a parent node, e.g. all of the &quot;node&quot; and &quot;group&quot; elements under the &quot;?xml&quot; parent node in the example above.<P>This mode is only applicable to the search function; the walk functions treat this as <TT>MXML_DESCEND</TT> since every call is a first time.<BR><BR></P></LI><LI><TT>MXML_DESCEND</TT> means to keep descending until you hit the bottom of the tree. The previous node from &quot;group&quot; would be the &quot;val3&quot; node and the next node would be the first node element under &quot;group&quot;.<P>If you were to walk from the root node &quot;?xml&quot; to the end of the tree with <TT>mxmlWalkNext()</TT>, the order would be:</P><P><TT>?xml data node val1 node val2 node val3 group node val4 node val5 node val6 node val7 node val8</TT></P><P>If you started at &quot;val8&quot; and walked using <TT>mxmlWalkPrev()</TT>, the order would be reversed, ending at &quot;?xml&quot;.</P></LI></UL><HR NOSHADE><H1 align="right"><A name="ADVANCED"><IMG align="right" alt="3" height="100"hspace="10" src="3.gif" width="100"></A>More Mini-XML Programming Techniques</H1><P>This chapter shows additional ways to use the Mini-XML library in your programs.</P><H2><A name="LOAD_CALLBACKS">Load Callbacks</A></H2><P><A href="#LOAD_XML">Chapter 2</A> introduced the <A href="#mxmlLoadFile"><TT>mxmlLoadFile()</TT></A> and <A href="#mxmlLoadString"><TT>mxmlLoadString()</TT></A> functions. The last argument to these functions is a callback function which is used to determine the value type of each data node in an XML document.</P><P>Mini-XML defines several standard callbacks for simple XML data files:</P><UL><LI><TT>MXML_INTEGER_CALLBACK</TT> - All data nodes contain whitespace-separated integers.</LI><LI><TT>MXML_OPAQUE_CALLBACK</TT> - All data nodes contain opaque strings (&quot;CDATA&quot;).</LI><LI><TT>MXML_REAL_CALLBACK</TT> - All data nodes contain whitespace-separated floating-point numbers.</LI><LI><TT>MXML_TEXT_CALLBACK</TT> - All data nodes contain whitespace-separated strings.</LI></UL><P>You can provide your own callback functions for more complex XML documents. Your callback function will receive a pointer to the current element node and must return the value type of the immediate children for that element node: <TT>MXML_INTEGER</TT>, <TT>MXML_OPAQUE</TT>, <TT>MXML_REAL</TT>, or <TT>MXML_TEXT</TT>. The function is called<I> after</I> the element and its attributes have been read, so you can look at the element name, attributes, and attribute values to determine the proper value type to return.</P><!-- NEED 2in --><P>The following callback function looks for an attribute named &quot;type&quot; or the element name to determine the value type for its child nodes:</P><PRE>    mxml_type_t    type_cb(mxml_node_t *node)    {      const char *type;     /*      * You can lookup attributes and/or use the      * element name, hierarchy, etc...      */      type = mxmlElementGetAttr(node, &quot;type&quot;);      if (type == NULL)	type = node-&gt;value.element.name;      if (!strcmp(type, &quot;integer&quot;))	return (MXML_INTEGER);      else if (!strcmp(type, &quot;opaque&quot;))	return (MXML_OPAQUE);      else if (!strcmp(type, &quot;real&quot;))	return (MXML_REAL);      else	return (MXML_TEXT);    }</PRE><P>To use this callback function, simply use the name when you call any of the load functions:</P><PRE>    FILE *fp;    mxml_node_t *tree;    fp = fopen(&quot;filename.xml&quot;, &quot;r&quot;);    tree = mxmlLoadFile(NULL, fp, <B>type_cb</B>);    fclose(fp);</PRE><H2><A name="SAVE_CALLBACKS">Save Callbacks</A></H2><P><A href="#LOAD_XML">Chapter 2</A> also introduced the <A href="#mxmlSaveFile"><TT>mxmlSaveFile()</TT></A>, <A href="#mxmlSaveString"><TT>mxmlSaveString()</TT></A>, and <A href="#mxmlSaveAllocString"><TT>mxmlSaveAllocString()</TT></A> functions. The last argument to these functions is a callback function which is used to automatically insert whitespace in an XML document.</P><P>Your callback function will be called up to four times for each element node with a pointer to the node and a &quot;where&quot; value of <TT>MXML_WS_BEFORE_OPEN</TT>, <TT>MXML_WS_AFTER_OPEN</TT>, <TT>MXML_WS_BEFORE_CLOSE</TT>, or <TT>MXML_WS_AFTER_CLOSE</TT>. The callback function should return <TT>NULL</TT> if no whitespace should be added and the string to insert (spaces, tabs, carriage returns, and newlines) otherwise.</P><P>The following whitespace callback can be used to add whitespace to XHTML output to make it more readable in a standard text editor:</P><PRE>    const char *    whitespace_cb(mxml_node_t *node,                  int where)    {      const char *name;     /*      * We can conditionally break to a new line      * before or after any element. These are      * just common HTML elements...      */      name = node-&gt;value.element.name;      if (!strcmp(name, &quot;html&quot;) ||          !strcmp(name, &quot;head&quot;) ||          !strcmp(name, &quot;body&quot;) ||	  !strcmp(name, &quot;pre&quot;) ||          !strcmp(name, &quot;p&quot;) ||	  !strcmp(name, &quot;h1&quot;) ||          !strcmp(name, &quot;h2&quot;) ||          !strcmp(name, &quot;h3&quot;) ||	  !strcmp(name, &quot;h4&quot;) ||          !strcmp(name, &quot;h5&quot;) ||          !strcmp(name, &quot;h6&quot;))      {       /*	* Newlines before open and after        * close...	*/	if (where == MXML_WS_BEFORE_OPEN ||            where == MXML_WS_AFTER_CLOSE)	  return (&quot;\n&quot;);      }      else if (!strcmp(name, &quot;dl&quot;) ||               !strcmp(name, &quot;ol&quot;) ||               !strcmp(name, &quot;ul&quot;))      {       /*	* Put a newline before and after list        * elements...	*/	return (&quot;\n&quot;);      }      else if (!strcmp(name, &quot;dd&quot;) ||               !strcmp(name, &quot;dt&quot;) ||               !strcmp(name, &quot;li&quot;))      {       /*	* Put a tab before &lt;li&gt;'s, * &lt;dd&gt;'s,        * and &lt;dt&gt;'s, and a newline after them...	*/	if (where == MXML_WS_BEFORE_OPEN)	  return (&quot;\t&quot;);	else if (where == MXML_WS_AFTER_CLOSE)	  return (&quot;\n&quot;);      }     /*      * Return NULL for no added whitespace...      */      return (NULL);    }</PRE><P>To use this callback function, simply use the name when you call any of the save functions:</P><PRE>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -