You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
mxml/www/docfiles/CreatingXMLDocuments.html

98 lines
3.8 KiB

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<TITLE>Mini-XML Programmers Manual, Version 2.3</TITLE>
<META NAME="author" CONTENT="Michael R. Sweet">
<META NAME="copyright" CONTENT="Copyright 2003-2007">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-iso-8859-1">
<LINK REL="Start" HREF="index.html">
<LINK REL="Contents" HREF="index.html">
<LINK REL="Prev" HREF="Nodes.html">
<LINK REL="Next" HREF="LoadingXML.html">
<STYLE TYPE="text/css"><!--
BODY { font-family: sans-serif }
H1 { font-family: sans-serif }
H2 { font-family: sans-serif }
H3 { font-family: sans-serif }
H4 { font-family: sans-serif }
H5 { font-family: sans-serif }
H6 { font-family: sans-serif }
SUB { font-size: smaller }
SUP { font-size: smaller }
PRE { font-family: monospace }
A { text-decoration: none }
--></STYLE>
</HEAD>
<BODY>
<A HREF="index.html">Contents</A>
<A HREF="Nodes.html">Previous</A>
<A HREF="LoadingXML.html">Next</A>
<HR NOSHADE>
<H2><A NAME="3_3">Creating XML Documents</A></H2>
<P>You can create and update XML documents in memory using the various <TT>
mxmlNew</TT> functions. The following code will create the XML document
described in the previous section:</P>
<PRE>
mxml_node_t *xml; /* &lt;?xml ... ?&gt; */
mxml_node_t *data; /* &lt;data&gt; */
mxml_node_t *node; /* &lt;node&gt; */
mxml_node_t *group; /* &lt;group&gt; */
xml = mxmlNewXML(&quot;1.0&quot;);
data = mxmlNewElement(xml, &quot;data&quot;);
node = mxmlNewElement(data, &quot;node&quot;);
mxmlNewText(node, 0, &quot;val1&quot;);
node = mxmlNewElement(data, &quot;node&quot;);
mxmlNewText(node, 0, &quot;val2&quot;);
node = mxmlNewElement(data, &quot;node&quot;);
mxmlNewText(node, 0, &quot;val3&quot;);
group = mxmlNewElement(data, &quot;group&quot;);
node = mxmlNewElement(group, &quot;node&quot;);
mxmlNewText(node, 0, &quot;val4&quot;);
node = mxmlNewElement(group, &quot;node&quot;);
mxmlNewText(node, 0, &quot;val5&quot;);
node = mxmlNewElement(group, &quot;node&quot;);
mxmlNewText(node, 0, &quot;val6&quot;);
node = mxmlNewElement(data, &quot;node&quot;);
mxmlNewText(node, 0, &quot;val7&quot;);
node = mxmlNewElement(data, &quot;node&quot;);
mxmlNewText(node, 0, &quot;val8&quot;);
</PRE>
<P>We start by creating the <TT>&lt;?xml version=&quot;1.0&quot;?&gt;</TT> node common
to all XML files using the <A href="MiniXML23mxmlNewXML.html#mxmlNewXML">
<TT>mxmlNewXML</TT></A> function:</P>
<PRE>
xml = mxmlNewXML(&quot;1.0&quot;);
</PRE>
<P>We then create the <TT>&lt;data&gt;</TT> node used for this document using
the <A href="mxmlNewElement.html#mxmlNewElement"><TT>mxmlNewElement</TT>
</A> function. The first argument specifies the parent node (<TT>xml</TT>
) while the second specifies the element name (<TT>data</TT>):</P>
<PRE>
data = mxmlNewElement(xml, &quot;data&quot;);
</PRE>
<P>Each <TT>&lt;node&gt;...&lt;/node&gt;</TT> in the file is created using the <TT>
mxmlNewElement</TT> and <A href="mxmlNewText.html#mxmlNewText"><TT>
mxmlNewText</TT></A> functions. The first argument of <TT>mxmlNewText</TT>
specifies the parent node (<TT>node</TT>). The second argument
specifies whether whitespace appears before the text - 0 or false in
this case. The last argument specifies the actual text to add:</P>
<PRE>
node = mxmlNewElement(data, &quot;node&quot;);
mxmlNewText(node, 0, &quot;val1&quot;);
</PRE>
<P>The resulting in-memory XML document can then be saved or processed
just like one loaded from disk or a string.</P>
<!-- NEW PAGE -->
<HR NOSHADE>
<A HREF="index.html">Contents</A>
<A HREF="Nodes.html">Previous</A>
<A HREF="LoadingXML.html">Next</A>
</BODY>
</HTML>