mirror of
https://github.com/michaelrsweet/mxml.git
synced 2024-11-17 08:25:31 +00:00
124 lines
4.4 KiB
HTML
124 lines
4.4 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.1</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="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 "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. 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->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>
|
|
|
|
<!-- 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("filename.xml", "w");
|
|
<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>
|