Michael R Sweet
Copyright © 2003-2024, All Rights Reserved.
Mini-XML is a small XML parsing library that you can use to read XML data files or strings in your application without requiring large non-standard libraries. Mini-XML provides the following functionality:
Reading of UTF-8 and UTF-16 and writing of UTF-8 encoded XML files and strings.
Data is stored in a linked-list tree structure, preserving the XML data hierarchy.
SAX (streamed) reading of XML files and strings to minimize memory usage.
Supports arbitrary element names, attributes, and attribute values with no preset limits, just available memory.
Supports integer, real, opaque ("CDATA"), text, and custom data types in "leaf" nodes.
Functions for creating and managing trees of data.
"Find" and "walk" functions for easily locating and navigating trees of data.
Mini-XML doesn't do validation or other types of processing on the data based upon schema files or other sources of definition information.
Mini-XML was initially developed for the Gutenprint project to replace the rather large and unwieldy libxml2
library with something substantially smaller and easier-to-use. It all began one morning in June of 2003 when Robert posted the following sentence to the developer's list:
It's bad enough that we require libxml2, but rolling our own XML parser is a bit more than we can handle.
I then replied with:
Given the limited scope of what you use in XML, it should be trivial to code a mini-XML API in a few hundred lines of code.
I took my own challenge and coded furiously for two days to produce the initial public release of Mini-XML, total lines of code: 696. Robert promptly integrated Mini-XML into Gutenprint and removed libxml2.
Thanks to lots of feedback and support from various developers, Mini-XML has evolved since then to provide a more complete XML implementation and now stands at a whopping 3,875 lines of code, compared to 175,808 lines of code for libxml2 version 2.11.7.
The Mini-XML home page can be found at https://www.msweet.org/mxml. From there you can download the current version of Mini-XML, access the issue tracker, and find other resources.
Mini-XML v4 has a slightly different API than prior releases. See the Migrating from Mini-XML v3.x chapter for details.
The Mini-XML library is copyright © 2003-2024 by Michael R Sweet and is provided under the Apache License Version 2.0 with an (optional) exception to allow linking against GPL2/LGPL2-only software. See the files "LICENSE" and "NOTICE" for more information.
Mini-XML provides a single header file which you include:
#include <mxml.h>
The Mini-XML library is included with your program using the -lmxml4
option:
gcc -o myprogram myprogram.c -lmxml4
If you have the pkg-config
software installed, you can use it to determine the proper compiler and linker options for your installation:
gcc `pkg-config --cflags mxml4` -o myprogram myprogram.c `pkg-config --libs mxml4`
Note: The library name "mxml4" is a configure-time option. If you use the
--disable-libmxml4-prefix
configure option the library is named "mxml".
You load an XML file using the mxmlLoadFile function:
mxml_node_t *
mxmlLoadFilename(mxml_node_t *top, const char *filename,
mxml_load_cb_t load_cb, void *load_cbdata,
mxml_sax_cb_t sax_cb, void *sax_cbdata);
The load_cb
argument specifies a function that assigns child (value) node types for each element in the document. The default callback (NULL
) supports passing a pointer to an mxml_type_t
variable containing the type of value nodes. For example, to load the XML file "filename.xml" containing literal strings you can use:
mxml_node_t *tree;
mxml_type_t type = MXML_TYPE_OPAQUE;
tree = mxmlLoadFilename(/*top*/NULL, "filename.xml",
/*load_cb*/NULL, /*load_cbdata*/&type,
/*sax_cb*/NULL, /*sax_cbdata*/NULL);
Mini-XML also provides functions to load from a FILE
pointer, a file descriptor, or string:
mxml_node_t *
mxmlLoadFd(mxml_node_t *top, int fd,
mxml_load_cb_t load_cb, void *load_cbdata,
mxml_sax_cb_t sax_cb, void *sax_cbdata);
mxml_node_t *
mxmlLoadFile(mxml_node_t *top, FILE *fp,
mxml_load_cb_t load_cb, void *load_cbdata,
mxml_sax_cb_t sax_cb, void *sax_cbdata);
mxml_node_t *
mxmlLoadString(mxml_node_t *top, const char *s,
mxml_load_cb_t load_cb, void *load_cbdata,
mxml_sax_cb_t sax_cb, void *sax_cbdata);
The load_xxx
arguments to the mxmlLoadXxx functions are a callback function and a data pointer which are used to determine the value type of each data node in an XML document. The default (NULL
) callback expects the load_cbdata
argument to be a pointer to a mxml_type_t
variable that contains the desired value node type - if NULL
, it uses the MXML_TYPE_TEXT
(whitespace-separated text) type.
You can provide your own callback function for more complex XML documents. Your callback function will receive a pointer to the current element node and must return the value type of the immediate children for that element node: MXML_TYPE_CUSTOM
, MXML_TYPE_INTEGER
, MXML_TYPE_OPAQUE
, MXML_TYPE_REAL
, or MXML_TYPE_TEXT
. The function is called after 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 following callback function looks for an attribute named "type" or the element name to determine the value type for its child nodes:
mxml_type_t
my_load_cb(void *cbdata, mxml_node_t *node)
{
const char *type;
/*
* You can lookup attributes and/or use the element name,
* hierarchy, etc...
*/
type = mxmlElementGetAttr(node, "type");
if (type == NULL)
type = mxmlGetElement(node);
if (type == NULL)
type = "text";
if (!strcmp(type, "integer"))
return (MXML_TYPE_INTEGER);
else if (!strcmp(type, "opaque"))
return (MXML_TYPE_OPAQUE);
else if (!strcmp(type, "real"))
return (MXML_TYPE_REAL);
else
return (MXML_TYPE_TEXT);
}
To use this callback function, simply specify it when you call any of the load functions:
mxml_node_t *tree;
tree = mxmlLoadFilename(/*top*/NULL, "filename.xml",
my_load_cb, /*load_cbdata*/NULL,
/*sax_cb*/NULL, /*sax_cbdata*/NULL);
Every piece of information in an XML file is stored in memory in "nodes". Nodes are defined by the mxml_node_t
structure. Each node has a typed value, optional user data, a parent node, sibling nodes (previous and next), and potentially child nodes.
For example, if you have an XML file like the following:
<?xml version="1.0" encoding="utf-8"?>
<data>
<node>val1</node>
<node>val2</node>
<node>val3</node>
<group>
<node>val4</node>
<node>val5</node>
<node>val6</node>
</group>
<node>val7</node>
<node>val8</node>
</data>
the node tree for the file would look like the following in memory:
?xml version="1.0" encoding="utf-8"?
|
data
|
node - node - node - group - node - node
| | | | | |
val1 val2 val3 | val7 val8
|
node - node - node
| | |
val4 val5 val6
where "-" is a pointer to the sibling node and "|" is a pointer to the first child or parent node.
The mxmlGetType function gets the type of a node:
mxml_type_t
mxmlGetType(mxml_node_t *node);
MXML_TYPE_CDATA
: CDATA,
MXML_TYPE_COMMENT
: A comment,
MXML_TYPE_CUSTOM
: A custom value defined by your application,
MXML_TYPE_DECLARATION
: A declaration such as <!DOCTYPE html>
,
MXML_TYPE_DIRECTIVE
: A processing instruction such as <?xml version="1.0"?>
,
MXML_TYPE_ELEMENT
: An XML element,
MXML_TYPE_INTEGER
: A whitespace-delimited integer value,
MXML_TYPE_OPAQUE
: An opaque string value that preserves all whitespace,
MXML_TYPE_REAL
: A whitespace-delimited floating point value, or
MXML_TYPE_TEXT
: A whitespace-delimited text (fragment) value.
The parent and sibling nodes are accessed using the mxmlGetParent, mxmlGetNextSibling, and mxmlGetPreviousSibling functions, while the children of an element node are accessed using the mxmlGetFirstChild or mxmlGetLastChild functions:
mxml_node_t *
mxmlGetFirstChild(mxml_node_t *node);
mxml_node_t *
mxmlGetLastChild(mxml_node_t *node);
mxml_node_t *
mxmlGetNextSibling(mxml_node_t *node);
mxml_node_t *
mxmlGetParent(mxml_node_t *node);
mxml_node_t *
mxmlGetPrevSibling(mxml_node_t *node);
The mxmlGetUserData function gets any user (application) data associated with the node:
void *
mxmlGetUserData(mxml_node_t *node);
You can create and update XML documents in memory using the various mxmlNewXxx functions. The following code will create the XML document described in the previous section:
mxml_node_t *xml; /* <?xml version="1.0"?> */
mxml_node_t *data; /* <data> */
mxml_node_t *node; /* <node> */
mxml_node_t *group; /* <group> */
xml = mxmlNewXML("1.0");
data = mxmlNewElement(xml, "data");
node = mxmlNewElement(data, "node");
mxmlNewText(node, false, "val1");
node = mxmlNewElement(data, "node");
mxmlNewText(node, false, "val2");
node = mxmlNewElement(data, "node");
mxmlNewText(node, false, "val3");
group = mxmlNewElement(data, "group");
node = mxmlNewElement(group, "node");
mxmlNewText(node, false, "val4");
node = mxmlNewElement(group, "node");
mxmlNewText(node, false, "val5");
node = mxmlNewElement(group, "node");
mxmlNewText(node, false, "val6");
node = mxmlNewElement(data, "node");
mxmlNewText(node, false, "val7");
node = mxmlNewElement(data, "node");
mxmlNewText(node, false, "val8");
We start by creating the declaration node common to all XML files using the mxmlNewXML function:
xml = mxmlNewXML("1.0");
We then create the <data>
node used for this document using the mxmlNewElement function. The first argument specifies the parent node (xml
) while the second specifies the element name (data
):
data = mxmlNewElement(xml, "data");
Each <node>...</node>
in the file is created using the mxmlNewElement and mxmlNewText functions. The first argument of mxmlNewText specifies the parent node (node
). The second argument specifies whether whitespace appears before the text - false
in this case. The last argument specifies the actual text to add:
node = mxmlNewElement(data, "node");
mxmlNewText(node, false, "val1");
The resulting in-memory XML document can then be saved or processed just like one loaded from disk or a string.
You save an XML file using the mxmlSaveFilename function:
bool
mxmlSaveFilename(mxml_node_t *node, const char *filename,
mxml_save_cb_t cb, void *cbdata);
The cb
and cbdata
arguments specify a function and data pointer that is called to determine what whitespace (if any) is inserted before and after each element node. A NULL
value tells Mini-XML to not include any extra whitespace. For example, so save an XML file to the file "filename.xml" with no extra whitespace:
mxmlSaveFile(xml, "filename.xml", /*cb*/NULL, /*cbdata*/NULL);
Mini-XML also provides functions to save to a file descriptor, FILE
pointer, or strings:
char *
mxmlSaveAllocString(mxml_node_t *node, mxml_save_cb_t cb,
void *cbdata);
bool
mxmlSaveFd(mxml_node_t *node, int fd, mxml_save_cb_t cb,
void *cbdata);
bool
mxmlSaveFile(mxml_node_t *node, FILE *fp, mxml_save_cb_t cb,
void *cbdata);
size_t
mxmlSaveString(mxml_node_t *node, char *buffer, size_t bufsize,
mxml_save_cb_t cb, void *cbdata);
When saving XML documents, Mini-XML normally wraps output lines at column 75 so that the text is readable in terminal windows. The mxmlSetWrapMargin function overrides the default wrap margin for the current thread:
void mxmlSetWrapMargin(int column);
For example, the following code sets the margin to 132 columns:
mxmlSetWrapMargin(132);
while the following code disables wrapping by setting the margin to 0:
mxmlSetWrapMargin(0);
The last arguments to the mxmlSaveXxx functions are a callback function and data pointer which is used to automatically insert whitespace in an XML document. 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 MXML_WS_BEFORE_OPEN
, MXML_WS_AFTER_OPEN
, MXML_WS_BEFORE_CLOSE
, or MXML_WS_AFTER_CLOSE
. The callback function should return NULL
if no whitespace should be added or 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:
const char *
whitespace_cb(void *cbdata, mxml_node_t *node, mxml_ws_t where)
{
const char *element;
/*
* We can conditionally break to a new line before or after
* any element. These are just common HTML elements...
*/
element = mxmlGetElement(node);
if (!strcmp(element, "html") ||
!strcmp(element, "head") ||
!strcmp(element, "body") ||
!strcmp(element, "pre") ||
!strcmp(element, "p") ||
!strcmp(element, "h1") ||
!strcmp(element, "h2") ||
!strcmp(element, "h3") ||
!strcmp(element, "h4") ||
!strcmp(element, "h5") ||
!strcmp(element, "h6"))
{
/*
* Newlines before open and after close...
*/
if (where == MXML_WS_BEFORE_OPEN ||
where == MXML_WS_AFTER_CLOSE)
return ("\n");
}
else if (!strcmp(element, "dl") ||
!strcmp(element, "ol") ||
!strcmp(element, "ul"))
{
/*
* Put a newline before and after list elements...
*/
return ("\n");
}
else if (!strcmp(element, "dd") ||
!strcmp(element, "dt") ||
!strcmp(element, "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");
}
/*
* Otherwise return NULL for no added whitespace...
*/
return (NULL);
}
To use this callback function, simply use the name when you call any of the save functions:
FILE *fp;
mxml_node_t *tree;
fp = fopen("filename.xml", "w");
mxmlSaveFile(tree, fp, whitespace_cb, /*cbdata*/NULL);
fclose(fp);
Once you are done with the XML data, use the mxmlDelete function to free the memory that is used for a particular node and its children:
void
mxmlDelete(mxml_node_t *tree);
You can also use reference counting to manage memory usage. The mxmlRetain and mxmlRelease functions increment and decrement a node's use count, respectively. When the use count goes to zero, mxmlRelease calls mxmlDelete to actually free the memory used by the node tree. New nodes start with a use count of 1
.
Element (MXML_TYPE_ELEMENT
) nodes are created using the mxmlNewElement function. Element attributes are set using the mxmlElementSetAttr and mxmlElementSetAttrf functions and cleared using the mxmlElementClearAttr function:
mxml_node_t *
mxmlNewElement(mxml_node_t *parent, const char *name);
void
mxmlElementClearAttr(mxml_node_t *node, const char *name);
void
mxmlElementSetAttr(mxml_node_t *node, const char *name,
const char *value);
void
mxmlElementSetAttrf(mxml_node_t *node, const char *name,
const char *format, ...);
The mxmlGetElement function retrieves the element name while the mxmlElementGetAttr function retrieves the value string for a named attribute associated with the element. The mxmlElementGetAttrByIndex and mxmlElementGetAttrCount functions retrieve attributes by index:
const char *
mxmlGetElement(mxml_node_t *node);
const char *
mxmlElementGetAttr(mxml_node_t *node, const char *name);
const char *
mxmlElementGetAttrByIndex(mxml_node_t *node, size_t idx,
const char **name);
size_t
mxmlElementGetAttrCount(mxml_node_t *node);
CDATA (MXML_TYPE_CDATA
) nodes are created using the mxmlNewCDATA and mxmlNewCDATAf functions:
mxml_node_t *
mxmlNewCDATA(mxml_node_t *parent, const char *string);
mxml_node_t *
mxmlNewCDATAf(mxml_node_t *parent, const char *format, ...);
The mxmlGetCDATA function retrieves the CDATA string pointer for a node:
const char *
mxmlGetCDATA(mxml_node_t *node);
Comment (MXML_TYPE_COMMENT
) nodes are created using the mxmlNewComment and mxmlNewCommentf functions, for example:
mxml_node_t *node = mxmlNewComment(" This is a comment ");
mxml_node_t *node = mxmlNewCommentf(" This is comment %d ", 42);
Similarly, the mxmlGetComment function retrieves the comment string pointer for a node:
const char *comment = mxmlGetComment(node);
/* returns " This is a comment " */
Processing instruction (MXML_TYPE_DIRECTIVE
) nodes are created using the mxmlNewDirective and mxmlNewDirectivef functions:
mxml_node_t *node = mxmlNewDirective("xml-stylesheet type=\"text/css\" href=\"style.css\"");
mxml_node_t *node = mxmlNewDirectivef("xml version=\"%s\"", version);
The mxmlGetDirective function retrieves the processing instruction string for a node:
const char *instr = mxmlGetElement(node);
/* returns "xml-stylesheet type=\"text/css\" href=\"style.css\"" */
The mxmlNewXML function can be used to create the top-level "xml" processing instruction with an associated version number:
mxml_node_t *
mxmlNewXML(const char *version);
Integer (MXML_TYPE_INTEGER
) nodes are created using the mxmlNewInteger function:
mxml_node_t *
mxmlNewInteger(mxml_node_t *parent, long integer);
The mxmlGetInteger function retrieves the integer value for a node:
long
mxmlGetInteger(mxml_node_t *node);
Opaque string (MXML_TYPE_OPAQUE
) nodes are created using the mxmlNewOpaque and mxmlNewOpaquef functions:
mxml_node_t *
mxmlNewOpaque(mxml_node_t *parent, const char *opaque);
mxml_node_t *
mxmlNewOpaquef(mxml_node_t *parent, const char *format, ...);
The mxmlGetOpaque function retrieves the opaque string pointer for a node:
const char *
mxmlGetOpaque(mxml_node_t *node);
Whitespace-delimited text string (MXML_TYPE_TEXT
) nodes are created using the mxmlNewText and mxmlNewTextf functions. Each text node consists of a text string and (leading) whitespace boolean value.
mxml_node_t *
mxmlNewText(mxml_node_t *parent, bool whitespace,
const char *string);
mxml_node_t *
mxmlNewTextf(mxml_node_t *parent, bool whitespace,
const char *format, ...);
The mxmlGetText function retrieves the text string pointer and whitespace boolean value for a node:
const char *
mxmlGetText(mxml_node_t *node, bool *whitespace);
Real number (MXML_TYPE_REAL
) nodes are created using the mxmlNewReal function:
mxml_node_t *
mxmlNewReal(mxml_node_t *parent, double real);
The mxmlGetReal function retrieves the real number for a node:
double
mxmlGetReal(mxml_node_t *node);
Mini-XML provides many functions for enumerating, searching, and indexing XML documents.
The mxmlFindPath function finds the (first) value node under a specific element using a "path":
mxml_node_t *
mxmlFindPath(mxml_node_t *node, const char *path);
The path
string can contain the "*" wildcard to match a single element node in the hierarchy. For example, the following code will find the first "node" element under the "group" element, first using an explicit path and then using a wildcard:
mxml_node_t *value = mxmlFindPath(xml, "data/group/node");
mxml_node_t *value = mxmlFindPath(xml, "data/*/node");
The mxmlFindElement function can be used to find a named element, optionally matching an attribute and value:
mxml_node_t *
mxmlFindElement(mxml_node_t *node, mxml_node_t *top,
const char *element, const char *attr,
const char *value, int descend);
The "element", "attr", and "value" arguments can be passed as NULL
to act as wildcards, e.g.:
/* Find the first "a" element */
node = mxmlFindElement(tree, tree, "a", NULL, NULL,
MXML_DESCEND_ALL);
/* Find the first "a" element with "href" attribute */
node = mxmlFindElement(tree, tree, "a", "href", NULL,
MXML_DESCEND_ALL);
/* Find the first "a" element with "href" to a URL */
node = mxmlFindElement(tree, tree, "a", "href",
"http://msweet.org/",
MXML_DESCEND_ALL);
/* Find the first element with a "src" attribute*/
node = mxmlFindElement(tree, tree, NULL, "src", NULL,
MXML_DESCEND_ALL);
/* Find the first element with a "src" = "foo.jpg" */
node = mxmlFindElement(tree, tree, NULL, "src", "foo.jpg",
MXML_DESCEND_ALL);
You can also iterate with the same function:
mxml_node_t *node;
for (node = mxmlFindElement(tree, tree, "element", NULL,
NULL, MXML_DESCEND_ALL);
node != NULL;
node = mxmlFindElement(node, tree, "element", NULL,
NULL, MXML_DESCEND_ALL))
{
... do something ...
}
The descend
argument (MXML_DESCEND_ALL
in the examples above) can be one of three constants:
MXML_DESCEND_NONE
: ignore child nodes in the element hierarchy, instead using siblings (same level) or parent nodes (above) until the top (root) node is reached.
MXML_DESCEND_FIRST
: start the search with the first child of the node, and then search siblings. You'll normally use this when iterating through direct children of a parent node, e.g. all of the "node" and "group" elements under the "?xml" parent node in the previous example.
MXML_DESCEND_ALL
: search child nodes first, then sibling nodes, and then parent nodes.
While the mxmlFindNode and mxmlFindPath functions will find a particular element node, sometimes you need to iterate over all nodes. The mxmlWalkNext and mxmlWalkPrev functions can be used to iterate through the XML node tree:
mxml_node_t *
mxmlWalkNext(mxml_node_t *node, mxml_node_t *top,
int descend);
mxml_node_t *
mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top,
int descend);
Depending on the value of the descend
argument, these functions will automatically traverse child, sibling, and parent nodes until the top
node is reached. For example, the following code will iterate over all of the nodes in the sample XML document in the previous section:
mxml_node_t *node;
for (node = xml;
node != NULL;
node = mxmlWalkNext(node, xml, MXML_DESCEND_ALL))
{
... do something ...
}
The nodes will be returned in the following order:
<?xml version="1.0" encoding="utf-8"?>
<data>
<node>
val1
<node>
val2
<node>
val3
<group>
<node>
val4
<node>
val5
<node>
val6
<node>
val7
<node>
val8
The mxmlIndexNew function allows you to create an index of nodes for faster searching and enumeration:
mxml_index_t *
mxmlIndexNew(mxml_node_t *node, const char *element,
const char *attr);
The element
and attr
arguments control which elements are included in the index. If element
is not NULL
then only elements with the specified name are added to the index. Similarly, if attr
is not NULL
then only elements containing the specified attribute are added to the index. The nodes are sorted in the index.
For example, the following code creates an index of all "id" values in an XML document:
mxml_index_t *ind = mxmlIndexNew(xml, NULL, "id");
Once the index is created, the mxmlIndexFind function can be used to find a matching node:
mxml_node_t *
mxmlIndexFind(mxml_index_t *ind, const char *element,
const char *value);
For example, the following code will find the element whose "id" string is "42":
mxml_node_t *node = mxmlIndexFind(ind, NULL, "42");
Alternately, the mxmlIndexReset and mxmlIndexEnum functions can be used to enumerate the nodes in the index:
mxml_node_t *
mxmlIndexReset(mxml_index_t *ind);
mxml_node_t *
mxmlIndexEnum(mxml_index_t *ind);
Typically these functions will be used in a for
loop:
mxml_node_t *node;
for (node = mxmlIndexReset(ind);
node != NULL;
node = mxmlIndexEnum(ind))
{
... do something ...
}
The mxmlIndexCount function returns the number of nodes in the index:
size_t
mxmlIndexGetCount(mxml_index_t *ind);
Finally, the mxmlIndexDelete function frees all memory associated with the index:
void
mxmlIndexDelete(mxml_index_t *ind);
Mini-XML supports custom data types via per-thread load and save callbacks. Only a single set of callbacks can be active at any time for the current thread, however your callbacks can store additional information in order to support multiple custom data types as needed. The MXML_TYPE_CUSTOM
node type identifies custom data nodes.
The mxmlGetCustom function retrieves the custom value pointer for a node.
const void *
mxmlGetCustom(mxml_node_t *node);
Custom (MXML_TYPE_CUSTOM
) nodes are created using the mxmlNewCustom function or using a custom per-thread load callbacks specified using the mxmlSetCustomHandlers function:
typedef void (*mxml_custom_destroy_cb_t)(void *);
typedef bool (*mxml_custom_load_cb_t)(mxml_node_t *, const char *);
typedef char *(*mxml_custom_save_cb_t)(mxml_node_t *);
mxml_node_t *
mxmlNewCustom(mxml_node_t *parent, void *data,
mxml_custom_destroy_cb_t destroy);
int
mxmlSetCustom(mxml_node_t *node, void *data,
mxml_custom_destroy_cb_t destroy);
void
mxmlSetCustomHandlers(mxml_custom_load_cb_t load,
mxml_custom_save_cb_t save);
The load callback receives a pointer to the current data node and a string of opaque character data from the XML source with character entities converted to the corresponding UTF-8 characters. For example, if we wanted to support a custom date/time type whose value is encoded as "yyyy-mm-ddThh:mm:ssZ" (ISO format), the load callback would look like the following:
typedef struct
{
unsigned year, /* Year */
month, /* Month */
day, /* Day */
hour, /* Hour */
minute, /* Minute */
second; /* Second */
time_t unix; /* UNIX time */
} iso_date_time_t;
bool
load_custom(mxml_node_t *node, const char *data)
{
iso_date_time_t *dt;
struct tm tmdata;
/*
* Allocate data structure...
*/
dt = calloc(1, sizeof(iso_date_time_t));
/*
* Try reading 6 unsigned integers from the data string...
*/
if (sscanf(data, "%u-%u-%uT%u:%u:%uZ", &(dt->year),
&(dt->month), &(dt->day), &(dt->hour),
&(dt->minute), &(dt->second)) != 6)
{
/*
* Unable to read numbers, free the data structure and
* return an error...
*/
free(dt);
return (false);
}
/*
* Range check values...
*/
if (dt->month < 1 || dt->month > 12 ||
dt->day < 1 || dt->day > 31 ||
dt->hour < 0 || dt->hour > 23 ||
dt->minute < 0 || dt->minute > 59 ||
dt->second < 0 || dt->second > 60)
{
/*
* Date information is out of range...
*/
free(dt);
return (false);
}
/*
* Convert ISO time to UNIX time in seconds...
*/
tmdata.tm_year = dt->year - 1900;
tmdata.tm_mon = dt->month - 1;
tmdata.tm_day = dt->day;
tmdata.tm_hour = dt->hour;
tmdata.tm_min = dt->minute;
tmdata.tm_sec = dt->second;
dt->unix = gmtime(&tmdata);
/*
* Assign custom node data and destroy (free) function
* pointers...
*/
mxmlSetCustom(node, data, free);
/*
* Return with no errors...
*/
return (true);
}
The function itself can return true
on success or false
if it is unable to decode the custom data or the data contains an error. Custom data nodes contain a void
pointer to the allocated custom data for the node and a pointer to a destructor function which will free the custom data when the node is deleted. In this example, we use the standard free
function since everything is contained in a single calloc'd block.
The save callback receives the node pointer and returns an allocated string containing the custom data value. The following save callback could be used for our ISO date/time type:
char *
save_custom(mxml_node_t *node)
{
char data[255];
iso_date_time_t *dt;
dt = (iso_date_time_t *)mxmlGetCustom(node);
snprintf(data, sizeof(data),
"%04u-%02u-%02uT%02u:%02u:%02uZ",
dt->year, dt->month, dt->day, dt->hour,
dt->minute, dt->second);
return (strdup(data));
}
You register the callback functions using the mxmlSetCustomCallbacks function:
mxmlSetCustomCallbacks(load_custom, save_custom);
Mini-XML supports an implementation of the Simple API for XML (SAX) which allows you to load and process an XML document as a stream of nodes. Aside from allowing you to process XML documents of any size, the Mini-XML implementation also allows you to retain portions of the document in memory for later processing.
The mxmlLoadXxx functions support a SAX callback and associated data. The callback function receives the data pointer you supplied, the node, and an event code and returns true
to continue processing or false
to stop:
bool
sax_cb(void *cbdata, mxml_node_t *node,
mxml_sax_event_t event)
{
... do something ...
// Continue processing...
return (true);
}
The event will be one of the following:
MXML_SAX_EVENT_CDATA
: CDATA was just read.
MXML_SAX_EVENT_COMMENT
: A comment was just read.
MXML_SAX_EVENT_DATA
: Data (integer, opaque, real, or text) was just read.
MXML_SAX_EVENT_DECLARATION
: A declaration was just read.
MXML_SAX_EVENT_DIRECTIVE
: A processing directive/instruction was just read.
MXML_SAX_EVENT_ELEMENT_CLOSE
- A close element was just read (</element>
)
MXML_SAX_EVENT_ELEMENT_OPEN
- An open element was just read (<element>
)
Elements are released after the close element is processed. All other nodes are released after they are processed. The SAX callback can retain the node using the mxmlRetain function. For example, the following SAX callback will retain all nodes, effectively simulating a normal in-memory load:
bool
sax_cb(void *cbdata, mxml_node_t *node, mxml_sax_event_t event)
{
if (event != MXML_SAX_ELEMENT_CLOSE)
mxmlRetain(node);
return (true);
}
More typically the SAX callback will only retain a small portion of the document that is needed for post-processing. For example, the following SAX callback will retain the title and headings in an XHTML file. It also retains the (parent) elements like <html>
, <head>
, and <body>
, and processing directives like <?xml ... ?>
and declarations like <!DOCTYPE ... >
:
bool
sax_cb(void *cbdata, mxml_node_t *node,
mxml_sax_event_t event)
{
if (event == MXML_SAX_ELEMENT_OPEN)
{
/*
* Retain headings and titles...
*/
const char *element = mxmlGetElement(node);
if (!strcmp(element, "html") ||
!strcmp(element, "head") ||
!strcmp(element, "title") ||
!strcmp(element, "body") ||
!strcmp(element, "h1") ||
!strcmp(element, "h2") ||
!strcmp(element, "h3") ||
!strcmp(element, "h4") ||
!strcmp(element, "h5") ||
!strcmp(element, "h6"))
mxmlRetain(node);
}
else if (event == MXML_SAX_DECLARATION)
mxmlRetain(node);
else if (event == MXML_SAX_DIRECTIVE)
mxmlRetain(node);
else if (event == MXML_SAX_DATA)
{
if (mxmlGetRefCount(mxmlGetParent(node)) > 1)
{
/*
* If the parent was retained, then retain this data
* node as well.
*/
mxmlRetain(node);
}
}
return (true);
}
The resulting skeleton document tree can then be searched just like one loaded without the SAX callback function. For example, a filter that reads an XHTML document from stdin and then shows the title and headings in the document would look like:
mxml_node_t *doc, *title, *body, *heading;
doc = mxmlLoadFd(/*top*/NULL, /*fd*/0,
/*load_cb*/NULL, /*load_cbdata*/NULL,
sax_cb, /*sax_cbdata*/NULL);
title = mxmlFindElement(doc, doc, "title", NULL, NULL,
MXML_DESCEND_ALL);
if (title)
print_children(title);
body = mxmlFindElement(doc, doc, "body", NULL, NULL,
MXML_DESCEND_ALL);
if (body)
{
for (heading = mxmlGetFirstChild(body);
heading;
heading = mxmlGetNextSibling(heading))
print_children(heading);
}
The print_children
function is:
void
print_children(mxml_node_t *parent)
{
mxml_node_t *node;
const char *text;
bool whitespace;
for (node = mxmlGetFirstChild(parent);
node != NULL;
node = mxmlGetNextSibling(node))
{
text = mxmlGetText(node, &whitespace);
if (whitespace)
putchar(' ');
fputs(text, stdout);
}
putchar('\n');
}
The following incompatible API changes were made in Mini-XML v4.0:
SAX events are now named MXML_SAX_EVENT_foo
instead of MXML_SAX_foo
.
SAX callbacks now return a boolean value.
The mxmlSAXLoadXxx functions have been removed in favor of passing the SAX callback function and data pointers to the mxmlLoadXxx functions.
Node types are now named MXML_TYPE_foo
instead of MXML_foo
.
Descend values are now normalized to MXML_DESCEND_ALL
, MXML_DESCEND_FIRST
, and MXML_DESCEND_NONE
.
Functions that returned 0
on success and -1
on error now return true
on success and false
on error.
CDATA nodes ("<![CDATA[...]]>
") now have their own type (MXML_TYPE_CDATA
).
Comment nodes ("<!-- ... -->
") now have their own type (MXML_TYPE_COMMENT
).
Custom node callbacks are now set using the mxmlSetCustomCallbacks function instead of mxmlSetCustomHandlers.
Declaration nodes ("<!...>
") now have their own type (MXML_TYPE_DECLARATION
).
Element attributes are now cleared with the mxmlElementClearAttr function instead of mxmlElementDeleteAttr.
Processing instruction/directive nodes ("<?...?>
") now have their own type (MXML_TYPE_DIRECTIVE
).
Integer nodes (MXML_TYPE_INTEGER
) now use the long
type.
Text nodes (MXML_TYPE_TEXT
) now use the bool
type for the whitespace value.
Add a node to a tree.
void mxmlAdd(mxml_node_t *parent, mxml_add_t add, mxml_node_t *child, mxml_node_t *node);
parent | Parent node |
---|---|
add | Where to add, MXML_ADD_BEFORE or MXML_ADD_AFTER |
child | Child node for where or MXML_ADD_TO_PARENT |
node | Node to add |
Adds the specified node to the parent. If the child argument is not
NULL
, puts the new node before or after the specified child depending
on the value of the add
argument. If the child argument is NULL
,
puts the new node at the beginning of the child list (MXML_ADD_BEFORE
)
or at the end of the child list (MXML_ADD_AFTER
).
Delete a node and all of its children.
void mxmlDelete(mxml_node_t *node);
node | Node to delete |
---|
If the specified node has a parent, this function first removes the
node from its parent using the mxmlRemove
function.
Delete an attribute.
void mxmlElementClearAttr(mxml_node_t *node, const char *name);
node | Element |
---|---|
name | Attribute name |
Get an attribute.
const char *mxmlElementGetAttr(mxml_node_t *node, const char *name);
node | Element node |
---|---|
name | Name of attribute |
Attribute value or NULL
This function returns NULL
if the node is not an element or the
named attribute does not exist.
Get an element attribute by index.
const char *mxmlElementGetAttrByIndex(mxml_node_t *node, int idx, const char **name);
node | Node |
---|---|
idx | Attribute index, starting at 0 |
name | Attribute name |
Attribute value
The index ("idx") is 0-based. NULL
is returned if the specified index
is out of range.
Get the number of element attributes.
size_t mxmlElementGetAttrCount(mxml_node_t *node);
node | Node |
---|
Number of attributes
Set an attribute.
void mxmlElementSetAttr(mxml_node_t *node, const char *name, const char *value);
node | Element node |
---|---|
name | Name of attribute |
value | Attribute value |
If the named attribute already exists, the value of the attribute is replaced by the new string value. The string value is copied into the element node. This function does nothing if the node is not an element.
Set an attribute with a formatted value.
void mxmlElementSetAttrf(mxml_node_t *node, const char *name, const char *format, ...);
node | Element node |
---|---|
name | Name of attribute |
format | Printf-style attribute value |
... | Additional arguments as needed |
If the named attribute already exists, the value of the attribute is replaced by the new formatted string. The formatted string value is copied into the element node. This function does nothing if the node is not an element.
Add a callback to convert entities to Unicode.
bool mxmlEntityAddCallback(mxml_entity_cb_t cb, void *cbdata);
cb | Callback function to add |
---|---|
cbdata | Callback data |
true
on success, false
on failure
Get the character corresponding to a named entity.
int mxmlEntityGetValue(const char *name);
name | Entity name |
---|
Character value or -1
on error
The entity name can also be a numeric constant. -1
is returned if the
name is not known.
Remove a callback.
void mxmlEntityRemoveCallback(mxml_entity_cb_t cb);
cb | Callback function to remove |
---|
Find the named element.
mxml_node_t *mxmlFindElement(mxml_node_t *node, mxml_node_t *top, const char *element, const char *attr, const char *value, mxml_descend_t descend);
node | Current node |
---|---|
top | Top node |
element | Element name or NULL for any |
attr | Attribute name, or NULL for none |
value | Attribute value, or NULL for any |
descend | Descend into tree - MXML_DESCEND_ALL , MXML_DESCEND_NONE , or MXML_DESCEND_FIRST |
Element node or NULL
The search is constrained by the name, attribute name, and value; any
NULL
names or values are treated as wildcards, so different kinds of
searches can be implemented by looking for all elements of a given name
or all elements with a specific attribute. The descend argument determines
whether the search descends into child nodes; normally you will use
MXML_DESCEND_FIRST
for the initial search and MXML_NO_DESCEND
to find additional direct descendents of the node. The top node argument
constrains the search to a particular node's children.
Find a node with the given path.
mxml_node_t *mxmlFindPath(mxml_node_t *top, const char *path);
top | Top node |
---|---|
path | Path to element |
Found node or NULL
The "path" is a slash-separated list of element names. The name "" is
considered a wildcard for one or more levels of elements. For example,
"foo/one/two", "bar/two/one", "/one", and so forth.
The first child node of the found node is returned if the given node has
children and the first child is a value node.
Get the value for a CDATA node.
const char *mxmlGetCDATA(mxml_node_t *node);
node | Node to get |
---|
CDATA value or NULL
NULL
is returned if the node is not a CDATA element.
Get the value for a comment node.
const char *mxmlGetComment(mxml_node_t *node);
node | Node to get |
---|
Comment value or NULL
NULL
is returned if the node is not a comment.
Get the value for a custom node.
const void *mxmlGetCustom(mxml_node_t *node);
node | Node to get |
---|
Custom value or NULL
NULL
is returned if the node (or its first child) is not a custom
value node.
Get the value for a declaration node.
const char *mxmlGetDeclaration(mxml_node_t *node);
node | Node to get |
---|
Declaraction value or NULL
NULL
is returned if the node is not a declaration.
Get the value for a processing instruction node.
const char *mxmlGetDirective(mxml_node_t *node);
node | Node to get |
---|
Comment value or NULL
NULL
is returned if the node is not a processing instruction.
Get the name for an element node.
const char *mxmlGetElement(mxml_node_t *node);
node | Node to get |
---|
Element name or NULL
NULL
is returned if the node is not an element node.
Get the first child of an element node.
mxml_node_t *mxmlGetFirstChild(mxml_node_t *node);
node | Node to get |
---|
First child or NULL
NULL
is returned if the node is not an element node or if the node
has no children.
Get the integer value from the specified node or its first child.
long mxmlGetInteger(mxml_node_t *node);
node | Node to get |
---|
Integer value or 0
0
is returned if the node (or its first child) is not an integer value node.
Get the last child of an element node.
mxml_node_t *mxmlGetLastChild(mxml_node_t *node);
node | Node to get |
---|
Last child or NULL
NULL
is returned if the node is not an element node or if the node
has no children.
mxml_node_t *mxmlGetNextSibling(mxml_node_t *node);
node | Node to get |
---|
Get the next node for the current parent.
NULL
is returned if this is the last child for the current parent.
Get an opaque string value for a node or its first child.
const char *mxmlGetOpaque(mxml_node_t *node);
node | Node to get |
---|
Opaque string or NULL
NULL
is returned if the node (or its first child) is not an opaque
value node.
Get the parent node.
mxml_node_t *mxmlGetParent(mxml_node_t *node);
node | Node to get |
---|
Parent node or NULL
NULL
is returned for a root node.
Get the previous node for the current parent.
mxml_node_t *mxmlGetPrevSibling(mxml_node_t *node);
node | Node to get |
---|
Previous node or NULL
NULL
is returned if this is the first child for the current parent.
Get the real value for a node or its first child.
double mxmlGetReal(mxml_node_t *node);
node | Node to get |
---|
Real value or 0.0
0.0 is returned if the node (or its first child) is not a real value node.
Get the current reference (use) count for a node.
size_t mxmlGetRefCount(mxml_node_t *node);
node | Node |
---|
Reference count
The initial reference count of new nodes is 1. Use the mxmlRetain
and mxmlRelease
functions to increment and decrement a node's
reference count.
Get the text value for a node or its first child.
const char *mxmlGetText(mxml_node_t *node, bool *whitespace);
node | Node to get |
---|---|
whitespace | true if string is preceded by whitespace, false otherwise |
Text string or NULL
NULL
is returned if the node (or its first child) is not a text node.
The "whitespace" argument can be NULL
.
Note: Text nodes consist of whitespace-delimited words. You will only get
single words of text when reading an XML file with MXML_TYPE_TEXT
nodes.
If you want the entire string between elements in the XML file, you MUST read
the XML file with MXML_TYPE_OPAQUE
nodes and get the resulting strings
using the mxmlGetOpaque
function instead.
Get the node type.
mxml_type_t mxmlGetType(mxml_node_t *node);
node | Node to get |
---|
Type of node
MXML_TYPE_IGNORE
is returned if "node" is NULL
.
Get the user data pointer for a node.
void *mxmlGetUserData(mxml_node_t *node);
node | Node to get |
---|
User data pointer
Delete an index.
void mxmlIndexDelete(mxml_index_t *ind);
ind | Index to delete |
---|
Return the next node in the index.
mxml_node_t *mxmlIndexEnum(mxml_index_t *ind);
ind | Index to enumerate |
---|
Next node or NULL
if there is none
You should call mxmlIndexReset
prior to using this function to get
the first node in the index. Nodes are returned in the sorted order of the
index.
Find the next matching node.
mxml_node_t *mxmlIndexFind(mxml_index_t *ind, const char *element, const char *value);
ind | Index to search |
---|---|
element | Element name to find, if any |
value | Attribute value, if any |
Node or NULL
if none found
You should call mxmlIndexReset
prior to using this function for
the first time with a particular set of "element" and "value"
strings. Passing NULL
for both "element" and "value" is equivalent
to calling mxmlIndexEnum
.
Get the number of nodes in an index.
size_t mxmlIndexGetCount(mxml_index_t *ind);
ind | Index of nodes |
---|
Number of nodes in index
Create a new index.
mxml_index_t *mxmlIndexNew(mxml_node_t *node, const char *element, const char *attr);
node | XML node tree |
---|---|
element | Element to index or NULL for all |
attr | Attribute to index or NULL for none |
New index
The index will contain all nodes that contain the named element and/or
attribute. If both "element" and "attr" are NULL
, then the index will
contain a sorted list of the elements in the node tree. Nodes are
sorted by element name and optionally by attribute value if the "attr"
argument is not NULL.
Reset the enumeration/find pointer in the index and return the first node in the index.
mxml_node_t *mxmlIndexReset(mxml_index_t *ind);
ind | Index to reset |
---|
First node or NULL
if there is none
This function should be called prior to using mxmlIndexEnum
or
mxmlIndexFind
for the first time.
Load a file descriptor into an XML node tree.
mxml_node_t *mxmlLoadFd(mxml_node_t *top, int fd, mxml_load_cb_t load_cb, void *load_cbdata, mxml_sax_cb_t sax_cb, void *sax_cbdata);
top | Top node |
---|---|
fd | File descriptor to read from |
load_cb | Load callback function or NULL |
load_cbdata | Load callback data |
sax_cb | SAX callback function or NULL
|
sax_cbdata | SAX callback data |
First node or NULL
if the file could not be read.
The nodes in the specified file are added to the specified top node.
If no top node is provided, the XML file MUST be well-formed with a
single parent node like ?xml for the entire file. The callback
function returns the value type that should be used for child nodes.
The constants MXML_INTEGER_CALLBACK
, MXML_OPAQUE_CALLBACK
,
MXML_REAL_CALLBACK
, and MXML_TEXT_CALLBACK
are defined for
loading child (data) nodes of the specified type.
Note: The most common programming error when using the Mini-XML library is
to load an XML file using the MXML_TEXT_CALLBACK
, which returns inline
text as a series of whitespace-delimited words, instead of using the
MXML_OPAQUE_CALLBACK
which returns the inline text as a single string
(including whitespace).
Load a file into an XML node tree.
mxml_node_t *mxmlLoadFile(mxml_node_t *top, FILE *fp, mxml_load_cb_t load_cb, void *load_cbdata, mxml_sax_cb_t sax_cb, void *sax_cbdata);
top | Top node |
---|---|
fp | File to read from |
load_cb | Load callback function or NULL |
load_cbdata | Load callback data |
sax_cb | SAX callback function or NULL
|
sax_cbdata | SAX callback data |
First node or NULL
if the file could not be read.
The nodes in the specified file are added to the specified top node.
If no top node is provided, the XML file MUST be well-formed with a
single parent node like ?xml for the entire file. The callback
function returns the value type that should be used for child nodes.
The constants MXML_INTEGER_CALLBACK
, MXML_OPAQUE_CALLBACK
,
MXML_REAL_CALLBACK
, and MXML_TEXT_CALLBACK
are defined for
loading child (data) nodes of the specified type.
Note: The most common programming error when using the Mini-XML library is
to load an XML file using the MXML_TEXT_CALLBACK
, which returns inline
text as a series of whitespace-delimited words, instead of using the
MXML_OPAQUE_CALLBACK
which returns the inline text as a single string
(including whitespace).
Load a file into an XML node tree.
mxml_node_t *mxmlLoadFilename(mxml_node_t *top, const char *filename, mxml_load_cb_t load_cb, void *load_cbdata, mxml_sax_cb_t sax_cb, void *sax_cbdata);
top | Top node |
---|---|
filename | File to read from |
load_cb | Load callback function or NULL |
load_cbdata | Load callback data |
sax_cb | SAX callback function or NULL
|
sax_cbdata | SAX callback data |
First node or NULL
if the file could not be read.
The nodes in the specified file are added to the specified top node.
If no top node is provided, the XML file MUST be well-formed with a
single parent node like ?xml for the entire file. The callback
function returns the value type that should be used for child nodes.
The constants MXML_INTEGER_CALLBACK
, MXML_OPAQUE_CALLBACK
,
MXML_REAL_CALLBACK
, and MXML_TEXT_CALLBACK
are defined for
loading child (data) nodes of the specified type.
Note: The most common programming error when using the Mini-XML library is
to load an XML file using the MXML_TEXT_CALLBACK
, which returns inline
text as a series of whitespace-delimited words, instead of using the
MXML_OPAQUE_CALLBACK
which returns the inline text as a single string
(including whitespace).
Load an XML node tree using a read callback.
mxml_node_t *mxmlLoadIO(mxml_node_t *top, mxml_read_cb_t read_cb, void *read_cbdata, mxml_load_cb_t load_cb, void *load_cbdata, mxml_sax_cb_t sax_cb, void *sax_cbdata);
top | Top node |
---|---|
read_cb | Read callback function |
read_cbdata | Read callback data |
load_cb | Load callback function or NULL |
load_cbdata | Load callback data |
sax_cb | SAX callback function or NULL
|
sax_cbdata | SAX callback data |
First node or NULL
if the file could not be read.
The nodes in the specified file are added to the specified top node.
If no top node is provided, the XML file MUST be well-formed with a
single parent node like ?xml for the entire file. The callback
function returns the value type that should be used for child nodes.
The constants MXML_INTEGER_CALLBACK
, MXML_OPAQUE_CALLBACK
,
MXML_REAL_CALLBACK
, and MXML_TEXT_CALLBACK
are defined for
loading child (data) nodes of the specified type.
Note: The most common programming error when using the Mini-XML library is
to load an XML file using the MXML_TEXT_CALLBACK
, which returns inline
text as a series of whitespace-delimited words, instead of using the
MXML_OPAQUE_CALLBACK
which returns the inline text as a single string
(including whitespace).
Load a string into an XML node tree.
mxml_node_t *mxmlLoadString(mxml_node_t *top, const char *s, mxml_load_cb_t load_cb, void *load_cbdata, mxml_sax_cb_t sax_cb, void *sax_cbdata);
top | Top node |
---|---|
s | String to load |
load_cb | Load callback function or NULL |
load_cbdata | Load callback data |
sax_cb | SAX callback function or NULL
|
sax_cbdata | SAX callback data |
First node or NULL
if the string has errors.
The nodes in the specified string are added to the specified top node.
If no top node is provided, the XML string MUST be well-formed with a
single parent node like ?xml for the entire string. The callback
function returns the value type that should be used for child nodes.
The constants MXML_INTEGER_CALLBACK
, MXML_OPAQUE_CALLBACK
,
MXML_REAL_CALLBACK
, and MXML_TEXT_CALLBACK
are defined for
loading child (data) nodes of the specified type.
Note: The most common programming error when using the Mini-XML library is
to load an XML file using the MXML_TEXT_CALLBACK
, which returns inline
text as a series of whitespace-delimited words, instead of using the
MXML_OPAQUE_CALLBACK
which returns the inline text as a single string
(including whitespace).
Create a new CDATA node.
mxml_node_t *mxmlNewCDATA(mxml_node_t *parent, const char *data);
parent | Parent node or MXML_NO_PARENT |
---|---|
data | Data string |
New node
The new CDATA node is added to the end of the specified parent's child
list. The constant MXML_NO_PARENT
can be used to specify that the new
CDATA node has no parent. The data string must be nul-terminated and
is copied into the new node. CDATA nodes currently use the
MXML_TYPE_ELEMENT
type.
Create a new formatted CDATA node.
mxml_node_t *mxmlNewCDATAf(mxml_node_t *parent, const char *format, ...);
parent | Parent node or MXML_NO_PARENT |
---|---|
format | Printf-style format string |
... | Additional args as needed |
New node
The new CDATA node is added to the end of the specified parent's
child list. The constant MXML_NO_PARENT
can be used to specify that
the new opaque string node has no parent. The format string must be
nul-terminated and is formatted into the new node.
Create a new comment node.
mxml_node_t *mxmlNewComment(mxml_node_t *parent, const char *comment);
parent | Parent node or MXML_NO_PARENT |
---|---|
comment | Comment string |
New node
The new comment node is added to the end of the specified parent's child
list. The constant MXML_NO_PARENT
can be used to specify that the new
comment node has no parent. The comment string must be nul-terminated and
is copied into the new node.
Create a new formatted comment string node.
mxml_node_t *mxmlNewCommentf(mxml_node_t *parent, const char *format, ...);
parent | Parent node or MXML_NO_PARENT |
---|---|
format | Printf-style format string |
... | Additional args as needed |
New node
The new comment string node is added to the end of the specified parent's
child list. The constant MXML_NO_PARENT
can be used to specify that
the new opaque string node has no parent. The format string must be
nul-terminated and is formatted into the new node.
Create a new custom data node.
mxml_node_t *mxmlNewCustom(mxml_node_t *parent, void *data, mxml_custom_destroy_cb_t destroy);
parent | Parent node or MXML_NO_PARENT |
---|---|
data | Pointer to data |
destroy | Function to destroy data |
New node
The new custom node is added to the end of the specified parent's child
list. The constant MXML_NO_PARENT
can be used to specify that the new
element node has no parent. NULL
can be passed when the data in the
node is not dynamically allocated or is separately managed.
Create a new declaraction node.
mxml_node_t *mxmlNewDeclaration(mxml_node_t *parent, const char *declaration);
parent | Parent node or MXML_NO_PARENT |
---|---|
declaration | Declaration string |
New node
The new declaration node is added to the end of the specified parent's child
list. The constant MXML_NO_PARENT
can be used to specify that the new
declaration node has no parent. The declaration string must be nul-
terminated and is copied into the new node.
Create a new formatted declaration node.
mxml_node_t *mxmlNewDeclarationf(mxml_node_t *parent, const char *format, ...);
parent | Parent node or MXML_NO_PARENT |
---|---|
format | Printf-style format string |
... | Additional args as needed |
New node
The new declaration node is added to the end of the specified parent's
child list. The constant MXML_NO_PARENT
can be used to specify that
the new opaque string node has no parent. The format string must be
nul-terminated and is formatted into the new node.
Create a new processing instruction node.
mxml_node_t *mxmlNewDirective(mxml_node_t *parent, const char *directive);
parent | Parent node or MXML_NO_PARENT |
---|---|
directive | Directive string |
New node
The new processing instruction node is added to the end of the specified
parent's child list. The constant MXML_NO_PARENT
can be used to specify
that the new processing instruction node has no parent. The data string must
be nul-terminated and is copied into the new node.
Create a new formatted processing instruction node.
mxml_node_t *mxmlNewDirectivef(mxml_node_t *parent, const char *format, ...);
parent | Parent node or MXML_NO_PARENT |
---|---|
format | Printf-style format string |
... | Additional args as needed |
New node
The new processing instruction node is added to the end of the specified parent's
child list. The constant MXML_NO_PARENT
can be used to specify that
the new opaque string node has no parent. The format string must be
nul-terminated and is formatted into the new node.
Create a new element node.
mxml_node_t *mxmlNewElement(mxml_node_t *parent, const char *name);
parent | Parent node or MXML_NO_PARENT |
---|---|
name | Name of element |
New node
The new element node is added to the end of the specified parent's child
list. The constant MXML_NO_PARENT
can be used to specify that the new
element node has no parent.
Create a new integer node.
mxml_node_t *mxmlNewInteger(mxml_node_t *parent, long integer);
parent | Parent node or MXML_NO_PARENT |
---|---|
integer | Integer value |
New node
The new integer node is added to the end of the specified parent's child
list. The constant MXML_NO_PARENT
can be used to specify that the new
integer node has no parent.
Create a new opaque string.
mxml_node_t *mxmlNewOpaque(mxml_node_t *parent, const char *opaque);
parent | Parent node or MXML_NO_PARENT |
---|---|
opaque | Opaque string |
New node
The new opaque string node is added to the end of the specified parent's
child list. The constant MXML_NO_PARENT
can be used to specify that
the new opaque string node has no parent. The opaque string must be nul-
terminated and is copied into the new node.
Create a new formatted opaque string node.
mxml_node_t *mxmlNewOpaquef(mxml_node_t *parent, const char *format, ...);
parent | Parent node or MXML_NO_PARENT |
---|---|
format | Printf-style format string |
... | Additional args as needed |
New node
The new opaque string node is added to the end of the specified parent's
child list. The constant MXML_NO_PARENT
can be used to specify that
the new opaque string node has no parent. The format string must be
nul-terminated and is formatted into the new node.
Create a new real number node.
mxml_node_t *mxmlNewReal(mxml_node_t *parent, double real);
parent | Parent node or MXML_NO_PARENT |
---|---|
real | Real number value |
New node
The new real number node is added to the end of the specified parent's
child list. The constant MXML_NO_PARENT
can be used to specify that
the new real number node has no parent.
Create a new text fragment node.
mxml_node_t *mxmlNewText(mxml_node_t *parent, bool whitespace, const char *string);
parent | Parent node or MXML_NO_PARENT |
---|---|
whitespace | true = leading whitespace, false = no whitespace |
string | String |
New node
The new text node is added to the end of the specified parent's child
list. The constant MXML_NO_PARENT
can be used to specify that the new
text node has no parent. The whitespace parameter is used to specify
whether leading whitespace is present before the node. The text
string must be nul-terminated and is copied into the new node.
Create a new formatted text fragment node.
mxml_node_t *mxmlNewTextf(mxml_node_t *parent, bool whitespace, const char *format, ...);
parent | Parent node or MXML_NO_PARENT |
---|---|
whitespace | true = leading whitespace, false = no whitespace |
format | Printf-style format string |
... | Additional args as needed |
New node
The new text node is added to the end of the specified parent's child
list. The constant MXML_NO_PARENT
can be used to specify that the new
text node has no parent. The whitespace parameter is used to specify
whether leading whitespace is present before the node. The format
string must be nul-terminated and is formatted into the new node.
Create a new XML document tree.
mxml_node_t *mxmlNewXML(const char *version);
version | Version number to use |
---|
New ?xml node
The "version" argument specifies the version number to put in the
?xml directive node. If NULL
, version "1.0" is assumed.
Release a node.
int mxmlRelease(mxml_node_t *node);
node | Node |
---|
New reference count
When the reference count reaches zero, the node (and any children)
is deleted via mxmlDelete
.
Remove a node from its parent.
void mxmlRemove(mxml_node_t *node);
node | Node to remove |
---|
This function does not free memory used by the node - use mxmlDelete
for that. This function does nothing if the node has no parent.
Retain a node.
int mxmlRetain(mxml_node_t *node);
node | Node |
---|
New reference count
Save an XML tree to an allocated string.
char *mxmlSaveAllocString(mxml_node_t *node, mxml_save_cb_t save_cb, void *save_cbdata);
node | Node to write |
---|---|
save_cb | Whitespace callback function |
save_cbdata | Whitespace callback data |
Allocated string or NULL
This function returns a pointer to a string containing the textual
representation of the XML node tree. The string should be freed
using free()
when you are done with it. NULL
is returned if the node
would produce an empty string or if the string cannot be allocated.
The callback argument specifies a function that returns a whitespace
string or NULL
before and after each element. If MXML_NO_CALLBACK
is specified, whitespace will only be added before MXML_TYPE_TEXT
nodes
with leading whitespace and before attribute names inside opening
element tags.
Save an XML tree to a file descriptor.
bool mxmlSaveFd(mxml_node_t *node, int fd, mxml_save_cb_t save_cb, void *save_cbdata);
node | Node to write |
---|---|
fd | File descriptor to write to |
save_cb | Whitespace callback function |
save_cbdata | Whitespace callback data |
true
on success, false
on error.
The callback argument specifies a function that returns a whitespace
string or NULL before and after each element. If MXML_NO_CALLBACK
is specified, whitespace will only be added before MXML_TYPE_TEXT
nodes
with leading whitespace and before attribute names inside opening
element tags.
Save an XML tree to a file.
bool mxmlSaveFile(mxml_node_t *node, FILE *fp, mxml_save_cb_t save_cb, void *save_cbdata);
node | Node to write |
---|---|
fp | File to write to |
save_cb | Whitespace callback function |
save_cbdata | Whitespace callback data |
true
on success, false
on error.
The callback argument specifies a function that returns a whitespace
string or NULL before and after each element. If MXML_NO_CALLBACK
is specified, whitespace will only be added before MXML_TYPE_TEXT
nodes
with leading whitespace and before attribute names inside opening
element tags.
Save an XML tree to a file.
bool mxmlSaveFilename(mxml_node_t *node, const char *filename, mxml_save_cb_t save_cb, void *save_cbdata);
node | Node to write |
---|---|
filename | File to write to |
save_cb | Whitespace callback function |
save_cbdata | Whitespace callback data |
true
on success, false
on error.
The callback argument specifies a function that returns a whitespace
string or NULL before and after each element. If MXML_NO_CALLBACK
is specified, whitespace will only be added before MXML_TYPE_TEXT
nodes
with leading whitespace and before attribute names inside opening
element tags.
Save an XML tree using a callback.
bool mxmlSaveIO(mxml_node_t *node, mxml_write_cb_t write_cb, void *write_cbdata, mxml_save_cb_t save_cb, void *save_cbdata);
node | Node to write |
---|---|
write_cb | Write callback function |
write_cbdata | Write callback data |
save_cb | Whitespace callback function |
save_cbdata | Whitespace callback data |
true
on success, false
on error.
The callback argument specifies a function that returns a whitespace
string or NULL before and after each element. If MXML_NO_CALLBACK
is specified, whitespace will only be added before MXML_TYPE_TEXT
nodes
with leading whitespace and before attribute names inside opening
element tags.
Save an XML node tree to a string.
size_t mxmlSaveString(mxml_node_t *node, char *buffer, size_t bufsize, mxml_save_cb_t save_cb, void *save_cbdata);
node | Node to write |
---|---|
buffer | String buffer |
bufsize | Size of string buffer |
save_cb | Whitespace callback function |
save_cbdata | Whitespace callback function |
Size of string
This function returns the total number of bytes that would be
required for the string but only copies (bufsize - 1) characters
into the specified buffer.
The callback argument specifies a function that returns a whitespace
string or NULL before and after each element. If MXML_NO_CALLBACK
is specified, whitespace will only be added before MXML_TYPE_TEXT
nodes
with leading whitespace and before attribute names inside opening
element tags.
Set the data for a CDATA node.
bool mxmlSetCDATA(mxml_node_t *node, const char *data);
node | Node to set |
---|---|
data | New data string |
true
on success, false
on failure
The node is not changed if it (or its first child) is not a CDATA node.
Set the data for a CDATA to a formatted string.
bool mxmlSetCDATAf(mxml_node_t *node, const char *format, ...);
node | Node |
---|---|
format | printf -style format string |
... | Additional arguments as needed |
true
on success, false
on failure
Set a comment to a literal string.
bool mxmlSetComment(mxml_node_t *node, const char *comment);
node | Node |
---|---|
comment | Literal string |
true
on success, false
on failure
Set a comment to a formatted string.
bool mxmlSetCommentf(mxml_node_t *node, const char *format, ...);
node | Node |
---|---|
format | printf -style format string |
... | Additional arguments as needed |
true
on success, false
on failure
Set the data and destructor of a custom data node.
bool mxmlSetCustom(mxml_node_t *node, void *data, mxml_custom_destroy_cb_t destroy);
node | Node to set |
---|---|
data | New data pointer |
destroy | New destructor function |
true
on success, false
on failure
The node is not changed if it (or its first child) is not a custom node.
Set a comment to a literal string.
bool mxmlSetDeclaration(mxml_node_t *node, const char *declaration);
node | Node |
---|---|
declaration | Literal string |
true
on success, false
on failure
Set a comment to a formatted string.
bool mxmlSetDeclarationf(mxml_node_t *node, const char *format, ...);
node | Node |
---|---|
format | printf -style format string |
... | Additional arguments as needed |
true
on success, false
on failure
Set a directive to a literal string.
bool mxmlSetDirective(mxml_node_t *node, const char *directive);
node | Node |
---|---|
directive | Literal string |
true
on success, false
on failure
Set a directive to a formatted string.
bool mxmlSetDirectivef(mxml_node_t *node, const char *format, ...);
node | Node |
---|---|
format | printf -style format string |
... | Additional arguments as needed |
true
on success, false
on failure
Set the name of an element node.
bool mxmlSetElement(mxml_node_t *node, const char *name);
node | Node to set |
---|---|
name | New name string |
true
on success, false
on failure
The node is not changed if it is not an element node.
Set the value of an integer node.
bool mxmlSetInteger(mxml_node_t *node, long integer);
node | Node to set |
---|---|
integer | Integer value |
true
on success, false
on failure
The node is not changed if it (or its first child) is not an integer node.
Set the value of an opaque node.
bool mxmlSetOpaque(mxml_node_t *node, const char *opaque);
node | Node to set |
---|---|
opaque | Opaque string |
true
on success, false
on failure
The node is not changed if it (or its first child) is not an opaque node.
Set the value of an opaque string node to a formatted string.
bool mxmlSetOpaquef(mxml_node_t *node, const char *format, ...);
node | Node to set |
---|---|
format | Printf-style format string |
... | Additional arguments as needed |
true
on success, false
on failure
The node is not changed if it (or its first child) is not an opaque node.
Set the value of a real number node.
bool mxmlSetReal(mxml_node_t *node, double real);
node | Node to set |
---|---|
real | Real number value |
true
on success, false
on failure
The node is not changed if it (or its first child) is not a real number node.
Set the value of a text node.
bool mxmlSetText(mxml_node_t *node, bool whitespace, const char *string);
node | Node to set |
---|---|
whitespace | true = leading whitespace, false = no whitespace |
string | String |
true
on success, false
on failure
The node is not changed if it (or its first child) is not a text node.
Set the value of a text node to a formatted string.
bool mxmlSetTextf(mxml_node_t *node, bool whitespace, const char *format, ...);
node | Node to set |
---|---|
whitespace | true = leading whitespace, false = no whitespace |
format | Printf-style format string |
... | Additional arguments as needed |
true
on success, false
on failure
The node is not changed if it (or its first child) is not a text node.
Set the user data pointer for a node.
bool mxmlSetUserData(mxml_node_t *node, void *data);
node | Node to set |
---|---|
data | User data pointer |
true
on success, false
on failure
Set the wrap margin when saving XML data.
void mxmlSetWrapMargin(int column);
column | Column for wrapping, 0 to disable wrapping |
---|
Wrapping is disabled when "column" is 0.
Walk to the next logical node in the tree.
mxml_node_t *mxmlWalkNext(mxml_node_t *node, mxml_node_t *top, mxml_descend_t descend);
node | Current node |
---|---|
top | Top node |
descend | Descend into tree - MXML_DESCEND_ALL , MXML_DESCEND_NONE , or MXML_DESCEND_FIRST |
Next node or NULL
The descend argument controls whether the first child is considered to be the next node. The top node argument constrains the walk to the node's children.
Walk to the previous logical node in the tree.
mxml_node_t *mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top, mxml_descend_t descend);
node | Current node |
---|---|
top | Top node |
descend | Descend into tree - MXML_DESCEND_ALL , MXML_DESCEND_NONE , or MXML_DESCEND_FIRST |
Previous node or NULL
The descend argument controls whether the previous node's last child is considered to be the previous node. The top node argument constrains the walk to the node's children.
mxmlAdd
add values
typedef enum mxml_add_e mxml_add_t;
Custom data destructor
typedef void (*mxml_custom_destroy_cb_t)(void *);
Custom data load callback function
typedef bool (*mxml_custom_load_cb_t)(void *cbdata mxml_node_t *node const char *s);
Custom data save callback function
typedef char *(*mxml_custom_save_cb_t)(void *cbdata mxml_node_t *node);
mxmlFindElement
, mxmlWalkNext
, and mxmlWalkPrev
descend values
typedef enum mxml_descend_e mxml_descend_t;
Entity callback function
typedef int (*mxml_entity_cb_t)(void *cbdata const char *name);
Error callback function
typedef void (*mxml_error_cb_t)(void *cbdata const char *message);
An XML node index.
typedef struct _mxml_index_s mxml_index_t;
Load callback function
typedef mxml_type_t (*mxml_load_cb_t)(void *cbdata mxml_node_t *node);
An XML node.
typedef struct _mxml_node_s mxml_node_t;
Read callback function
typedef ssize_t (*mxml_read_cb_t)(void *cbdata void *buffer size_t bytes);
Save callback function
typedef const char *(*mxml_save_cb_t)(void *cbdata mxml_node_t *node mxml_ws_t when);
SAX callback function
typedef bool (*mxml_sax_cb_t)(void *cbdata mxml_node_t *node mxml_sax_event_t event);
SAX event type.
typedef enum mxml_sax_event_e mxml_sax_event_t;
String copy/allocation callback
typedef char *(*mxml_strcopy_cb_t)(void *cbdata const char *s);
String free callback
typedef void (*mxml_strfree_cb_t)(void *cbdata char *s);
The XML node type.
typedef enum mxml_type_e mxml_type_t;
Write callback function
typedef ssize_t (*mxml_write_cb_t)(void *cbdata const void *buffer size_t bytes);
Whitespace periods
typedef enum mxml_ws_e mxml_ws_t;
mxmlAdd
add values
MXML_ADD_AFTER | Add node after specified node |
---|---|
MXML_ADD_BEFORE | Add node before specified node |
mxmlFindElement
, mxmlWalkNext
, and mxmlWalkPrev
descend values
MXML_DESCEND_ALL | Descend when finding/walking |
---|---|
MXML_DESCEND_FIRST | Descend for first find |
MXML_DESCEND_NONE | Don't descend when finding/walking |
SAX event type.
MXML_SAX_EVENT_CDATA | CDATA node |
---|---|
MXML_SAX_EVENT_COMMENT | Comment node |
MXML_SAX_EVENT_DATA | Data node |
MXML_SAX_EVENT_DECLARATION | Declaration node |
MXML_SAX_EVENT_DIRECTIVE | Processing instruction node |
MXML_SAX_EVENT_ELEMENT_CLOSE | Element closed |
MXML_SAX_EVENT_ELEMENT_OPEN | Element opened |
The XML node type.
MXML_TYPE_CDATA | CDATA value ("[CDATA[...]]") |
---|---|
MXML_TYPE_COMMENT | Comment ("!--...--") |
MXML_TYPE_CUSTOM | Custom data |
MXML_TYPE_DECLARATION | Declaration ("!...") |
MXML_TYPE_DIRECTIVE | Processing instruction ("?...?") |
MXML_TYPE_ELEMENT | XML element with attributes |
MXML_TYPE_IGNORE | Ignore/throw away node |
MXML_TYPE_INTEGER | Integer value |
MXML_TYPE_OPAQUE | Opaque string |
MXML_TYPE_REAL | Real value |
MXML_TYPE_TEXT | Text fragment |
Whitespace periods
MXML_WS_AFTER_CLOSE | Callback for after close tag |
---|---|
MXML_WS_AFTER_OPEN | Callback for after open tag |
MXML_WS_BEFORE_CLOSE | Callback for before close tag |
MXML_WS_BEFORE_OPEN | Callback for before open tag |