mxml/www/docfiles/SavingXML.html
2004-06-21 11:00:28 +00:00

75 lines
3.0 KiB
HTML

<!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="LoadingXML.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="LoadingXML.html">Previous</A>
<A HREF="FindingandIteratingNodes.html">Next</A>
<HR NOSHADE>
<H2><A NAME="3_4">Saving XML</A></H2>
<P>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>The first argument is the XML node tree to save. It should normally
be a pointer to the top-level <TT>?xml</TT> node in your XML document.</P>
<P>The second argument is the stdio file to write to, as opened by <TT>
fopen()</TT> or <TT>popen()</TT>. You can also use <TT>stdout</TT> if
you are implementing an XML filter program.</P>
<P>The third argument is the whitespace callback to use when saving the
file. Whitespace callbacks are covered in detail in <A href="SAVE_CALLBACKS">
Chapter 3</A>. The example code above uses the <TT>MXML_NO_CALLBACK</TT>
constant to specify that no special whitespace handling is required.</P>
<P>The <A href="mxmlSaveAllocString.html#mxmlSaveAllocString"><TT>
mxmlSaveAllocString()</TT></A>, and <A href="mxmlSaveString.html#mxmlSaveString">
<TT>mxmlSaveString()</TT></A> functions 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;
...
<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>
<P>The first and last arguments are the same as used for <TT>
mxmlSaveFile()</TT>. The <TT>mxmlSaveString()</TT> function takes
pointer and size arguments for saving the XML document to a fixed-size
buffer, while <TT>mxmlSaveAllocString()</TT> returns a string buffer
that was allocated using <TT>malloc()</TT>.</P>
<HR NOSHADE>
<A HREF="toc.html">Contents</A>
<A HREF="LoadingXML.html">Previous</A>
<A HREF="FindingandIteratingNodes.html">Next</A>
</BODY>
</HTML>