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

123 lines
4.4 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.2.1</TITLE>
<META NAME="author" CONTENT="Michael Sweet">
<META NAME="copyright" CONTENT="Copyright 2003-2005">
<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="LoadCallbacks.html">
<LINK REL="Next" HREF="CustomDataTypes.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="LoadCallbacks.html">Previous</A>
<A HREF="CustomDataTypes.html">Next</A>
<HR NOSHADE>
<H2><A name="SAVE_CALLBACKS">Save Callbacks</A></H2>
<P><A href="#LOAD_XML">Chapter 2</A> also introduced the <A href="mxmlSaveFile.html#mxmlSaveFile">
<TT>mxmlSaveFile()</TT></A>, <A href="mxmlSaveString.html#mxmlSaveString">
<TT>mxmlSaveString()</TT></A>, and <A href="mxmlSaveAllocString.html#mxmlSaveAllocString">
<TT>mxmlSaveAllocString()</TT></A> functions. The last argument to these
functions is a callback function which is used to automatically insert
whitespace in an XML document.</P>
<P>Your callback 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. The following whitespace callback can be used to add
whitespace to XHTML output to make it more readable in a standard text
editor:</P>
<!-- NEW PAGE -->
<PRE>
/*
* 'whitespace_cb()' - Let the mxmlSaveFile() function know when to insert
* newlines and tabs...
*/
const char * /* O - Whitespace string or NULL */
whitespace_cb(mxml_node_t *node, /* I - Element node */
int where) /* I - Open or close tag? */
{
const char *name; /* Name of element */
/*
* We can conditionally break to a new line before or after any element.
* These are just common HTML elements...
*/
name = node-&gt;value.element.name;
if (!strcmp(name, &quot;html&quot;) || !strcmp(name, &quot;head&quot;) || !strcmp(name, &quot;body&quot;) ||
!strcmp(name, &quot;pre&quot;) || !strcmp(name, &quot;p&quot;) ||
!strcmp(name, &quot;h1&quot;) || !strcmp(name, &quot;h2&quot;) || !strcmp(name, &quot;h3&quot;) ||
!strcmp(name, &quot;h4&quot;) || !strcmp(name, &quot;h5&quot;) || !strcmp(name, &quot;h6&quot;))
{
/*
* Newlines before open and after close...
*/
if (where == MXML_WS_BEFORE_OPEN || where == MXML_WS_AFTER_CLOSE)
return (&quot;\n&quot;);
}
else if (!strcmp(name, &quot;dl&quot;) || !strcmp(name, &quot;ol&quot;) || !strcmp(name, &quot;ul&quot;))
{
/*
* Put a newline before and after list elements...
*/
return (&quot;\n&quot;);
}
else if (!strcmp(name, &quot;dd&quot;) || !strcmp(name, &quot;dt&quot;) || !strcmp(name, &quot;li&quot;))
{
/*
* Put a tab before &lt;li&gt;'s, &lt;dd&gt;'s, and &lt;dt&gt;'s, and a newline after them...
*/
if (where == MXML_WS_BEFORE_OPEN)
return (&quot;\t&quot;);
else if (where == MXML_WS_AFTER_CLOSE)
return (&quot;\n&quot;);
}
/*
* Return NULL for no added whitespace...
*/
return (NULL);
}
</PRE>
<!-- NEW PAGE -->
<P>To use this callback function, simply use the name when you call any
of the save functions:</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, whitespace_cb);
fclose(fp);
</PRE>
<HR NOSHADE>
<A HREF="toc.html">Contents</A>
<A HREF="LoadCallbacks.html">Previous</A>
<A HREF="CustomDataTypes.html">Next</A>
</BODY>
</HTML>