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.
98 lines
3.8 KiB
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; /* <?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 <A href="MiniXML23mxmlNewXML.html#mxmlNewXML">
|
|
<TT>mxmlNewXML</TT></A> function:</P>
|
|
<PRE>
|
|
xml = mxmlNewXML("1.0");
|
|
</PRE>
|
|
<P>We then create the <TT><data></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, "data");
|
|
</PRE>
|
|
<P>Each <TT><node>...</node></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, "node");
|
|
mxmlNewText(node, 0, "val1");
|
|
</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>
|
|
|