mirror of
https://github.com/michaelrsweet/mxml.git
synced 2024-11-14 15:55:30 +00:00
Update docos some more.
This commit is contained in:
parent
a9aea16f3a
commit
c1d787dc5f
@ -26,7 +26,8 @@ Mini-XML provides the following functionality:
|
||||
- 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"), and text data types in "leaf" nodes.
|
||||
- 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.
|
||||
|
||||
|
241
doc/body.md
241
doc/body.md
@ -19,8 +19,8 @@ Mini-XML provides the following functionality:
|
||||
- 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"), and text data types in "leaf"
|
||||
nodes.
|
||||
- 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.
|
||||
@ -299,12 +299,12 @@ mxmlGetUserData(mxml_node_t *node);
|
||||
Creating XML Documents
|
||||
----------------------
|
||||
|
||||
You can create and update XML documents in memory using the various `mxmlNew`
|
||||
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:
|
||||
|
||||
```c
|
||||
mxml_node_t *xml; /* <?xml ... ?> */
|
||||
mxml_node_t *xml; /* <?xml version="1.0"?> */
|
||||
mxml_node_t *data; /* <data> */
|
||||
mxml_node_t *node; /* <node> */
|
||||
mxml_node_t *group; /* <group> */
|
||||
@ -336,25 +336,25 @@ data = mxmlNewElement(xml, "data");
|
||||
```
|
||||
|
||||
We start by creating the declaration node common to all XML files using the
|
||||
`mxmlNewXML` function:
|
||||
[mxmlNewXML](@@) function:
|
||||
|
||||
```c
|
||||
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
|
||||
[mxmlNewElement](@@) function. The first argument specifies the parent node
|
||||
\(`xml`) while the second specifies the element name \(`data`):
|
||||
|
||||
```c
|
||||
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 - 0 or false in this case. The last argument specifies the
|
||||
actual text to add:
|
||||
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:
|
||||
|
||||
```c
|
||||
node = mxmlNewElement(data, "node");
|
||||
@ -368,47 +368,51 @@ one loaded from disk or a string.
|
||||
Saving an XML File
|
||||
------------------
|
||||
|
||||
You save an XML file using the `mxmlSaveFile` function:
|
||||
You save an XML file using the [mxmlSaveFilename](@@) function:
|
||||
|
||||
```c
|
||||
bool
|
||||
mxmlSaveFile(mxml_node_t *node, FILE *fp,
|
||||
mxml_save_cb_t cb);
|
||||
mxmlSaveFilename(mxml_node_t *node, const char *filename,
|
||||
mxml_save_cb_t cb, void *cbdata);
|
||||
```
|
||||
|
||||
The `cb` argument specifies a function that returns the whitespace (if any) that
|
||||
is inserted before and after each element node. The `MXML_NO_CALLBACK` constant
|
||||
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:
|
||||
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:
|
||||
|
||||
```c
|
||||
FILE *fp;
|
||||
|
||||
fp = fopen("filename.xml", "w");
|
||||
mxmlSaveFile(xml, fp, MXML_NO_CALLBACK);
|
||||
fclose(fp);
|
||||
mxmlSaveFile(xml, "filename.xml", /*cb*/NULL, /*cbdata*/NULL);
|
||||
```
|
||||
|
||||
Mini-XML also provides functions to save to a file descriptor or strings:
|
||||
Mini-XML also provides functions to save to a file descriptor, `FILE` pointer,
|
||||
or strings:
|
||||
|
||||
```c
|
||||
char *
|
||||
mxmlSaveAllocString(mxml_node_t *node, mxml_save_cb_t cb);
|
||||
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);
|
||||
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);
|
||||
mxml_save_cb_t cb, void *cbdata);
|
||||
```
|
||||
|
||||
|
||||
### Controlling Line Wrapping
|
||||
|
||||
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:
|
||||
that the text is readable in terminal windows. The [mxmlSetWrapMargin](@@)
|
||||
function overrides the default wrap margin for the current thread:
|
||||
|
||||
```c
|
||||
void mxmlSetWrapMargin(int column);
|
||||
@ -429,20 +433,19 @@ mxmlSetWrapMargin(0);
|
||||
|
||||
### Save Callbacks
|
||||
|
||||
The last argument to the `mxmlSave` functions is a callback function 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 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:
|
||||
|
||||
```c
|
||||
const char *
|
||||
whitespace_cb(mxml_node_t *node, int where)
|
||||
whitespace_cb(void *cbdata, mxml_node_t *node, mxml_ws_t where)
|
||||
{
|
||||
const char *element;
|
||||
|
||||
@ -514,7 +517,7 @@ FILE *fp;
|
||||
mxml_node_t *tree;
|
||||
|
||||
fp = fopen("filename.xml", "w");
|
||||
mxmlSaveFile(tree, fp, whitespace_cb);
|
||||
mxmlSaveFile(tree, fp, whitespace_cb, /*cbdata*/NULL);
|
||||
fclose(fp);
|
||||
```
|
||||
|
||||
@ -522,20 +525,19 @@ fclose(fp);
|
||||
Memory Management
|
||||
-----------------
|
||||
|
||||
Once you are done with the XML data, use the `mxmlDelete` function to
|
||||
recursively free the memory that is used for a particular node or the entire
|
||||
tree:
|
||||
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:
|
||||
|
||||
```c
|
||||
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` automatically
|
||||
calls `mxmlDelete` to actually free the memory used by the node tree. New nodes
|
||||
start with a use count of 1.
|
||||
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`.
|
||||
|
||||
|
||||
More About Nodes
|
||||
@ -544,17 +546,17 @@ More About Nodes
|
||||
Element Nodes
|
||||
-------------
|
||||
|
||||
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 `mxmlElementDeleteAttr`
|
||||
function:
|
||||
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:
|
||||
|
||||
```c
|
||||
mxml_node_t *
|
||||
mxmlNewElement(mxml_node_t *parent, const char *name);
|
||||
|
||||
void
|
||||
mxmlElementDeleteAttr(mxml_node_t *node, const char *name);
|
||||
mxmlElementClearAttr(mxml_node_t *node, const char *name);
|
||||
|
||||
void
|
||||
mxmlElementSetAttr(mxml_node_t *node, const char *name,
|
||||
@ -565,18 +567,10 @@ mxmlElementSetAttrf(mxml_node_t *node, const char *name,
|
||||
const char *format, ...);
|
||||
```
|
||||
|
||||
Child nodes are added using the various `mxmlNew` functions. The top (root)
|
||||
node must be an element, usually created by the `mxmlNewXML` function:
|
||||
|
||||
```c
|
||||
mxml_node_t *
|
||||
mxmlNewXML(const char *version);
|
||||
```
|
||||
|
||||
The `mxmlGetElement` function retrieves the element name, the
|
||||
`mxmlElementGetAttr` function retrieves the value string for a named attribute
|
||||
associated with the element. The `mxmlElementGetAttrByIndex` and
|
||||
`mxmlElementGetAttrCount` functions retrieve attributes by index:
|
||||
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:
|
||||
|
||||
```c
|
||||
const char *
|
||||
@ -597,27 +591,39 @@ mxmlElementGetAttrCount(mxml_node_t *node);
|
||||
CDATA Nodes
|
||||
-----------
|
||||
|
||||
CDATA \(`MXML_TYPE_CDATA`) nodes are created using the `mxmlNewCDATA` function:
|
||||
CDATA \(`MXML_TYPE_CDATA`) nodes are created using the [mxmlNewCDATA](@@)
|
||||
and [mxmlNewCDATAf](@@) functions:
|
||||
|
||||
mxml_node_t *mxmlNewCDATA(mxml_node_t *parent, const char *string);
|
||||
```c
|
||||
mxml_node_t *
|
||||
mxmlNewCDATA(mxml_node_t *parent, const char *string);
|
||||
|
||||
The `mxmlGetCDATA` function retrieves the CDATA string pointer for a node:
|
||||
mxml_node_t *
|
||||
mxmlNewCDATAf(mxml_node_t *parent, const char *format, ...);
|
||||
```
|
||||
|
||||
const char *mxmlGetCDATA(mxml_node_t *node);
|
||||
The [mxmlGetCDATA](@@) function retrieves the CDATA string pointer for a node:
|
||||
|
||||
```c
|
||||
const char *
|
||||
mxmlGetCDATA(mxml_node_t *node);
|
||||
```
|
||||
|
||||
|
||||
Comment Nodes
|
||||
-------------
|
||||
|
||||
Comment \(`MXML_TYPE_COMMENT`) nodes are created using the `mxmlNewComment`
|
||||
function, for example:
|
||||
Comment \(`MXML_TYPE_COMMENT`) nodes are created using the [mxmlNewComment](@@)
|
||||
and [mxmlNewCommentf](@@) functions, for example:
|
||||
|
||||
```c
|
||||
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:
|
||||
Similarly, the [mxmlGetComment](@@) function retrieves the comment string
|
||||
pointer for a node:
|
||||
|
||||
```c
|
||||
const char *comment = mxmlGetComment(node);
|
||||
@ -629,25 +635,35 @@ Processing Instruction Nodes
|
||||
----------------------------
|
||||
|
||||
Processing instruction \(`MXML_TYPE_DIRECTIVE`) nodes are created using the
|
||||
`mxmlNewDirective` function:
|
||||
[mxmlNewDirective](@@) and [mxmlNewDirectivef](@@) functions:
|
||||
|
||||
```c
|
||||
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:
|
||||
The [mxmlGetDirective](@@) function retrieves the processing instruction string
|
||||
for a node:
|
||||
|
||||
```c
|
||||
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:
|
||||
|
||||
```c
|
||||
mxml_node_t *
|
||||
mxmlNewXML(const char *version);
|
||||
```
|
||||
|
||||
|
||||
Integer Nodes
|
||||
-------------
|
||||
|
||||
Integer \(`MXML_TYPE_INTEGER`) nodes are created using the `mxmlNewInteger`
|
||||
Integer \(`MXML_TYPE_INTEGER`) nodes are created using the [mxmlNewInteger](@@)
|
||||
function:
|
||||
|
||||
```c
|
||||
@ -655,7 +671,7 @@ mxml_node_t *
|
||||
mxmlNewInteger(mxml_node_t *parent, long integer);
|
||||
```
|
||||
|
||||
The `mxmlGetInteger` function retrieves the integer value for a node:
|
||||
The [mxmlGetInteger](@@) function retrieves the integer value for a node:
|
||||
|
||||
|
||||
```c
|
||||
@ -667,15 +683,18 @@ mxmlGetInteger(mxml_node_t *node);
|
||||
Opaque String Nodes
|
||||
-------------------
|
||||
|
||||
Opaque string \(`MXML_TYPE_OPAQUE`) nodes are created using the `mxmlNewOpaque`
|
||||
function:
|
||||
Opaque string \(`MXML_TYPE_OPAQUE`) nodes are created using the
|
||||
[mxmlNewOpaque](@@) and [mxmlNewOpaquef](@@) functions:
|
||||
|
||||
```c
|
||||
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:
|
||||
The [mxmlGetOpaque](@@) function retrieves the opaque string pointer for a node:
|
||||
|
||||
```c
|
||||
const char *
|
||||
@ -687,8 +706,8 @@ Text Nodes
|
||||
----------
|
||||
|
||||
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.
|
||||
[mxmlNewText](@@) and [mxmlNewTextf](@@) functions. Each text node consists of
|
||||
a text string and (leading) whitespace boolean value.
|
||||
|
||||
```c
|
||||
mxml_node_t *
|
||||
@ -700,7 +719,7 @@ mxmlNewTextf(mxml_node_t *parent, bool whitespace,
|
||||
const char *format, ...);
|
||||
```
|
||||
|
||||
The `mxmlGetText` function retrieves the text string pointer and whitespace
|
||||
The [mxmlGetText](@@) function retrieves the text string pointer and whitespace
|
||||
boolean value for a node:
|
||||
|
||||
```c
|
||||
@ -712,7 +731,7 @@ mxmlGetText(mxml_node_t *node, bool *whitespace);
|
||||
Real Number Nodes
|
||||
--------------------
|
||||
|
||||
Real number \(`MXML_TYPE_REAL`) nodes are created using the `mxmlNewReal`
|
||||
Real number \(`MXML_TYPE_REAL`) nodes are created using the [mxmlNewReal](@@)
|
||||
function:
|
||||
|
||||
```c
|
||||
@ -720,7 +739,7 @@ mxml_node_t *
|
||||
mxmlNewReal(mxml_node_t *parent, double real);
|
||||
```
|
||||
|
||||
The `mxmlGetReal` function retrieves the real number for a node:
|
||||
The [mxmlGetReal](@@) function retrieves the real number for a node:
|
||||
|
||||
```c
|
||||
double
|
||||
@ -738,7 +757,7 @@ documents.
|
||||
Finding Nodes
|
||||
-------------
|
||||
|
||||
The `mxmlFindPath` function finds the (first) value node under a specific
|
||||
The [mxmlFindPath](@@) function finds the (first) value node under a specific
|
||||
element using a "path":
|
||||
|
||||
```c
|
||||
@ -757,8 +776,8 @@ 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:
|
||||
The [mxmlFindElement](@@) function can be used to find a named element,
|
||||
optionally matching an attribute and value:
|
||||
|
||||
```c
|
||||
mxml_node_t *
|
||||
@ -781,7 +800,7 @@ node = mxmlFindElement(tree, tree, "a", "href", NULL,
|
||||
|
||||
/* Find the first "a" element with "href" to a URL */
|
||||
node = mxmlFindElement(tree, tree, "a", "href",
|
||||
"http://michaelrsweet.github.io/",
|
||||
"http://msweet.org/",
|
||||
MXML_DESCEND_ALL);
|
||||
|
||||
/* Find the first element with a "src" attribute*/
|
||||
@ -827,10 +846,10 @@ three constants:
|
||||
Iterating 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:
|
||||
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:
|
||||
|
||||
```c
|
||||
mxml_node_t *
|
||||
@ -886,8 +905,8 @@ val8
|
||||
Indexing
|
||||
--------
|
||||
|
||||
The `mxmlIndexNew` function allows you to create an index of nodes for faster
|
||||
searching and enumeration:
|
||||
The [mxmlIndexNew](@@) function allows you to create an index of nodes for
|
||||
faster searching and enumeration:
|
||||
|
||||
```c
|
||||
mxml_index_t *
|
||||
@ -908,7 +927,7 @@ document:
|
||||
mxml_index_t *ind = mxmlIndexNew(xml, NULL, "id");
|
||||
```
|
||||
|
||||
Once the index is created, the `mxmlIndexFind` function can be used to find a
|
||||
Once the index is created, the [mxmlIndexFind](@@) function can be used to find a
|
||||
matching node:
|
||||
|
||||
```c
|
||||
@ -923,7 +942,7 @@ 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
|
||||
Alternately, the [mxmlIndexReset](@@) and [mxmlIndexEnum](@@) functions can be used to
|
||||
enumerate the nodes in the index:
|
||||
|
||||
```c
|
||||
@ -947,14 +966,14 @@ for (node = mxmlIndexReset(ind);
|
||||
}
|
||||
```
|
||||
|
||||
The `mxmlIndexCount` function returns the number of nodes in the index:
|
||||
The [mxmlIndexCount](@@) function returns the number of nodes in the index:
|
||||
|
||||
```c
|
||||
size_t
|
||||
mxmlIndexGetCount(mxml_index_t *ind);
|
||||
```
|
||||
|
||||
Finally, the `mxmlIndexDelete` function frees all memory associated with the
|
||||
Finally, the [mxmlIndexDelete](@@) function frees all memory associated with the
|
||||
index:
|
||||
|
||||
```c
|
||||
@ -972,16 +991,16 @@ 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.
|
||||
The [mxmlGetCustom](@@) function retrieves the custom value pointer for a node.
|
||||
|
||||
```c
|
||||
const void *
|
||||
mxmlGetCustom(mxml_node_t *node);
|
||||
```
|
||||
|
||||
Custom \(`MXML_TYPE_CUSTOM`) nodes are created using the `mxmlNewCustom`
|
||||
Custom \(`MXML_TYPE_CUSTOM`) nodes are created using the [mxmlNewCustom](@@)
|
||||
function or using a custom per-thread load callbacks specified using the
|
||||
`mxmlSetCustomHandlers` function:
|
||||
[mxmlSetCustomHandlers](@@) function:
|
||||
|
||||
```c
|
||||
typedef void (*mxml_custom_destroy_cb_t)(void *);
|
||||
@ -1126,10 +1145,11 @@ save_custom(mxml_node_t *node)
|
||||
}
|
||||
```
|
||||
|
||||
You register the callback functions using the `mxmlSetCustomHandlers` function:
|
||||
You register the callback functions using the [mxmlSetCustomCallbacks](@@)
|
||||
function:
|
||||
|
||||
```c
|
||||
mxmlSetCustomHandlers(load_custom, save_custom);
|
||||
mxmlSetCustomCallbacks(load_custom, save_custom);
|
||||
```
|
||||
|
||||
|
||||
@ -1142,8 +1162,7 @@ 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 `mxmlLoadFd`, `mxmlLoadFile`, `mxmlLoadFilename`, `mxmlLoadIO`, and
|
||||
`mxmlLoadString` functions support a SAX callback and associated data. The
|
||||
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:
|
||||
|
||||
@ -1171,8 +1190,8 @@ The event will be one of the following:
|
||||
|
||||
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:
|
||||
using the [mxmlRetain](@@) function. For example, the following SAX callback
|
||||
will retain all nodes, effectively simulating a normal in-memory load:
|
||||
|
||||
```c
|
||||
bool
|
||||
@ -1301,8 +1320,8 @@ 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.
|
||||
- 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`.
|
||||
@ -1311,8 +1330,12 @@ The following incompatible API changes were made in Mini-XML v4.0:
|
||||
- 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.
|
||||
|
@ -1,4 +1,4 @@
|
||||
.TH mxml 3 "Mini-XML API" "2024-03-07" "Mini-XML API"
|
||||
.TH mxml 3 "Mini-XML API" "2024-03-13" "Mini-XML API"
|
||||
.SH NAME
|
||||
mxml \- Mini-XML API
|
||||
.SH INCLUDE FILE
|
||||
@ -330,11 +330,11 @@ void mxmlDelete (
|
||||
.PP
|
||||
If the specified node has a parent, this function first removes the
|
||||
node from its parent using the \fImxmlRemove\fR function.
|
||||
.SS mxmlElementDeleteAttr
|
||||
.SS mxmlElementClearAttr
|
||||
Delete an attribute.
|
||||
.PP
|
||||
.nf
|
||||
void mxmlElementDeleteAttr (
|
||||
void mxmlElementClearAttr (
|
||||
mxml_node_t *node,
|
||||
const char *name
|
||||
);
|
||||
|
BIN
doc/mxml.epub
BIN
doc/mxml.epub
Binary file not shown.
203
doc/mxml.html
203
doc/mxml.html
@ -285,7 +285,7 @@ span.string {
|
||||
<li><a href="#FUNCTIONS">Functions</a><ul class="subcontents">
|
||||
<li><a href="#mxmlAdd">mxmlAdd</a></li>
|
||||
<li><a href="#mxmlDelete">mxmlDelete</a></li>
|
||||
<li><a href="#mxmlElementDeleteAttr">mxmlElementDeleteAttr</a></li>
|
||||
<li><a href="#mxmlElementClearAttr">mxmlElementClearAttr</a></li>
|
||||
<li><a href="#mxmlElementGetAttr">mxmlElementGetAttr</a></li>
|
||||
<li><a href="#mxmlElementGetAttrByIndex">mxmlElementGetAttrByIndex</a></li>
|
||||
<li><a href="#mxmlElementGetAttrCount">mxmlElementGetAttrCount</a></li>
|
||||
@ -414,7 +414,7 @@ span.string {
|
||||
</li>
|
||||
<li><p>Supports arbitrary element names, attributes, and attribute values with no preset limits, just available memory.</p>
|
||||
</li>
|
||||
<li><p>Supports integer, real, opaque ("CDATA"), and text data types in "leaf" nodes.</p>
|
||||
<li><p>Supports integer, real, opaque ("CDATA"), text, and custom data types in "leaf" nodes.</p>
|
||||
</li>
|
||||
<li><p>Functions for creating and managing trees of data.</p>
|
||||
</li>
|
||||
@ -431,10 +431,11 @@ span.string {
|
||||
<blockquote>
|
||||
<p>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.</p>
|
||||
</blockquote>
|
||||
<p>I took my own challenge and coded furiously for two days to produced the initial public release of Mini-XML, total lines of code: 696. Robert promptly integrated Mini-XML into Gutenprint and removed libxml2.</p>
|
||||
<p>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,839 lines of code, compared to 175,808 lines of code for libxml2 version 2.11.7.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<h3 class="title" id="resources">Resources</h3>
|
||||
<p>The Mini-XML home page can be found at <a href="https://www.msweet.org/mxml">https://www.msweet.org/mxml</a>. From there you can download the current version of Mini-XML, access the issue tracker, and find other resources.</p>
|
||||
<p>Mini-XML v4 has a slightly different API than prior releases. See the <a href="#migrating-from-mini-xml-v3.x">Migrating from Mini-XML v3.x</a> chapter for details.</p>
|
||||
<h3 class="title" id="legal-stuff">Legal Stuff</h3>
|
||||
<p>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.</p>
|
||||
<h2 class="title" id="using-mini-xml">Using Mini-XML</h2>
|
||||
@ -447,31 +448,32 @@ span.string {
|
||||
<p>If you have the <code>pkg-config</code> software installed, you can use it to determine the proper compiler and linker options for your installation:</p>
|
||||
<pre><code>gcc `pkg-config --cflags mxml4` -o myprogram myprogram.c `pkg-config --libs mxml4`
|
||||
</code></pre>
|
||||
<blockquote>
|
||||
<p>Note: The library name "mxml4" is a configure-time option. If you use the <code>--disable-libmxml4-prefix</code> configure option the library is named "mxml".</p>
|
||||
</blockquote>
|
||||
<h3 class="title" id="loading-an-xml-file">Loading an XML File</h3>
|
||||
<p>You load an XML file using the <code>mxmlLoadFile</code> function:</p>
|
||||
<p>You load an XML file using the <a href="#mxmlLoadFile">mxmlLoadFile</a> function:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlLoadFile(mxml_node_t *top, FILE *fp,
|
||||
mxmlLoadFilename(mxml_node_t *top, <span class="reserved">const</span> <span class="reserved">char</span> *filename,
|
||||
mxml_load_cb_t load_cb, <span class="reserved">void</span> *load_cbdata,
|
||||
mxml_sax_cb_t sax_cb, <span class="reserved">void</span> *sax_cbdata);
|
||||
</code></pre>
|
||||
<p>The <code>load_cb</code> argument specifies a function that assigns child (value) node types for each element in the document. The default callback (<code>NULL</code>) supports passing a pointer to an <code>mxml_type_t</code> variable containing the type of value nodes. For example, to load the XML file "filename.xml" containing literal strings you can use:</p>
|
||||
<pre><code class="language-c">FILE *fp;
|
||||
mxml_node_t *tree;
|
||||
<pre><code class="language-c">mxml_node_t *tree;
|
||||
mxml_type_t type = MXML_TYPE_OPAQUE;
|
||||
|
||||
fp = fopen(<span class="string">"filename.xml"</span>, <span class="string">"r"</span>);
|
||||
tree = mxmlLoadFile(<span class="comment">/*top*/</span>NULL, fp, <span class="comment">/*load_cb*/</span>NULL, &type,
|
||||
tree = mxmlLoadFilename(<span class="comment">/*top*/</span>NULL, <span class="string">"filename.xml"</span>,
|
||||
<span class="comment">/*load_cb*/</span>NULL, <span class="comment">/*load_cbdata*/</span>&type,
|
||||
<span class="comment">/*sax_cb*/</span>NULL, <span class="comment">/*sax_cbdata*/</span>NULL);
|
||||
fclose(fp);
|
||||
</code></pre>
|
||||
<p>Mini-XML also provides functions to load from a named file, a file descriptor, or string:</p>
|
||||
<p>Mini-XML also provides functions to load from a <code>FILE</code> pointer, a file descriptor, or string:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlLoadFd(mxml_node_t *top, <span class="reserved">int</span> fd,
|
||||
mxml_load_cb_t load_cb, <span class="reserved">void</span> *load_cbdata,
|
||||
mxml_sax_cb_t sax_cb, <span class="reserved">void</span> *sax_cbdata);
|
||||
|
||||
mxml_node_t *
|
||||
mxmlLoadFilename(mxml_node_t *top, <span class="reserved">const</span> <span class="reserved">char</span> *filename,
|
||||
mxmlLoadFile(mxml_node_t *top, FILE *fp,
|
||||
mxml_load_cb_t load_cb, <span class="reserved">void</span> *load_cbdata,
|
||||
mxml_sax_cb_t sax_cb, <span class="reserved">void</span> *sax_cbdata);
|
||||
|
||||
@ -481,11 +483,11 @@ mxmlLoadString(mxml_node_t *top, <span class="reserved">const</span> <span class
|
||||
mxml_sax_cb_t sax_cb, <span class="reserved">void</span> *sax_cbdata);
|
||||
</code></pre>
|
||||
<h4 id="load-callbacks">Load Callbacks</h4>
|
||||
<p>The <code>load_xxx</code> arguments to the <code>mxmlLoad</code> 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 (<code>NULL</code>) callback expects the <code>load_cbdata</code> argument to be a pointer to a <code>mxml_type_t</code> variable - if <code>NULL</code> it returns the <code>MXML_TYPE_TEXT</code> type.</p>
|
||||
<p>You can provide your own callback functions 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: <code>MXML_TYPE_CUSTOM</code>, <code>MXML_TYPE_INTEGER</code>, <code>MXML_TYPE_OPAQUE</code>, <code>MXML_TYPE_REAL</code>, or <code>MXML_TYPE_TEXT</code>. The function is called <em>after</em> 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.</p>
|
||||
<p>The <code>load_xxx</code> 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 (<code>NULL</code>) callback expects the <code>load_cbdata</code> argument to be a pointer to a <code>mxml_type_t</code> variable that contains the desired value node type - if <code>NULL</code>, it uses the <code>MXML_TYPE_TEXT</code> (whitespace-separated text) type.</p>
|
||||
<p>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: <code>MXML_TYPE_CUSTOM</code>, <code>MXML_TYPE_INTEGER</code>, <code>MXML_TYPE_OPAQUE</code>, <code>MXML_TYPE_REAL</code>, or <code>MXML_TYPE_TEXT</code>. The function is called <em>after</em> 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.</p>
|
||||
<p>The following callback function looks for an attribute named "type" or the element name to determine the value type for its child nodes:</p>
|
||||
<pre><code class="language-c">mxml_type_t
|
||||
type_cb(<span class="reserved">void</span> *cbdata, mxml_node_t *node)
|
||||
my_load_cb(<span class="reserved">void</span> *cbdata, mxml_node_t *node)
|
||||
{
|
||||
<span class="reserved">const</span> <span class="reserved">char</span> *type;
|
||||
|
||||
@ -497,6 +499,8 @@ type_cb(<span class="reserved">void</span> *cbdata, mxml_node_t *node)
|
||||
type = mxmlElementGetAttr(node, <span class="string">"type"</span>);
|
||||
<span class="reserved">if</span> (type == NULL)
|
||||
type = mxmlGetElement(node);
|
||||
<span class="reserved">if</span> (type == NULL)
|
||||
type = <span class="string">"text"</span>;
|
||||
|
||||
<span class="reserved">if</span> (!strcmp(type, <span class="string">"integer"</span>))
|
||||
<span class="reserved">return</span> (MXML_TYPE_INTEGER);
|
||||
@ -508,15 +512,12 @@ type_cb(<span class="reserved">void</span> *cbdata, mxml_node_t *node)
|
||||
<span class="reserved">return</span> (MXML_TYPE_TEXT);
|
||||
}
|
||||
</code></pre>
|
||||
<p>To use this callback function, simply use the name when you call any of the load functions:</p>
|
||||
<pre><code class="language-c">FILE *fp;
|
||||
mxml_node_t *tree;
|
||||
<p>To use this callback function, simply specify it when you call any of the load functions:</p>
|
||||
<pre><code class="language-c">mxml_node_t *tree;
|
||||
|
||||
fp = fopen(<span class="string">"filename.xml"</span>, <span class="string">"r"</span>);
|
||||
tree = mxmlLoadFile(<span class="comment">/*top*/</span>NULL, fp,
|
||||
type_cb, <span class="comment">/*load_cbdata*/</span>NULL,
|
||||
<span class="comment">/*sax_cb*/</span>NULL, <span class="comment">/*sax_cbata*/</span>NULL);
|
||||
fclose(fp);
|
||||
tree = mxmlLoadFilename(<span class="comment">/*top*/</span>NULL, <span class="string">"filename.xml"</span>,
|
||||
my_load_cb, <span class="comment">/*load_cbdata*/</span>NULL,
|
||||
<span class="comment">/*sax_cb*/</span>NULL, <span class="comment">/*sax_cbdata*/</span>NULL);
|
||||
</code></pre>
|
||||
<h3 class="title" id="nodes">Nodes</h3>
|
||||
<p>Every piece of information in an XML file is stored in memory in "nodes". Nodes are defined by the <code>mxml_node_t</code> structure. Each node has a typed value, optional user data, a parent node, sibling nodes (previous and next), and potentially child nodes.</p>
|
||||
@ -549,7 +550,7 @@ val1 val2 val3 | val7 val8
|
||||
val4 val5 val6
|
||||
</code></pre>
|
||||
<p>where "-" is a pointer to the sibling node and "|" is a pointer to the first child or parent node.</p>
|
||||
<p>The <code>mxmlGetType</code> function gets the type of a node:</p>
|
||||
<p>The <a href="#mxmlGetType">mxmlGetType</a> function gets the type of a node:</p>
|
||||
<pre><code class="language-c">mxml_type_t
|
||||
mxmlGetType(mxml_node_t *node);
|
||||
</code></pre>
|
||||
@ -575,7 +576,7 @@ mxmlGetType(mxml_node_t *node);
|
||||
<li><p><code>MXML_TYPE_TEXT</code> : A whitespace-delimited text (fragment) value.</p>
|
||||
</li>
|
||||
</ul>
|
||||
<p>The parent and sibling nodes are accessed using the <code>mxmlGetParent</code>, <code>mxmlGetNextSibling</code>, and <code>mxmlGetPreviousSibling</code> functions, while the children of an element node are accessed using the <code>mxmlGetFirstChild</code> or <code>mxmlGetLastChild</code> functions:</p>
|
||||
<p>The parent and sibling nodes are accessed using the <a href="#mxmlGetParent">mxmlGetParent</a>, <a href="#mxmlGetNextSibling">mxmlGetNextSibling</a>, and <a href="#mxmlGetPreviousSibling">mxmlGetPreviousSibling</a> functions, while the children of an element node are accessed using the <a href="#mxmlGetFirstChild">mxmlGetFirstChild</a> or <a href="#mxmlGetLastChild">mxmlGetLastChild</a> functions:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlGetFirstChild(mxml_node_t *node);
|
||||
|
||||
@ -591,13 +592,13 @@ mxmlGetParent(mxml_node_t *node);
|
||||
mxml_node_t *
|
||||
mxmlGetPrevSibling(mxml_node_t *node);
|
||||
</code></pre>
|
||||
<p>The <code>mxmlGetUserData</code> function gets any user (application) data associated with the node:</p>
|
||||
<p>The <a href="#mxmlGetUserData">mxmlGetUserData</a> function gets any user (application) data associated with the node:</p>
|
||||
<pre><code class="language-c"><span class="reserved">void</span> *
|
||||
mxmlGetUserData(mxml_node_t *node);
|
||||
</code></pre>
|
||||
<h3 class="title" id="creating-xml-documents">Creating XML Documents</h3>
|
||||
<p>You can create and update XML documents in memory using the various <code>mxmlNew</code> functions. The following code will create the XML document described in the previous section:</p>
|
||||
<pre><code class="language-c">mxml_node_t *xml; <span class="comment">/* <?xml ... ?> */</span>
|
||||
<p>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:</p>
|
||||
<pre><code class="language-c">mxml_node_t *xml; <span class="comment">/* <?xml version="1.0"?> */</span>
|
||||
mxml_node_t *data; <span class="comment">/* <data> */</span>
|
||||
mxml_node_t *node; <span class="comment">/* <node> */</span>
|
||||
mxml_node_t *group; <span class="comment">/* <group> */</span>
|
||||
@ -627,43 +628,45 @@ data = mxmlNewElement(xml, <span class="string">"data"</span>);
|
||||
node = mxmlNewElement(data, <span class="string">"node"</span>);
|
||||
mxmlNewText(node, <span class="reserved">false</span>, <span class="string">"val8"</span>);
|
||||
</code></pre>
|
||||
<p>We start by creating the declaration node common to all XML files using the <code>mxmlNewXML</code> function:</p>
|
||||
<p>We start by creating the declaration node common to all XML files using the <a href="#mxmlNewXML">mxmlNewXML</a> function:</p>
|
||||
<pre><code class="language-c">xml = mxmlNewXML(<span class="string">"1.0"</span>);
|
||||
</code></pre>
|
||||
<p>We then create the <code><data></code> node used for this document using the <code>mxmlNewElement</code> function. The first argument specifies the parent node (<code>xml</code>) while the second specifies the element name (<code>data</code>):</p>
|
||||
<p>We then create the <code><data></code> node used for this document using the <a href="#mxmlNewElement">mxmlNewElement</a> function. The first argument specifies the parent node (<code>xml</code>) while the second specifies the element name (<code>data</code>):</p>
|
||||
<pre><code class="language-c">data = mxmlNewElement(xml, <span class="string">"data"</span>);
|
||||
</code></pre>
|
||||
<p>Each <code><node>...</node></code> in the file is created using the <code>mxmlNewElement</code> and <code>mxmlNewText</code> functions. The first argument of <code>mxmlNewText</code> specifies the parent node (<code>node</code>). The second argument specifies whether whitespace appears before the text - 0 or false in this case. The last argument specifies the actual text to add:</p>
|
||||
<p>Each <code><node>...</node></code> in the file is created using the <a href="#mxmlNewElement">mxmlNewElement</a> and <a href="#mxmlNewText">mxmlNewText</a> functions. The first argument of <a href="#mxmlNewText">mxmlNewText</a> specifies the parent node (<code>node</code>). The second argument specifies whether whitespace appears before the text - <code>false</code> in this case. The last argument specifies the actual text to add:</p>
|
||||
<pre><code class="language-c">node = mxmlNewElement(data, <span class="string">"node"</span>);
|
||||
mxmlNewText(node, <span class="reserved">false</span>, <span class="string">"val1"</span>);
|
||||
</code></pre>
|
||||
<p>The resulting in-memory XML document can then be saved or processed just like one loaded from disk or a string.</p>
|
||||
<h3 class="title" id="saving-an-xml-file">Saving an XML File</h3>
|
||||
<p>You save an XML file using the <code>mxmlSaveFile</code> function:</p>
|
||||
<p>You save an XML file using the <a href="#mxmlSaveFilename">mxmlSaveFilename</a> function:</p>
|
||||
<pre><code class="language-c"><span class="reserved">bool</span>
|
||||
mxmlSaveFile(mxml_node_t *node, FILE *fp,
|
||||
mxml_save_cb_t cb);
|
||||
mxmlSaveFilename(mxml_node_t *node, <span class="reserved">const</span> <span class="reserved">char</span> *filename,
|
||||
mxml_save_cb_t cb, <span class="reserved">void</span> *cbdata);
|
||||
</code></pre>
|
||||
<p>The <code>cb</code> argument specifies a function that returns the whitespace (if any) that is inserted before and after each element node. The <code>MXML_NO_CALLBACK</code> constant 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:</p>
|
||||
<pre><code class="language-c">FILE *fp;
|
||||
|
||||
fp = fopen(<span class="string">"filename.xml"</span>, <span class="string">"w"</span>);
|
||||
mxmlSaveFile(xml, fp, MXML_NO_CALLBACK);
|
||||
fclose(fp);
|
||||
<p>The <code>cb</code> and <code>cbdata</code> 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 <code>NULL</code> 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:</p>
|
||||
<pre><code class="language-c">mxmlSaveFile(xml, <span class="string">"filename.xml"</span>, <span class="comment">/*cb*/</span>NULL, <span class="comment">/*cbdata*/</span>NULL);
|
||||
</code></pre>
|
||||
<p>Mini-XML also provides functions to save to a file descriptor or strings:</p>
|
||||
<p>Mini-XML also provides functions to save to a file descriptor, <code>FILE</code> pointer, or strings:</p>
|
||||
<pre><code class="language-c"><span class="reserved">char</span> *
|
||||
mxmlSaveAllocString(mxml_node_t *node, mxml_save_cb_t cb);
|
||||
mxmlSaveAllocString(mxml_node_t *node, mxml_save_cb_t cb,
|
||||
<span class="reserved">void</span> *cbdata);
|
||||
|
||||
<span class="reserved">bool</span>
|
||||
mxmlSaveFd(mxml_node_t *node, <span class="reserved">int</span> fd, mxml_save_cb_t cb);
|
||||
mxmlSaveFd(mxml_node_t *node, <span class="reserved">int</span> fd, mxml_save_cb_t cb,
|
||||
<span class="reserved">void</span> *cbdata);
|
||||
|
||||
<span class="reserved">bool</span>
|
||||
mxmlSaveFile(mxml_node_t *node, FILE *fp, mxml_save_cb_t cb,
|
||||
<span class="reserved">void</span> *cbdata);
|
||||
|
||||
size_t
|
||||
mxmlSaveString(mxml_node_t *node, <span class="reserved">char</span> *buffer, size_t bufsize,
|
||||
mxml_save_cb_t cb);
|
||||
mxml_save_cb_t cb, <span class="reserved">void</span> *cbdata);
|
||||
</code></pre>
|
||||
<h4 id="controlling-line-wrapping">Controlling Line Wrapping</h4>
|
||||
<p>When saving XML documents, Mini-XML normally wraps output lines at column 75 so that the text is readable in terminal windows. The <code>mxmlSetWrapMargin</code> function overrides the default wrap margin for the current thread:</p>
|
||||
<p>When saving XML documents, Mini-XML normally wraps output lines at column 75 so that the text is readable in terminal windows. The <a href="#mxmlSetWrapMargin">mxmlSetWrapMargin</a> function overrides the default wrap margin for the current thread:</p>
|
||||
<pre><code class="language-c"><span class="reserved">void</span> mxmlSetWrapMargin(<span class="reserved">int</span> column);
|
||||
</code></pre>
|
||||
<p>For example, the following code sets the margin to 132 columns:</p>
|
||||
@ -673,10 +676,10 @@ mxmlSaveString(mxml_node_t *node, <span class="reserved">char</span> *buffer, si
|
||||
<pre><code class="language-c">mxmlSetWrapMargin(<span class="number">0</span>);
|
||||
</code></pre>
|
||||
<h4 id="save-callbacks">Save Callbacks</h4>
|
||||
<p>The last argument to the <code>mxmlSave</code> functions is a callback function 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 <code>MXML_WS_BEFORE_OPEN</code>, <code>MXML_WS_AFTER_OPEN</code>, <code>MXML_WS_BEFORE_CLOSE</code>, or <code>MXML_WS_AFTER_CLOSE</code>. The callback function should return <code>NULL</code> if no whitespace should be added or the string to insert (spaces, tabs, carriage returns, and newlines) otherwise.</p>
|
||||
<p>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 <code>where</code> value of <code>MXML_WS_BEFORE_OPEN</code>, <code>MXML_WS_AFTER_OPEN</code>, <code>MXML_WS_BEFORE_CLOSE</code>, or <code>MXML_WS_AFTER_CLOSE</code>. The callback function should return <code>NULL</code> if no whitespace should be added or 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><code class="language-c"><span class="reserved">const</span> <span class="reserved">char</span> *
|
||||
whitespace_cb(mxml_node_t *node, <span class="reserved">int</span> where)
|
||||
whitespace_cb(<span class="reserved">void</span> *cbdata, mxml_node_t *node, mxml_ws_t where)
|
||||
{
|
||||
<span class="reserved">const</span> <span class="reserved">char</span> *element;
|
||||
|
||||
@ -744,23 +747,23 @@ whitespace_cb(mxml_node_t *node, <span class="reserved">int</span> where)
|
||||
mxml_node_t *tree;
|
||||
|
||||
fp = fopen(<span class="string">"filename.xml"</span>, <span class="string">"w"</span>);
|
||||
mxmlSaveFile(tree, fp, whitespace_cb);
|
||||
mxmlSaveFile(tree, fp, whitespace_cb, <span class="comment">/*cbdata*/</span>NULL);
|
||||
fclose(fp);
|
||||
</code></pre>
|
||||
<h3 class="title" id="memory-management">Memory Management</h3>
|
||||
<p>Once you are done with the XML data, use the <code>mxmlDelete</code> function to recursively free the memory that is used for a particular node or the entire tree:</p>
|
||||
<p>Once you are done with the XML data, use the <a href="#mxmlDelete">mxmlDelete</a> function to free the memory that is used for a particular node and its children:</p>
|
||||
<pre><code class="language-c"><span class="reserved">void</span>
|
||||
mxmlDelete(mxml_node_t *tree);
|
||||
</code></pre>
|
||||
<p>You can also use reference counting to manage memory usage. The <code>mxmlRetain</code> and <code>mxmlRelease</code> functions increment and decrement a node's use count, respectively. When the use count goes to zero, <code>mxmlRelease</code> automatically calls <code>mxmlDelete</code> to actually free the memory used by the node tree. New nodes start with a use count of 1.</p>
|
||||
<p>You can also use reference counting to manage memory usage. The <a href="#mxmlRetain">mxmlRetain</a> and <a href="#mxmlRelease">mxmlRelease</a> functions increment and decrement a node's use count, respectively. When the use count goes to zero, <a href="#mxmlRelease">mxmlRelease</a> calls <a href="#mxmlDelete">mxmlDelete</a> to actually free the memory used by the node tree. New nodes start with a use count of <code>1</code>.</p>
|
||||
<h2 class="title" id="more-about-nodes">More About Nodes</h2>
|
||||
<h3 class="title" id="element-nodes">Element Nodes</h3>
|
||||
<p>Element (<code>MXML_TYPE_ELEMENT</code>) nodes are created using the <code>mxmlNewElement</code> function. Element attributes are set using the <code>mxmlElementSetAttr</code> and <code>mxmlElementSetAttrf</code> functions and cleared using the <code>mxmlElementDeleteAttr</code> function:</p>
|
||||
<p>Element (<code>MXML_TYPE_ELEMENT</code>) nodes are created using the <a href="#mxmlNewElement">mxmlNewElement</a> function. Element attributes are set using the <a href="#mxmlElementSetAttr">mxmlElementSetAttr</a> and <a href="#mxmlElementSetAttrf">mxmlElementSetAttrf</a> functions and cleared using the <a href="#mxmlElementClearAttr">mxmlElementClearAttr</a> function:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlNewElement(mxml_node_t *parent, <span class="reserved">const</span> <span class="reserved">char</span> *name);
|
||||
|
||||
<span class="reserved">void</span>
|
||||
mxmlElementDeleteAttr(mxml_node_t *node, <span class="reserved">const</span> <span class="reserved">char</span> *name);
|
||||
mxmlElementClearAttr(mxml_node_t *node, <span class="reserved">const</span> <span class="reserved">char</span> *name);
|
||||
|
||||
<span class="reserved">void</span>
|
||||
mxmlElementSetAttr(mxml_node_t *node, <span class="reserved">const</span> <span class="reserved">char</span> *name,
|
||||
@ -770,11 +773,7 @@ mxmlElementSetAttr(mxml_node_t *node, <span class="reserved">const</span> <span
|
||||
mxmlElementSetAttrf(mxml_node_t *node, <span class="reserved">const</span> <span class="reserved">char</span> *name,
|
||||
<span class="reserved">const</span> <span class="reserved">char</span> *format, ...);
|
||||
</code></pre>
|
||||
<p>Child nodes are added using the various <code>mxmlNew</code> functions. The top (root) node must be an element, usually created by the <code>mxmlNewXML</code> function:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlNewXML(<span class="reserved">const</span> <span class="reserved">char</span> *version);
|
||||
</code></pre>
|
||||
<p>The <code>mxmlGetElement</code> function retrieves the element name, the <code>mxmlElementGetAttr</code> function retrieves the value string for a named attribute associated with the element. The <code>mxmlElementGetAttrByIndex</code> and <code>mxmlElementGetAttrCount</code> functions retrieve attributes by index:</p>
|
||||
<p>The <a href="#mxmlGetElement">mxmlGetElement</a> function retrieves the element name while the <a href="#mxmlElementGetAttr">mxmlElementGetAttr</a> function retrieves the value string for a named attribute associated with the element. The <a href="#mxmlElementGetAttrByIndex">mxmlElementGetAttrByIndex</a> and <a href="#mxmlElementGetAttrCount">mxmlElementGetAttrCount</a> functions retrieve attributes by index:</p>
|
||||
<pre><code class="language-c"><span class="reserved">const</span> <span class="reserved">char</span> *
|
||||
mxmlGetElement(mxml_node_t *node);
|
||||
|
||||
@ -789,48 +788,64 @@ size_t
|
||||
mxmlElementGetAttrCount(mxml_node_t *node);
|
||||
</code></pre>
|
||||
<h3 class="title" id="cdata-nodes">CDATA Nodes</h3>
|
||||
<p>CDATA (<code>MXML_TYPE_CDATA</code>) nodes are created using the <code>mxmlNewCDATA</code> function:</p>
|
||||
<pre><code>mxml_node_t *mxmlNewCDATA(mxml_node_t *parent, const char *string);
|
||||
<p>CDATA (<code>MXML_TYPE_CDATA</code>) nodes are created using the <a href="#mxmlNewCDATA">mxmlNewCDATA</a> and <a href="#mxmlNewCDATAf">mxmlNewCDATAf</a> functions:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlNewCDATA(mxml_node_t *parent, <span class="reserved">const</span> <span class="reserved">char</span> *string);
|
||||
|
||||
mxml_node_t *
|
||||
mxmlNewCDATAf(mxml_node_t *parent, <span class="reserved">const</span> <span class="reserved">char</span> *format, ...);
|
||||
</code></pre>
|
||||
<p>The <code>mxmlGetCDATA</code> function retrieves the CDATA string pointer for a node:</p>
|
||||
<pre><code>const char *mxmlGetCDATA(mxml_node_t *node);
|
||||
<p>The <a href="#mxmlGetCDATA">mxmlGetCDATA</a> function retrieves the CDATA string pointer for a node:</p>
|
||||
<pre><code class="language-c"><span class="reserved">const</span> <span class="reserved">char</span> *
|
||||
mxmlGetCDATA(mxml_node_t *node);
|
||||
</code></pre>
|
||||
<h3 class="title" id="comment-nodes">Comment Nodes</h3>
|
||||
<p>Comment (<code>MXML_TYPE_COMMENT</code>) nodes are created using the <code>mxmlNewComment</code> function, for example:</p>
|
||||
<p>Comment (<code>MXML_TYPE_COMMENT</code>) nodes are created using the <a href="#mxmlNewComment">mxmlNewComment</a> and <a href="#mxmlNewCommentf">mxmlNewCommentf</a> functions, for example:</p>
|
||||
<pre><code class="language-c">mxml_node_t *node = mxmlNewComment(<span class="string">" This is a comment "</span>);
|
||||
|
||||
mxml_node_t *node = mxmlNewCommentf(<span class="string">" This is comment %d "</span>, <span class="number">42</span>);
|
||||
</code></pre>
|
||||
<p>Similarly, the <code>mxmlGetComment</code> function retrieves the comment string pointer for a node:</p>
|
||||
<p>Similarly, the <a href="#mxmlGetComment">mxmlGetComment</a> function retrieves the comment string pointer for a node:</p>
|
||||
<pre><code class="language-c"><span class="reserved">const</span> <span class="reserved">char</span> *comment = mxmlGetComment(node);
|
||||
<span class="comment">/* returns " This is a comment " */</span>
|
||||
</code></pre>
|
||||
<h3 class="title" id="processing-instruction-nodes">Processing Instruction Nodes</h3>
|
||||
<p>Processing instruction (<code>MXML_TYPE_DIRECTIVE</code>) nodes are created using the <code>mxmlNewDirective</code> function:</p>
|
||||
<p>Processing instruction (<code>MXML_TYPE_DIRECTIVE</code>) nodes are created using the <a href="#mxmlNewDirective">mxmlNewDirective</a> and <a href="#mxmlNewDirectivef">mxmlNewDirectivef</a> functions:</p>
|
||||
<pre><code class="language-c">mxml_node_t *node = mxmlNewDirective(<span class="string">"xml-stylesheet type=\"text/css\" href=\"style.css\""</span>);
|
||||
|
||||
mxml_node_t *node = mxmlNewDirectivef(<span class="string">"xml version=\"%s\""</span>, version);
|
||||
</code></pre>
|
||||
<p>The <code>mxmlGetDirective</code> function retrieves the processing instruction string for a node:</p>
|
||||
<p>The <a href="#mxmlGetDirective">mxmlGetDirective</a> function retrieves the processing instruction string for a node:</p>
|
||||
<pre><code class="language-c"><span class="reserved">const</span> <span class="reserved">char</span> *instr = mxmlGetElement(node);
|
||||
<span class="comment">/* returns "xml-stylesheet type=\"text/css\" href=\"style.css\"" */</span>
|
||||
</code></pre>
|
||||
<p>The <a href="#mxmlNewXML">mxmlNewXML</a> function can be used to create the top-level "xml" processing instruction with an associated version number:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlNewXML(<span class="reserved">const</span> <span class="reserved">char</span> *version);
|
||||
</code></pre>
|
||||
<h3 class="title" id="integer-nodes">Integer Nodes</h3>
|
||||
<p>Integer (<code>MXML_TYPE_INTEGER</code>) nodes are created using the <code>mxmlNewInteger</code> function:</p>
|
||||
<p>Integer (<code>MXML_TYPE_INTEGER</code>) nodes are created using the <a href="#mxmlNewInteger">mxmlNewInteger</a> function:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlNewInteger(mxml_node_t *parent, <span class="reserved">long</span> integer);
|
||||
</code></pre>
|
||||
<p>The <code>mxmlGetInteger</code> function retrieves the integer value for a node:</p>
|
||||
<p>The <a href="#mxmlGetInteger">mxmlGetInteger</a> function retrieves the integer value for a node:</p>
|
||||
<pre><code class="language-c"><span class="reserved">long</span>
|
||||
mxmlGetInteger(mxml_node_t *node);
|
||||
</code></pre>
|
||||
<h3 class="title" id="opaque-string-nodes">Opaque String Nodes</h3>
|
||||
<p>Opaque string (<code>MXML_TYPE_OPAQUE</code>) nodes are created using the <code>mxmlNewOpaque</code> function:</p>
|
||||
<p>Opaque string (<code>MXML_TYPE_OPAQUE</code>) nodes are created using the <a href="#mxmlNewOpaque">mxmlNewOpaque</a> and <a href="#mxmlNewOpaquef">mxmlNewOpaquef</a> functions:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlNewOpaque(mxml_node_t *parent, <span class="reserved">const</span> <span class="reserved">char</span> *opaque);
|
||||
|
||||
mxml_node_t *
|
||||
mxmlNewOpaquef(mxml_node_t *parent, <span class="reserved">const</span> <span class="reserved">char</span> *format, ...);
|
||||
</code></pre>
|
||||
<p>The <code>mxmlGetOpaque</code> function retrieves the opaque string pointer for a node:</p>
|
||||
<p>The <a href="#mxmlGetOpaque">mxmlGetOpaque</a> function retrieves the opaque string pointer for a node:</p>
|
||||
<pre><code class="language-c"><span class="reserved">const</span> <span class="reserved">char</span> *
|
||||
mxmlGetOpaque(mxml_node_t *node);
|
||||
</code></pre>
|
||||
<h3 class="title" id="text-nodes">Text Nodes</h3>
|
||||
<p>Whitespace-delimited text string (<code>MXML_TYPE_TEXT</code>) nodes are created using the <code>mxmlNewText</code> and <code>mxmlNewTextf</code> functions. Each text node consists of a text string and (leading) whitespace boolean value.</p>
|
||||
<p>Whitespace-delimited text string (<code>MXML_TYPE_TEXT</code>) nodes are created using the <a href="#mxmlNewText">mxmlNewText</a> and <a href="#mxmlNewTextf">mxmlNewTextf</a> functions. Each text node consists of a text string and (leading) whitespace boolean value.</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlNewText(mxml_node_t *parent, <span class="reserved">bool</span> whitespace,
|
||||
<span class="reserved">const</span> <span class="reserved">char</span> *string);
|
||||
@ -839,23 +854,23 @@ mxml_node_t *
|
||||
mxmlNewTextf(mxml_node_t *parent, <span class="reserved">bool</span> whitespace,
|
||||
<span class="reserved">const</span> <span class="reserved">char</span> *format, ...);
|
||||
</code></pre>
|
||||
<p>The <code>mxmlGetText</code> function retrieves the text string pointer and whitespace boolean value for a node:</p>
|
||||
<p>The <a href="#mxmlGetText">mxmlGetText</a> function retrieves the text string pointer and whitespace boolean value for a node:</p>
|
||||
<pre><code class="language-c"><span class="reserved">const</span> <span class="reserved">char</span> *
|
||||
mxmlGetText(mxml_node_t *node, <span class="reserved">bool</span> *whitespace);
|
||||
</code></pre>
|
||||
<h3 class="title" id="real-number-nodes">Real Number Nodes</h3>
|
||||
<p>Real number (<code>MXML_TYPE_REAL</code>) nodes are created using the <code>mxmlNewReal</code> function:</p>
|
||||
<p>Real number (<code>MXML_TYPE_REAL</code>) nodes are created using the <a href="#mxmlNewReal">mxmlNewReal</a> function:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlNewReal(mxml_node_t *parent, <span class="reserved">double</span> real);
|
||||
</code></pre>
|
||||
<p>The <code>mxmlGetReal</code> function retrieves the real number for a node:</p>
|
||||
<p>The <a href="#mxmlGetReal">mxmlGetReal</a> function retrieves the real number for a node:</p>
|
||||
<pre><code class="language-c"><span class="reserved">double</span>
|
||||
mxmlGetReal(mxml_node_t *node);
|
||||
</code></pre>
|
||||
<h2 class="title" id="locating-data-in-an-xml-document">Locating Data in an XML Document</h2>
|
||||
<p>Mini-XML provides many functions for enumerating, searching, and indexing XML documents.</p>
|
||||
<h3 class="title" id="finding-nodes">Finding Nodes</h3>
|
||||
<p>The <code>mxmlFindPath</code> function finds the (first) value node under a specific element using a "path":</p>
|
||||
<p>The <a href="#mxmlFindPath">mxmlFindPath</a> function finds the (first) value node under a specific element using a "path":</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlFindPath(mxml_node_t *node, <span class="reserved">const</span> <span class="reserved">char</span> *path);
|
||||
</code></pre>
|
||||
@ -864,7 +879,7 @@ mxmlFindPath(mxml_node_t *node, <span class="reserved">const</span> <span class=
|
||||
|
||||
mxml_node_t *value = mxmlFindPath(xml, <span class="string">"data/*/node"</span>);
|
||||
</code></pre>
|
||||
<p>The <code>mxmlFindElement</code> function can be used to find a named element, optionally matching an attribute and value:</p>
|
||||
<p>The <a href="#mxmlFindElement">mxmlFindElement</a> function can be used to find a named element, optionally matching an attribute and value:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlFindElement(mxml_node_t *node, mxml_node_t *top,
|
||||
<span class="reserved">const</span> <span class="reserved">char</span> *element, <span class="reserved">const</span> <span class="reserved">char</span> *attr,
|
||||
@ -881,7 +896,7 @@ node = mxmlFindElement(tree, tree, <span class="string">"a"</span>, <s
|
||||
|
||||
<span class="comment">/* Find the first "a" element with "href" to a URL */</span>
|
||||
node = mxmlFindElement(tree, tree, <span class="string">"a"</span>, <span class="string">"href"</span>,
|
||||
<span class="string">"http://michaelrsweet.github.io/"</span>,
|
||||
<span class="string">"http://msweet.org/"</span>,
|
||||
MXML_DESCEND_ALL);
|
||||
|
||||
<span class="comment">/* Find the first element with a "src" attribute*/</span>
|
||||
@ -914,7 +929,7 @@ node = mxmlFindElement(tree, tree, NULL, <span class="string">"src"</s
|
||||
</li>
|
||||
</ul>
|
||||
<h3 class="title" id="iterating-nodes">Iterating Nodes</h3>
|
||||
<p>While the <code>mxmlFindNode</code> and <code>mxmlFindPath</code> functions will find a particular element node, sometimes you need to iterate over all nodes. The <code>mxmlWalkNext</code> and <code>mxmlWalkPrev</code> functions can be used to iterate through the XML node tree:</p>
|
||||
<p>While the <a href="#mxmlFindNode">mxmlFindNode</a> and <a href="#mxmlFindPath">mxmlFindPath</a> functions will find a particular element node, sometimes you need to iterate over all nodes. The <a href="#mxmlWalkNext">mxmlWalkNext</a> and <a href="#mxmlWalkPrev">mxmlWalkPrev</a> functions can be used to iterate through the XML node tree:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlWalkNext(mxml_node_t *node, mxml_node_t *top,
|
||||
<span class="reserved">int</span> descend);
|
||||
@ -955,7 +970,7 @@ val7
|
||||
val8
|
||||
</code></pre>
|
||||
<h3 class="title" id="indexing">Indexing</h3>
|
||||
<p>The <code>mxmlIndexNew</code> function allows you to create an index of nodes for faster searching and enumeration:</p>
|
||||
<p>The <a href="#mxmlIndexNew">mxmlIndexNew</a> function allows you to create an index of nodes for faster searching and enumeration:</p>
|
||||
<pre><code class="language-c">mxml_index_t *
|
||||
mxmlIndexNew(mxml_node_t *node, <span class="reserved">const</span> <span class="reserved">char</span> *element,
|
||||
<span class="reserved">const</span> <span class="reserved">char</span> *attr);
|
||||
@ -964,7 +979,7 @@ mxmlIndexNew(mxml_node_t *node, <span class="reserved">const</span> <span class=
|
||||
<p>For example, the following code creates an index of all "id" values in an XML document:</p>
|
||||
<pre><code class="language-c">mxml_index_t *ind = mxmlIndexNew(xml, NULL, <span class="string">"id"</span>);
|
||||
</code></pre>
|
||||
<p>Once the index is created, the <code>mxmlIndexFind</code> function can be used to find a matching node:</p>
|
||||
<p>Once the index is created, the <a href="#mxmlIndexFind">mxmlIndexFind</a> function can be used to find a matching node:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlIndexFind(mxml_index_t *ind, <span class="reserved">const</span> <span class="reserved">char</span> *element,
|
||||
<span class="reserved">const</span> <span class="reserved">char</span> *value);
|
||||
@ -972,7 +987,7 @@ mxmlIndexFind(mxml_index_t *ind, <span class="reserved">const</span> <span class
|
||||
<p>For example, the following code will find the element whose "id" string is "42":</p>
|
||||
<pre><code class="language-c">mxml_node_t *node = mxmlIndexFind(ind, NULL, <span class="string">"42"</span>);
|
||||
</code></pre>
|
||||
<p>Alternately, the <code>mxmlIndexReset</code> and <code>mxmlIndexEnum</code> functions can be used to enumerate the nodes in the index:</p>
|
||||
<p>Alternately, the <a href="#mxmlIndexReset">mxmlIndexReset</a> and <a href="#mxmlIndexEnum">mxmlIndexEnum</a> functions can be used to enumerate the nodes in the index:</p>
|
||||
<pre><code class="language-c">mxml_node_t *
|
||||
mxmlIndexReset(mxml_index_t *ind);
|
||||
|
||||
@ -989,21 +1004,21 @@ mxmlIndexEnum(mxml_index_t *ind);
|
||||
... <span class="reserved">do</span> something ...
|
||||
}
|
||||
</code></pre>
|
||||
<p>The <code>mxmlIndexCount</code> function returns the number of nodes in the index:</p>
|
||||
<p>The <a href="#mxmlIndexCount">mxmlIndexCount</a> function returns the number of nodes in the index:</p>
|
||||
<pre><code class="language-c">size_t
|
||||
mxmlIndexGetCount(mxml_index_t *ind);
|
||||
</code></pre>
|
||||
<p>Finally, the <code>mxmlIndexDelete</code> function frees all memory associated with the index:</p>
|
||||
<p>Finally, the <a href="#mxmlIndexDelete">mxmlIndexDelete</a> function frees all memory associated with the index:</p>
|
||||
<pre><code class="language-c"><span class="reserved">void</span>
|
||||
mxmlIndexDelete(mxml_index_t *ind);
|
||||
</code></pre>
|
||||
<h2 class="title" id="custom-data-types">Custom Data Types</h2>
|
||||
<p>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 <code>MXML_TYPE_CUSTOM</code> node type identifies custom data nodes.</p>
|
||||
<p>The <code>mxmlGetCustom</code> function retrieves the custom value pointer for a node.</p>
|
||||
<p>The <a href="#mxmlGetCustom">mxmlGetCustom</a> function retrieves the custom value pointer for a node.</p>
|
||||
<pre><code class="language-c"><span class="reserved">const</span> <span class="reserved">void</span> *
|
||||
mxmlGetCustom(mxml_node_t *node);
|
||||
</code></pre>
|
||||
<p>Custom (<code>MXML_TYPE_CUSTOM</code>) nodes are created using the <code>mxmlNewCustom</code> function or using a custom per-thread load callbacks specified using the <code>mxmlSetCustomHandlers</code> function:</p>
|
||||
<p>Custom (<code>MXML_TYPE_CUSTOM</code>) nodes are created using the <a href="#mxmlNewCustom">mxmlNewCustom</a> function or using a custom per-thread load callbacks specified using the <a href="#mxmlSetCustomHandlers">mxmlSetCustomHandlers</a> function:</p>
|
||||
<pre><code class="language-c"><span class="reserved">typedef</span> <span class="reserved">void</span> (*mxml_custom_destroy_cb_t)(<span class="reserved">void</span> *);
|
||||
<span class="reserved">typedef</span> <span class="reserved">bool</span> (*mxml_custom_load_cb_t)(mxml_node_t *, <span class="reserved">const</span> <span class="reserved">char</span> *);
|
||||
<span class="reserved">typedef</span> <span class="reserved">char</span> *(*mxml_custom_save_cb_t)(mxml_node_t *);
|
||||
@ -1127,12 +1142,12 @@ save_custom(mxml_node_t *node)
|
||||
<span class="reserved">return</span> (strdup(data));
|
||||
}
|
||||
</code></pre>
|
||||
<p>You register the callback functions using the <code>mxmlSetCustomHandlers</code> function:</p>
|
||||
<pre><code class="language-c">mxmlSetCustomHandlers(load_custom, save_custom);
|
||||
<p>You register the callback functions using the <a href="#mxmlSetCustomCallbacks">mxmlSetCustomCallbacks</a> function:</p>
|
||||
<pre><code class="language-c">mxmlSetCustomCallbacks(load_custom, save_custom);
|
||||
</code></pre>
|
||||
<h2 class="title" id="sax-stream-loading-of-documents">SAX (Stream) Loading of Documents</h2>
|
||||
<p>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.</p>
|
||||
<p>The <code>mxmlLoadFd</code>, <code>mxmlLoadFile</code>, <code>mxmlLoadFilename</code>, <code>mxmlLoadIO</code>, and <code>mxmlLoadString</code> 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 <code>true</code> to continue processing or <code>false</code> to stop:</p>
|
||||
<p>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 <code>true</code> to continue processing or <code>false</code> to stop:</p>
|
||||
<pre><code class="language-c"><span class="reserved">bool</span>
|
||||
sax_cb(<span class="reserved">void</span> *cbdata, mxml_node_t *node,
|
||||
mxml_sax_event_t event)
|
||||
@ -1160,7 +1175,7 @@ sax_cb(<span class="reserved">void</span> *cbdata, mxml_node_t *node,
|
||||
<li><p><code>MXML_SAX_EVENT_ELEMENT_OPEN</code> - An open element was just read (<code><element></code>)</p>
|
||||
</li>
|
||||
</ul>
|
||||
<p>Elements are <em>released</em> after the close element is processed. All other nodes are released after they are processed. The SAX callback can <em>retain</em> the node using the <code>mxmlRetain</code> function. For example, the following SAX callback will retain all nodes, effectively simulating a normal in-memory load:</p>
|
||||
<p>Elements are <em>released</em> after the close element is processed. All other nodes are released after they are processed. The SAX callback can <em>retain</em> the node using the <a href="#mxmlRetain">mxmlRetain</a> function. For example, the following SAX callback will retain all nodes, effectively simulating a normal in-memory load:</p>
|
||||
<pre><code class="language-c"><span class="reserved">bool</span>
|
||||
sax_cb(<span class="reserved">void</span> *cbdata, mxml_node_t *node, mxml_sax_event_t event)
|
||||
{
|
||||
@ -1269,7 +1284,7 @@ print_children(mxml_node_t *parent)
|
||||
</li>
|
||||
<li><p>SAX callbacks now return a boolean value.</p>
|
||||
</li>
|
||||
<li><p>The <code>mxmlSAXLoadXxx</code> functions have been removed in favor of passing the SAX callback function and data pointers to the <code>mxmlLoadXxx</code> functions.</p>
|
||||
<li><p>The mxmlSAXLoadXxx functions have been removed in favor of passing the SAX callback function and data pointers to the mxmlLoadXxx functions.</p>
|
||||
</li>
|
||||
<li><p>Node types are now named <code>MXML_TYPE_foo</code> instead of <code>MXML_foo</code>.</p>
|
||||
</li>
|
||||
@ -1281,8 +1296,12 @@ print_children(mxml_node_t *parent)
|
||||
</li>
|
||||
<li><p>Comment nodes ("<code><!-- ... --></code>") now have their own type (<code>MXML_TYPE_COMMENT</code>).</p>
|
||||
</li>
|
||||
<li><p>Custom node callbacks are now set using the <a href="#mxmlSetCustomCallbacks">mxmlSetCustomCallbacks</a> function instead of mxmlSetCustomHandlers.</p>
|
||||
</li>
|
||||
<li><p>Declaration nodes ("<code><!...></code>") now have their own type (<code>MXML_TYPE_DECLARATION</code>).</p>
|
||||
</li>
|
||||
<li><p>Element attributes are now cleared with the <a href="#mxmlElementClearAttr">mxmlElementClearAttr</a> function instead of mxmlElementDeleteAttr.</p>
|
||||
</li>
|
||||
<li><p>Processing instruction/directive nodes ("<code><?...?></code>") now have their own type (<code>MXML_TYPE_DIRECTIVE</code>).</p>
|
||||
</li>
|
||||
<li><p>Integer nodes (<code>MXML_TYPE_INTEGER</code>) now use the <code>long</code> type.</p>
|
||||
@ -1324,10 +1343,10 @@ void mxmlDelete(<a href="#mxml_node_t">mxml_node_t</a> *node);</p>
|
||||
<h4 class="discussion">Discussion</h4>
|
||||
<p class="discussion">If the specified node has a parent, this function first removes the
|
||||
node from its parent using the <a href="#mxmlRemove"><code>mxmlRemove</code></a> function.</p>
|
||||
<h3 class="function"><a id="mxmlElementDeleteAttr">mxmlElementDeleteAttr</a></h3>
|
||||
<h3 class="function"><a id="mxmlElementClearAttr">mxmlElementClearAttr</a></h3>
|
||||
<p class="description">Delete an attribute.</p>
|
||||
<p class="code">
|
||||
void mxmlElementDeleteAttr(<a href="#mxml_node_t">mxml_node_t</a> *node, const char *name);</p>
|
||||
void mxmlElementClearAttr(<a href="#mxml_node_t">mxml_node_t</a> *node, const char *name);</p>
|
||||
<h4 class="parameters">Parameters</h4>
|
||||
<table class="list"><tbody>
|
||||
<tr><th>node</th>
|
||||
|
10
mxml-attr.c
10
mxml-attr.c
@ -20,18 +20,18 @@ static bool mxml_set_attr(mxml_node_t *node, const char *name, char *value);
|
||||
|
||||
|
||||
//
|
||||
// 'mxmlElementDeleteAttr()' - Delete an attribute.
|
||||
// 'mxmlElementClearAttr()' - Delete an attribute.
|
||||
//
|
||||
|
||||
void
|
||||
mxmlElementDeleteAttr(mxml_node_t *node,// I - Element
|
||||
const char *name)// I - Attribute name
|
||||
mxmlElementClearAttr(mxml_node_t *node, // I - Element
|
||||
const char *name) // I - Attribute name
|
||||
{
|
||||
size_t i; // Looping var
|
||||
_mxml_attr_t *attr; // Cirrent attribute
|
||||
|
||||
|
||||
MXML_DEBUG("mxmlElementDeleteAttr(node=%p, name=\"%s\")\n", node, name ? name : "(null)");
|
||||
MXML_DEBUG("mxmlElementClearAttr(node=%p, name=\"%s\")\n", node, name ? name : "(null)");
|
||||
|
||||
// Range check input...
|
||||
if (!node || node->type != MXML_TYPE_ELEMENT || !name)
|
||||
@ -40,7 +40,7 @@ mxmlElementDeleteAttr(mxml_node_t *node,// I - Element
|
||||
// Look for the attribute...
|
||||
for (i = node->value.element.num_attrs, attr = node->value.element.attrs; i > 0; i --, attr ++)
|
||||
{
|
||||
MXML_DEBUG("mxmlElementDeleteAttr: %s=\"%s\"\n", attr->name, attr->value);
|
||||
MXML_DEBUG("mxmlElementClearAttr: %s=\"%s\"\n", attr->name, attr->value);
|
||||
|
||||
if (!strcmp(attr->name, name))
|
||||
{
|
||||
|
2
mxml.h
2
mxml.h
@ -144,7 +144,7 @@ extern void mxmlAdd(mxml_node_t *parent, mxml_add_t add, mxml_node_t *child, mx
|
||||
|
||||
extern void mxmlDelete(mxml_node_t *node);
|
||||
|
||||
extern void mxmlElementDeleteAttr(mxml_node_t *node, const char *name);
|
||||
extern void mxmlElementClearAttr(mxml_node_t *node, const char *name);
|
||||
extern const char *mxmlElementGetAttr(mxml_node_t *node, const char *name);
|
||||
extern const char *mxmlElementGetAttrByIndex(mxml_node_t *node, int idx, const char **name);
|
||||
extern size_t mxmlElementGetAttrCount(mxml_node_t *node);
|
||||
|
Loading…
Reference in New Issue
Block a user