📄 basics.html
字号:
<html><body><h1 align='right'><a name='BASICS'><img src="2.gif" align="right"hspace="10" width="100" height="100" alt="2"></a>Getting Startedwith Mini-XML</h1><p>This chapter describes how to write programs that use Mini-XML toaccess data in an XML file. Mini-XML provides the followingfunctionality:</p><ul> <li>Functions for creating and managing XML documents in memory.</li> <li>Reading of UTF-8 and UTF-16 encoded XML files and strings.</li> <li>Writing of UTF-8 encoded XML files and strings.</li> <li>Support for arbitrary element names, attributes, and attribute values with no preset limits, just available memory.</li> <li>Support for integer, real, opaque ("cdata"), and text data types in "leaf" nodes.</li> <li>"Find", "index", and "walk" functions for easily accessing data in an XML document.</li></ul><p>Mini-XML doesn't do validation or other types of processingon the data based upon schema files or other sources ofdefinition information, nor does it support character entitiesother than those required by the XML specification.</p><h2>The Basics</h2><p>Mini-XML provides a single header file which you include:</p><pre> #include <mxml.h></pre><p>The Mini-XML library is included with your program using the<kbd>-lmxml</kbd> option:</p><pre> <kbd>gcc -o myprogram myprogram.c -lmxml ENTER</kbd></pre><p>If you have the <tt>pkg-config(1)</tt> software installed,you can use it to determine the proper compiler and linker optionsfor your installation:</p><pre> <kbd>pkg-config --cflags mxml ENTER</kbd> <kbd>pkg-config --libs mxml ENTER</kbd></pre><h2>Nodes</h2><p>Every piece of information in an XML file (elements, text,numbers) is stored in memory in "nodes". Nodes are defined bythe <ahref='#mxml_node_t'><tt>mxml_node_t</tt></a>structure. The <ahref='#mxml_type_t'><tt>type</tt></a> memberdefines the node type (element, integer, opaque, real, or text)which determines which value you want to look at in the <ahref='#mxml_value_t'><tt>value</tt></a> union.</p><!-- NEED 10 --><center><table width="80%" border="1" cellpadding="5" cellspacing="0" summary="Mini-XML Node Value Members"><caption align="bottom"><i>Table 2-1: Mini-XML Node Value Members</i></caption><tr bgcolor="#cccccc"> <th>Value</th> <th>Type</th> <th>Node member</th></tr><tr> <td>Custom</td> <td><tt>void *</tt></td> <td><tt>node->value.custom.data</tt></td></tr><tr> <td>Element</td> <td><tt>char *</tt></td> <td><tt>node->value.element.name</tt></td></tr><tr> <td>Integer</td> <td><tt>int</tt></td> <td><tt>node->value.integer</tt></td></tr><tr> <td>Opaque (string)</td> <td><tt>char *</tt></td> <td><tt>node->value.opaque</tt></td></tr><tr> <td>Real</td> <td><tt>double</tt></td> <td><tt>node->value.real</tt></td></tr><tr> <td>Text</td> <td><tt>char *</tt></td> <td><tt>node->value.text.string</tt></td></tr></table></center><p>Each node also has a <tt>user_data</tt> member which allows youto associate application-specific data with each node as needed.</p><p>New nodes are created using the <ahref='#mxmlNewElement'><tt>mxmlNewElement</tt></a>, <ahref='#mxmlNewInteger'><tt>mxmlNewInteger</tt></a>, <ahref='#mxmlNewOpaque'><tt>mxmlNewOpaque</tt></a>, <ahref='#mxmlNewReal'><tt>mxmlNewReal</tt></a>, <ahref='#mxmlNewText'><tt>mxmlNewText</tt></a> <ahref='#mxmlNewTextf'><tt>mxmlNewTextf</tt></a> <ahref='#mxmlNewXML'><tt>mxmlNewXML</tt></a> functions. Onlyelements can have child nodes, and the top node must be an element,usually the <tt><?xml version="1.0"?></tt> node created by<tt>mxmlNewXML()</tt>.</p><p>Nodes have pointers to the node above (<tt>parent</tt>), below(<tt>child</tt>), left (<tt>prev</tt>), and right (<tt>next</tt>)of the current node. If you have an XML file like the following:</p><pre> <?xml version="1.0"?> <data> <node>val1</node> <node>val2</node> <node>val3</node> <group> <node>val4</node> <node>val5</node> <node>val6</node> </group> <node>val7</node> <node>val8</node> </data></pre><p>the node tree for the file would look like the following inmemory:</p><pre> ?xml | data | node - node - node - group - node - node | | | | | | val1 val2 val3 | val7 val8 | node - node - node | | | val4 val5 val6</pre><p>where "-" is a pointer to the next node and "|" is a pointerto the first child node.</p><p>Once you are done with the XML data, use the <ahref='#mxmlDelete'><tt>mxmlDelete</tt></a> function to recursivelyfree the memory that is used for a particular node or the entiretree:</p><pre> mxmlDelete(tree);</pre><!-- NEW PAGE --><h2>Creating XML Documents</h2><p>You can create and update XML documents in memory using thevarious <tt>mxmlNew</tt> functions. The following code willcreate the XML document described in the previous section:</p><pre> mxml_node_t *xml; /* <?xml ... ?> */ mxml_node_t *data; /* <data> */ mxml_node_t *node; /* <node> */ mxml_node_t *group; /* <group> */ xml = mxmlNewXML("1.0"); data = mxmlNewElement(xml, "data"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val1"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val2"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val3"); group = mxmlNewElement(data, "group"); node = mxmlNewElement(group, "node"); mxmlNewText(node, 0, "val4"); node = mxmlNewElement(group, "node"); mxmlNewText(node, 0, "val5"); node = mxmlNewElement(group, "node"); mxmlNewText(node, 0, "val6"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val7"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val8");</pre><p>We start by creating the <tt><?xml version="1.0"?></tt>node common to all XML files using the <ahref="#mxmlNewXML"><tt>mxmlNewXML</tt></a> function:</p><pre> xml = mxmlNewXML("1.0");</pre><p>We then create the <tt><data></tt> node used for thisdocument using the <ahref="#mxmlNewElement"><tt>mxmlNewElement</tt></a> function. Thefirst argument specifies the parent node (<tt>xml</tt>) while thesecond specifies the element name (<tt>data</tt>):</p><pre> data = mxmlNewElement(xml, "data");</pre><p>Each <tt><node>...</node></tt> in the file iscreated using the <tt>mxmlNewElement</tt> and <ahref="#mxmlNewText"><tt>mxmlNewText</tt></a> functions. The firstargument of <tt>mxmlNewText</tt> specifies the parent node(<tt>node</tt>). The second argument specifies whether whitespaceappears before the text - 0 or false in this case. The lastargument specifies the actual text to add:</p><pre> node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val1");</pre><p>The resulting in-memory XML document can then be saved orprocessed just like one loaded from disk or a string.</p><!-- NEW PAGE --><h2>Loading XML</h2><p>You load an XML file using the <ahref='#mxmlLoadFile'><tt>mxmlLoadFile</tt></a>function:</p><pre> FILE *fp; mxml_node_t *tree; fp = fopen("filename.xml", "r");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -