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.
133 lines
4.2 KiB
133 lines
4.2 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="LoadCallbacks.html">
|
|
<LINK REL="Next" HREF="CustomDataTypes.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="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 "where" 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 following whitespace callback can be used to add whitespace to
|
|
XHTML output to make it more readable in a standard text editor:</P>
|
|
<PRE>
|
|
const char *
|
|
whitespace_cb(mxml_node_t *node,
|
|
int where)
|
|
{
|
|
const char *name;
|
|
|
|
/*
|
|
* We can conditionally break to a new line
|
|
* before or after any element. These are
|
|
* just common HTML elements...
|
|
*/
|
|
|
|
name = node->value.element.name;
|
|
|
|
if (!strcmp(name, "html") ||
|
|
!strcmp(name, "head") ||
|
|
!strcmp(name, "body") ||
|
|
!strcmp(name, "pre") ||
|
|
!strcmp(name, "p") ||
|
|
!strcmp(name, "h1") ||
|
|
!strcmp(name, "h2") ||
|
|
!strcmp(name, "h3") ||
|
|
!strcmp(name, "h4") ||
|
|
!strcmp(name, "h5") ||
|
|
!strcmp(name, "h6"))
|
|
{
|
|
/*
|
|
* Newlines before open and after
|
|
* close...
|
|
*/
|
|
|
|
if (where == MXML_WS_BEFORE_OPEN ||
|
|
where == MXML_WS_AFTER_CLOSE)
|
|
return ("\n");
|
|
}
|
|
else if (!strcmp(name, "dl") ||
|
|
!strcmp(name, "ol") ||
|
|
!strcmp(name, "ul"))
|
|
{
|
|
/*
|
|
* Put a newline before and after list
|
|
* elements...
|
|
*/
|
|
|
|
return ("\n");
|
|
}
|
|
else if (!strcmp(name, "dd") ||
|
|
!strcmp(name, "dt") ||
|
|
!strcmp(name, "li"))
|
|
{
|
|
/*
|
|
* Put a tab before <li>'s, * <dd>'s,
|
|
* and <dt>'s, and a newline after them...
|
|
*/
|
|
|
|
if (where == MXML_WS_BEFORE_OPEN)
|
|
return ("\t");
|
|
else if (where == MXML_WS_AFTER_CLOSE)
|
|
return ("\n");
|
|
}
|
|
|
|
/*
|
|
* Return NULL for no added whitespace...
|
|
*/
|
|
|
|
return (NULL);
|
|
}
|
|
</PRE>
|
|
<P>To use this callback function, simply use the name when you call any
|
|
of the save functions:</P>
|
|
<PRE>
|
|
FILE *fp;
|
|
mxml_node_t *tree;
|
|
|
|
fp = fopen("filename.xml", "w");
|
|
mxmlSaveFile(tree, fp, <B>whitespace_cb</B>);
|
|
fclose(fp);
|
|
</PRE>
|
|
|
|
<!-- NEED 10 -->
|
|
<HR NOSHADE>
|
|
<A HREF="index.html">Contents</A>
|
|
<A HREF="LoadCallbacks.html">Previous</A>
|
|
<A HREF="CustomDataTypes.html">Next</A>
|
|
</BODY>
|
|
</HTML>
|
|
|