?? basics.html
字號:
tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK); fclose(fp);</pre><p>The first argument specifies an existing XML parent node, ifany. Normally you will pass <tt>NULL</tt> for this argumentunless you are combining multiple XML sources. The XML file mustcontain a complete XML document including the <tt>?xml</tt>element if the parent node is <tt>NULL</tt>.</p><p>The second argument specifies the stdio file to read from, asopened by <tt>fopen()</tt> or <tt>popen()</tt>. You can also use<tt>stdin</tt> if you are implementing an XML filterprogram.</p><p>The third argument specifies a callback function which returnsthe value type of the immediate children for a new element node:<tt>MXML_CUSTOM</tt>, <tt>MXML_IGNORE</tt>,<tt>MXML_INTEGER</tt>, <tt>MXML_OPAQUE</tt>, <tt>MXML_REAL</tt>,or <tt>MXML_TEXT</tt>. Load callbacks are described in detail in<a href='#LOAD_CALLBACKS'>Chapter 3</a>. The example code usesthe <tt>MXML_TEXT_CALLBACK</tt> constant which specifies that alldata nodes in the document contain whitespace-separated textvalues. Other standard callbacks include<tt>MXML_IGNORE_CALLBACK</tt>, <tt>MXML_INTEGER_CALLBACK</tt>,<tt>MXML_OPAQUE_CALLBACK</tt>, and <tt>MXML_REAL_CALLBACK</tt>.</p><p>The <a href='#mxmlLoadString'><tt>mxmlLoadString</tt></a>function loads XML node trees from a string:</p><!-- NEED 10 --><pre> char buffer[8192]; mxml_node_t *tree; ... tree = mxmlLoadString(NULL, buffer, MXML_TEXT_CALLBACK);</pre><p>The first and third arguments are the same as used for<tt>mxmlLoadFile()</tt>. The second argument specifies thestring or character buffer to load and must be a complete XMLdocument including the <tt>?xml</tt> element if the parent nodeis <tt>NULL</tt>.</p><!-- NEW PAGE --><h2>Saving XML</h2><p>You save an XML file using the <ahref='#mxmlSaveFile'><tt>mxmlSaveFile</tt></a> function:</p><pre> FILE *fp; mxml_node_t *tree; fp = fopen("filename.xml", "w"); mxmlSaveFile(tree, fp, MXML_NO_CALLBACK); fclose(fp);</pre><p>The first argument is the XML node tree to save. It shouldnormally be a pointer to the top-level <tt>?xml</tt> node inyour XML document.</p><p>The second argument is the stdio file to write to, as openedby <tt>fopen()</tt> or <tt>popen()</tt>. You can also use<tt>stdout</tt> if you are implementing an XML filterprogram.</p><p>The third argument is the whitespace callback to use whensaving the file. Whitespace callbacks are covered in detail in <ahref='SAVE_CALLBACKS'>Chapter 3</a>. The previous example codeuses the <tt>MXML_NO_CALLBACK</tt> constant to specify that nospecial whitespace handling is required.</p><p>The <ahref='#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> functiontakes pointer and size arguments for saving the XML document toa fixed-size buffer, while <tt>mxmlSaveAllocString()</tt>returns a string buffer that was allocated using<tt>malloc()</tt>.</p><h3>Controlling Line Wrapping</h3><p>When saving XML documents, Mini-XML normally wraps outputlines at column 75 so that the text is readable in terminalwindows. The <ahref='#mxmlSetWrapMargin'><tt>mxmlSetWrapMargin</tt></a> functionoverrides the default wrap margin:</p><pre> /* Set the margin to 132 columns */ mxmlSetWrapMargin(132); /* Disable wrapping */ mxmlSetWrapMargin(0);</pre><!-- NEW PAGE--><h2>Finding and Iterating Nodes</h2><p>The <ahref='#mxmlWalkPrev'><tt>mxmlWalkPrev</tt></a>and <ahref='#mxmlWalkNext'><tt>mxmlWalkNext</tt></a>functionscan 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 <ahref='#mxmlFindElement'><tt>mxmlFindElement</tt></a>function:</p><pre> mxml_node_t *node; node = mxmlFindElement(tree, tree, "name", "attr", "value", 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 "a" element */ node = mxmlFindElement(tree, tree, "a", NULL, NULL, MXML_DESCEND);</pre><!-- NEED 5 --><pre> /* Find the first "a" element with "href" attribute */ node = mxmlFindElement(tree, tree, "a", "href", NULL, MXML_DESCEND);</pre><!-- NEED 6 --><pre> /* Find the first "a" element with "href" to a URL */ node = mxmlFindElement(tree, tree, "a", "href", "http://www.easysw.com/", MXML_DESCEND);</pre><!-- NEED 5 --><pre> /* Find the first element with a "src" attribute */ node = mxmlFindElement(tree, tree, NULL, "src", NULL, MXML_DESCEND);</pre><!-- NEED 5 --><pre> /* Find the first element with a "src" = "foo.jpg" */ node = mxmlFindElement(tree, tree, NULL, "src", "foo.jpg", MXML_DESCEND);</pre><p>You can also iterate with the same function:</p><pre> mxml_node_t *node; for (node = mxmlFindElement(tree, tree, "name", NULL, NULL, MXML_DESCEND); node != NULL; node = mxmlFindElement(node, tree, "name", NULL, NULL, MXML_DESCEND)) { ... do something ... }</pre><!-- NEED 10 --><p>The <tt>MXML_DESCEND</tt> argument can actually be one ofthree 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 "group" would be the "node" element to the left, while the next node from "group" would be the "node" 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 "node" and "group" elements under the "?xml" 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 "group" would be the "val3" node and the next node would be the first node element under "group". <p>If you were to walk from the root node "?xml" 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 "val8" and walked using <tt>mxmlWalkPrev()</tt>, the order would be reversed, ending at "?xml".</p></li></ul></body></html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -