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/LoadingandSavingXMLFiles.html

90 lines
3.7 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.0</TITLE>
<META NAME="author" CONTENT="Michael Sweet">
<META NAME="copyright" CONTENT="Copyright 2003-2004">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-iso-8859-1">
<LINK REL="Start" HREF="index.html">
<LINK REL="Contents" HREF="toc.html">
<LINK REL="Prev" HREF="Nodes.html">
<LINK REL="Next" HREF="FindingandIteratingNodes.html">
<STYLE TYPE="text/css"><!--
BODY { font-family: 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 }
--></STYLE>
</HEAD>
<BODY>
<A HREF="toc.html">Contents</A>
<A HREF="Nodes.html">Previous</A>
<A HREF="FindingandIteratingNodes.html">Next</A>
<HR NOSHADE>
<H2><A NAME="3_3">Loading and Saving XML Files</A></H2>
<P>You load an XML file using the <A href="mxmlLoadFile.html#mxmlLoadFile">
<TT>mxmlLoadFile()</TT></A> function:</P>
<PRE>
FILE *fp;
<A href="mxmlnodet.html#mxml_node_t">mxml_node_t</A> *tree;
fp = fopen(&quot;filename.xml&quot;, &quot;r&quot;);
tree = <A href="mxmlLoadFile.html#mxmlLoadFile">mxmlLoadFile</A>(NULL, fp, MXML_NO_CALLBACK);
fclose(fp);
</PRE>
<P>The third argument specifies a callback function which returns the
value type of the immediate children for a new element node: <TT>
MXML_INTEGER</TT>, <TT>MXML_OPAQUE</TT>, <TT>MXML_REAL</TT>, or <TT>
MXML_TEXT</TT>. This 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. The default value type is MXML_TEXT if no callback is used.</P>
<P>Similarly, you save an XML file using the <A href="mxmlSaveFile.html#mxmlSaveFile">
<TT>mxmlSaveFile()</TT></A> function:</P>
<PRE>
FILE *fp;
<A href="mxmlnodet.html#mxml_node_t">mxml_node_t</A> *tree;
fp = fopen(&quot;filename.xml&quot;, &quot;w&quot;);
<A href="mxmlSaveFile.html#mxmlSaveFile">mxmlSaveFile</A>(tree, fp, MXML_NO_CALLBACK);
fclose(fp);
</PRE>
<P>Callback functions for saving are used to optionally insert
whitespace before and after elements in the node tree. Your 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 <A href="mxmlLoadString.html#mxmlLoadString"><TT>mxmlLoadString()</TT>
</A>, <A href="mxmlSaveAllocString.html#mxmlSaveAllocString"><TT>
mxmlSaveAllocString()</TT></A>, and <A href="mxmlSaveString.html#mxmlSaveString">
<TT>mxmlSaveString()</TT></A> functions load XML node trees from and
save XML node trees to strings:</P>
<PRE>
char buffer[8192];
char *ptr;
<A href="mxmlnodet.html#mxml_node_t">mxml_node_t</A> *tree;
...
tree = <A href="mxmlLoadString.html#mxmlLoadString">mxmlLoadString</A>(NULL, buffer, MXML_NO_CALLBACK);
...
<A href="mxmlSaveString.html#mxmlSaveString">mxmlSaveString</A>(tree, buffer, sizeof(buffer), MXML_NO_CALLBACK);
...
ptr = <A href="mxmlSaveAllocString.html#mxmlSaveAllocString">mxmlSaveAllocString</A>(tree, MXML_NO_CALLBACK);
</PRE>
<HR NOSHADE>
<A HREF="toc.html">Contents</A>
<A HREF="Nodes.html">Previous</A>
<A HREF="FindingandIteratingNodes.html">Next</A>
</BODY>
</HTML>