diff --git a/doc/body.md b/doc/body.md index 44e8b4c..d5eaed7 100644 --- a/doc/body.md +++ b/doc/body.md @@ -5,8 +5,6 @@ copyright: Copyright © 2003-2024, All Rights Reserved. version: 4.0 ... -> TODO: Update for mxmlOptions APIs! - Introduction ============ @@ -102,6 +100,80 @@ proper compiler and linker options for your installation: > `--disable-libmxml4-prefix` configure option the library is named "mxml". +API Basics +---------- + +Every piece of information in an XML file is stored in memory in "nodes". Nodes +are represented by `mxml_node_t` pointers. Each node has an associated type, +value(s), a parent node, sibling nodes (previous and next), potentially first +and last child nodes, and an optional user data pointer. + +For example, if you have an XML file like the following: + +```xml + + + val1 + val2 + val3 + + val4 + val5 + val6 + + val7 + val8 + +``` + +the node tree for the file would look like the following in memory: + +``` + + | + + | + - - - - - + | | | | | | + val1 val2 val3 | val7 val8 + | + - - + | | | + 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 which is represented as a +`mxml_type_t` enumeration value: + +- `MXML_TYPE_CDATA`: CDATA such as ``, +- `MXML_TYPE_COMMENT`: A comment such as ``, +- `MXML_TYPE_CUSTOM`: A custom value defined by your application, +- `MXML_TYPE_DECLARATION`: A declaration such as ``, +- `MXML_TYPE_DIRECTIVE`: A processing instruction such as + ``, +- `MXML_TYPE_ELEMENT`: An XML element with optional attributes such as + ``, +- `MXML_TYPE_INTEGER`: A whitespace-delimited integer value such as `42`, +- `MXML_TYPE_OPAQUE`: An opaque string value that preserves all whitespace + such as `All work and no play makes Johnny a dull boy.`, +- `MXML_TYPE_REAL`: A whitespace-delimited floating point value such as + `123.4`, or +- `MXML_TYPE_TEXT`: A whitespace-delimited text (fragment) value such as + `Word`. + +The parent, sibling, and child nodes are accessed using the [mxmlGetParent](@@), +[mxmlGetNextSibling](@@), [mxmlGetPreviousSibling](@@), [mxmlGetFirstChild](@@), +and [mxmlGetLastChild](@@) functions. + +The value(s) of a node are accessed using the [mxmlGetCDATA](@@), +[mxmlGetComment](@@), [mxmlGetDeclaration](@@), [mxmlGetDirective](@@), +[mxmlGetElement](@@), [mxmlElementGetAttr](@@), [mxmlGetInteger](@@), +[mxmlGetOpaque](@@), [mxmlGetReal](@@), and [mxmlGetText](@@) functions. + + Loading an XML File ------------------- @@ -142,13 +214,14 @@ default load options: ```c mxml_node_t *xml; -xml = mxmlLoadFilename(/*top*/NULL, /*options*/NULL, "example.xml"); +xml = mxmlLoadFilename(/*top*/NULL, /*options*/NULL, + "example.xml"); ``` ### Load Options -Load options are specified using a `mxml_options_t` object, which you create +Load options are specified using a `mxml_options_t` pointer, which you create using the [mxmlOptionsNew](@@) function: ```c @@ -176,42 +249,9 @@ mxmlOptionsSetTypeValue(options, my_type_cb, /*cbdata*/NULL); The `my_type_cb` function accepts the callback data pointer (`NULL` in this case) and the `mxml_node_t` pointer for the current element and returns a -`mxml_type_t` enumeration value specifying the value type for child nodes: - - -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: - -```c -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); -``` - - -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` enumeration value specifying the value type for child nodes. For +example, the following function looks at the "type" attribute and the element +name to determine the value types of the node's children: ```c mxml_type_t @@ -241,176 +281,198 @@ my_load_cb(void *cbdata, mxml_node_t *node) } ``` -To use this callback function, simply specify it when you call any of the load -functions: + +Finding Nodes +------------- + +The [mxmlFindPath](@@) function finds the (first) value node under a specific +element using a 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: ```c -mxml_node_t *tree; +mxml_node_t *directnode = mxmlFindPath(xml, "data/group/node"); -tree = mxmlLoadFilename(/*top*/NULL, "filename.xml", - my_load_cb, /*load_cbdata*/NULL, - /*sax_cb*/NULL, /*sax_cbdata*/NULL); +mxml_node_t *wildnode = mxmlFindPath(xml, "data/*/node"); ``` - -Nodes ------ - -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: - - - - val1 - val2 - val3 - - val4 - val5 - val6 - - val7 - val8 - - -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: - -```c -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 ``, -- `MXML_TYPE_DIRECTIVE` : A processing instruction such as - ``, -- `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: +The [mxmlFindElement](@@) function can be used to find a named element, +optionally matching an attribute and value: ```c 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); +mxmlFindElement(mxml_node_t *node, mxml_node_t *top, + const char *element, const char *attr, + const char *value, int descend); ``` -The [mxmlGetUserData](@@) function gets any user (application) data associated -with the node: +The `element`, `attr`, and `value` arguments can be passed as `NULL` to act as +wildcards, e.g.: ```c -void * -mxmlGetUserData(mxml_node_t *node); +mxml_node_t *node; + +/* 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); ``` - -Creating XML Documents ----------------------- - -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: +You can also iterate with the same function: ```c -mxml_node_t *xml; /* */ -mxml_node_t *data; /* */ -mxml_node_t *node; /* */ -mxml_node_t *group; /* */ +mxml_node_t *node; -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"); +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 ... +} ``` -We start by creating the declaration node common to all XML files using the -[mxmlNewXML](@@) function: +The `descend` argument \(`MXML_DESCEND_ALL` in the previous examples) 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 `` and `` elements + under the `` parent node in the previous example. + +- `MXML_DESCEND_ALL`: search child nodes first, then sibling nodes, and then + parent nodes. + + +Getting the Value(s) from Nodes +------------------------------- + +Once you have the node you can use one of the mxmlGetXxx functions to retrieve +its value(s). + +Element \(`MXML_TYPE_ELEMENT`) nodes have an associated name and zero or more +named attributes with (string) values. The [mxmlGetElement](@@) function +retrieves the element name while the [mxmlElementGetAttr](@@) function retrieves +the value string for a named attribute. For example, the following code looks +for HTML heading elements and, when found, displays the "id" attribute for the +heading: ```c -xml = mxmlNewXML("1.0"); +const char *elemname = mxmlGetElement(node); +const char *id_value = mxmlElementGetAttr(node, "id"); + +if ((*elemname == 'h' || *elemname == 'H') && + elemname[1] >= '1' && elemname[1] <= '6' && + id_value != NULL) + printf("%s: %s\n", elemname, id_value); ``` -We then create the `` 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`): +The [mxmlElementGetAttrByIndex](@@) and [mxmlElementGetAttrCount](@@) functions +allow you to iterate all attributes of an element. For example, the following +code prints the element name and each of its attributes: ```c -data = mxmlNewElement(xml, "data"); +const char *elemname = mxmlGetElement(node); +printf("%s:\n", elemname); + +size_t i, count; +for (i = 0, count = mxmlElementGetAttrCount(node); i < count; i ++) +{ + const char *attrname, *attrvalue; + + attrvalue = mxmlElementGetAttrByIndex(node, i, &attrname); + + printf(" %s=\"%s\"\n", attrname, attrvalue); +} ``` -Each `...` 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: +CDATA \(`MXML_TYPE_CDATA`) nodes have an associated string value consisting of +the text between the `` delimiters. The [mxmlGetCDATA](@@) +function retrieves the CDATA string pointer for a node. For example, the +following code gets the CDATA string value: ```c -node = mxmlNewElement(data, "node"); -mxmlNewText(node, false, "val1"); +const char *cdatavalue = mxmlGetCDATA(node); ``` -The resulting in-memory XML document can then be saved or processed just like -one loaded from disk or a string. +Comment \(`MXML_TYPE_COMMENT`) nodes have an associated string value consisting +of the text between the `` delimiters. The [mxmlGetComment](@@) +function retrieves the comment string pointer for a node. For example, the +following code gets the comment string value: + +```c +const char *commentvalue = mxmlGetComment(node); +``` + +Processing instruction \(`MXML_TYPE_DIRECTIVE`) nodes have an associated string +value consisting of the text between the `` delimiters. The +[mxmlGetDirective](@@) function retrieves the processing instruction string +for a node. For example, the following code gets the processing instruction +string value: + +```c +const char *instrvalue = mxmlGetDirective(node); +``` + +Integer \(`MXML_TYPE_INTEGER`) nodes have an associated `long` value. The +[mxmlGetInteger](@@) function retrieves the integer value for a node. For +example, the following code gets the integer value: + +```c +long intvalue = mxmlGetInteger(node); +``` + +Opaque string \(`MXML_TYPE_OPAQUE`) nodes have an associated string value +consisting of the text between elements. The [mxmlGetOpaque](@@) function +retrieves the opaque string pointer for a node. For example, the following +code gets the opaque string value: + +```c +const char *opaquevalue = mxmlGetOpaque(node); +``` + +Real number \(`MXML_TYPE_REAL`) nodes have an associated `double` value. The +[mxmlGetReal](@@) function retrieves the real number for a node. For example, +the following code gets the real value: + +```c +double realvalue = mxmlGetReal(node); +``` + +Whitespace-delimited text string \(`MXML_TYPE_TEXT`) nodes have an associated +whitespace indicator and string value extracted from the text between elements. +The [mxmlGetText](@@) function retrieves the text string pointer and whitespace +boolean value for a node. For example, the following code gets the text and +whitespace indicator: + +```c +const char *textvalue; +bool whitespace; + +textvalue = mxmlGetText(node, &whitespace); +``` Saving an XML File @@ -420,73 +482,68 @@ You save an XML file using the [mxmlSaveFilename](@@) function: ```c bool -mxmlSaveFilename(mxml_node_t *node, const char *filename, - mxml_save_cb_t cb, void *cbdata); +mxmlSaveFilename(mxml_node_t *node, mxml_options_t *options, + const char *filename); ``` -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 -mxmlSaveFile(xml, "filename.xml", /*cb*/NULL, /*cbdata*/NULL); -``` - -Mini-XML also provides functions to save to a file descriptor, `FILE` pointer, -or strings: +Mini-XML also provides functions to save to a `FILE` pointer, a file descriptor, +a string, or using a callback: ```c char * -mxmlSaveAllocString(mxml_node_t *node, mxml_save_cb_t cb, - void *cbdata); +mxmlSaveAllocString(mxml_node_t *node, mxml_options_t *options); bool -mxmlSaveFd(mxml_node_t *node, int fd, mxml_save_cb_t cb, - void *cbdata); +mxmlSaveFd(mxml_node_t *node, mxml_options_t *options, + int fd); bool -mxmlSaveFile(mxml_node_t *node, FILE *fp, mxml_save_cb_t cb, - void *cbdata); +mxmlSaveFile(mxml_node_t *node, mxml_options_t *options, + FILE *fp); + +bool +mxmlSaveIO(mxml_node_t *node, mxml_options_t *options, + mxml_io_cb_t *io_cb, void *io_cbdata); size_t -mxmlSaveString(mxml_node_t *node, char *buffer, size_t bufsize, - mxml_save_cb_t cb, void *cbdata); +mxmlSaveString(mxml_node_t *node, mxml_options_t *options, + char *buffer, size_t bufsize); ``` - -### 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: +Each accepts a pointer to the top-most ("root") node, any save options, and (as +needed) the destination. For example, the following code saves an XML file to +the file "example.xml" with the default options: ```c -void mxmlSetWrapMargin(int column); +mxmlSaveFile(xml, /*options*/NULL, "example.xml"); ``` -For example, the following code sets the margin to 132 columns: + +### Save Options + +Save options are specified using a `mxml_options_t` pointer, which you create +using the [mxmlOptionsNew](@@) function: ```c -mxmlSetWrapMargin(132); +mxml_options_t *options = mxmlOptionsNew(); ``` -while the following code disables wrapping by setting the margin to 0: +The default save options will wrap output lines at column 72 but not add any +additional whitespace otherwise. You can change the wrap column using the +[mxmlOptionsSetWrapMargin](@@) function. For example, the following will set +the wrap column to 0 which disables wrapping: ```c -mxmlSetWrapMargin(0); +mxmlOptionsSetWrapMargin(options, 0); ``` - -### Save Callbacks - -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. +To add additional whitespace to the output, set a whitespace callback using the +[mxmlOptionsSetWhitespaceCallback](@@) function. A whitespace callback accepts +a callback data pointer, the current node, and a whitespace position value of +`MXML_WS_BEFORE_OPEN`, `MXML_WS_AFTER_OPEN`, `MXML_WS_BEFORE_CLOSE`, or +`MXML_WS_AFTER_CLOSE`. The callback should return `NULL` if no whitespace +is to be inserted or a string of spaces, tabs, carriage returns, and newlines to +insert otherwise. The following whitespace callback can be used to add whitespace to XHTML output to make it more readable in a standard text editor: @@ -557,39 +614,93 @@ whitespace_cb(void *cbdata, mxml_node_t *node, mxml_ws_t where) } ``` -To use this callback function, simply use the name when you call any of the save -functions: +The following code will set the whitespace callback for the save options: ```c -FILE *fp; -mxml_node_t *tree; - -fp = fopen("filename.xml", "w"); -mxmlSaveFile(tree, fp, whitespace_cb, /*cbdata*/NULL); -fclose(fp); +mxmlOptionsSetWhitespaceCallback(options, whitespace_cb, /*cbdata*/NULL); ``` -Memory Management ------------------ +Freeing Memory +-------------- 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: +free the memory that is used for a particular node and its children. For +example, the following code frees the XML data loaded by the previous examples: ```c -void -mxmlDelete(mxml_node_t *tree); +mxmlDelete(xml); ``` -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`. +Creating New XML Documents +========================== + +You can create new and update existing XML documents in memory using the various +mxmlNewXxx functions. The following code will create the XML document described +in the [Using Mini-XML](@) chapter: + +```c +mxml_node_t *xml; /* */ +mxml_node_t *data; /* */ +mxml_node_t *node; /* */ +mxml_node_t *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 processing instruction node common to all XML files +using the [mxmlNewXML](@@) function: + +```c +xml = mxmlNewXML("1.0"); +``` + +We then create the `` 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`): + +```c +data = mxmlNewElement(xml, "data"); +``` + +Each `...` 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"); +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. -More About Nodes -================ Element Nodes ------------- @@ -615,32 +726,13 @@ 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: - -```c -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 Nodes ----------- CDATA \(`MXML_TYPE_CDATA`) nodes are created using the [mxmlNewCDATA](@@) -and [mxmlNewCDATAf](@@) functions: +and [mxmlNewCDATAf](@@) functions and set using the [mxmlSetCDATA](@@) and +[mxmlSetCDATAf](@@) functions: ```c mxml_node_t * @@ -648,13 +740,12 @@ 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: +void +mxmlSetCDATA(mxml_node_t *node, const char *string); -```c -const char * -mxmlGetCDATA(mxml_node_t *node); +void +mxmlSetCDATAf(mxml_node_t *node, const char *format, ...); ``` @@ -662,20 +753,21 @@ Comment Nodes ------------- Comment \(`MXML_TYPE_COMMENT`) nodes are created using the [mxmlNewComment](@@) -and [mxmlNewCommentf](@@) functions, for example: +and [mxmlNewCommentf](@@) functions and set using the [mxmlSetComment](@@) +and [mxmlSetCommentf](@@) functions: ```c -mxml_node_t *node = mxmlNewComment(" This is a comment "); +mxml_node_t * +mxmlNewComment(mxml_node_t *parent, const char *string); -mxml_node_t *node = mxmlNewCommentf(" This is comment %d ", 42); -``` +mxml_node_t * +mxmlNewCommentf(mxml_node_t *parent, const char *format, ...); -Similarly, the [mxmlGetComment](@@) function retrieves the comment string -pointer for a node: +void +mxmlSetComment(mxml_node_t *node, const char *string); -```c -const char *comment = mxmlGetComment(node); -/* returns " This is a comment " */ +void +mxmlSetCommentf(mxml_node_t *node, const char *format, ...); ``` @@ -683,7 +775,8 @@ Processing Instruction Nodes ---------------------------- Processing instruction \(`MXML_TYPE_DIRECTIVE`) nodes are created using the -[mxmlNewDirective](@@) and [mxmlNewDirectivef](@@) functions: +[mxmlNewDirective](@@) and [mxmlNewDirectivef](@@) functions and set using the +[mxmlSetDirective](@@) and [mxmlSetDirectivef](@@) functions: ```c mxml_node_t *node = mxmlNewDirective("xml-stylesheet type=\"text/css\" href=\"style.css\""); @@ -691,14 +784,6 @@ mxml_node_t *node = mxmlNewDirective("xml-stylesheet type=\"text/css\" href=\"st mxml_node_t *node = mxmlNewDirectivef("xml version=\"%s\"", version); ``` -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: @@ -712,19 +797,14 @@ Integer Nodes ------------- Integer \(`MXML_TYPE_INTEGER`) nodes are created using the [mxmlNewInteger](@@) -function: +function and set using the [mxmlSetInteger](@@) function: ```c mxml_node_t * mxmlNewInteger(mxml_node_t *parent, long integer); -``` -The [mxmlGetInteger](@@) function retrieves the integer value for a node: - - -```c -long -mxmlGetInteger(mxml_node_t *node); +void +mxmlSetInteger(mxml_node_t *node, long integer); ``` @@ -732,7 +812,8 @@ Opaque String Nodes ------------------- Opaque string \(`MXML_TYPE_OPAQUE`) nodes are created using the -[mxmlNewOpaque](@@) and [mxmlNewOpaquef](@@) functions: +[mxmlNewOpaque](@@) and [mxmlNewOpaquef](@@) functions and set using the +[mxmlSetOpaque](@@) and [mxmlSetOpaquef](@@) functions: ```c mxml_node_t * @@ -740,13 +821,27 @@ mxmlNewOpaque(mxml_node_t *parent, const char *opaque); mxml_node_t * mxmlNewOpaquef(mxml_node_t *parent, const char *format, ...); + +void +mxmlSetOpaque(mxml_node_t *node, const char *opaque); + +void +mxmlSetOpaquef(mxml_node_t *node, const char *format, ...); ``` -The [mxmlGetOpaque](@@) function retrieves the opaque string pointer for a node: + +Real Number Nodes +----------------- + +Real number \(`MXML_TYPE_REAL`) nodes are created using the [mxmlNewReal](@@) +function and set using the [mxmlSetReal](@@) function: ```c -const char * -mxmlGetOpaque(mxml_node_t *node); +mxml_node_t * +mxmlNewReal(mxml_node_t *parent, double real); + +void +mxmlSetReal(mxml_node_t *node, double real); ``` @@ -754,7 +849,8 @@ Text Nodes ---------- Whitespace-delimited text string \(`MXML_TYPE_TEXT`) nodes are created using the -[mxmlNewText](@@) and [mxmlNewTextf](@@) functions. Each text node consists of +[mxmlNewText](@@) and [mxmlNewTextf](@@) functions and set using the +[mxmlSetText](@@) and [mxmlSetTextf](@@) functions. Each text node consists of a text string and (leading) whitespace boolean value. ```c @@ -765,130 +861,19 @@ mxmlNewText(mxml_node_t *parent, bool whitespace, 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: +void +mxmlSetText(mxml_node_t *node, bool whitespace, + const char *string); -```c -const char * -mxmlGetText(mxml_node_t *node, bool *whitespace); +void +mxmlSetTextf(mxml_node_t *node, bool whitespace, + const char *format, ...); ``` -Real Number Nodes --------------------- - -Real number \(`MXML_TYPE_REAL`) nodes are created using the [mxmlNewReal](@@) -function: - -```c -mxml_node_t * -mxmlNewReal(mxml_node_t *parent, double real); -``` - -The [mxmlGetReal](@@) function retrieves the real number for a node: - -```c -double -mxmlGetReal(mxml_node_t *node); -``` - - -Locating Data in an XML Document -================================ - -Mini-XML provides many functions for enumerating, searching, and indexing XML -documents. - - -Finding Nodes -------------- - -The [mxmlFindPath](@@) function finds the (first) value node under a specific -element using a "path": - -```c -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: - -```c -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: - -```c -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.: - -```c -/* 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: - -```c -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. +Iterating and Indexing the Tree +=============================== Iterating Nodes @@ -912,7 +897,7 @@ mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top, 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: +the sample XML document in the [Using Mini-XML](@) chapter: ```c mxml_node_t *node; @@ -1030,13 +1015,17 @@ mxmlIndexDelete(mxml_index_t *ind); ``` -Custom Data Types -================= +Advanced Usage +============== -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 + +Custom Data Types +----------------- + +Mini-XML supports custom data types via load and save callback options. +Only a single set of callbacks can be active at any time for a `mxml_options_t` +pointer, 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. @@ -1047,35 +1036,38 @@ 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: +function or using the custom load callback specified using the +[mxmlOptionsSetCustomCallbacks](@@) function: ```c -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 *); +typedef void (*mxml_custfree_cb_t)(void *cbdata, void *data); +typedef bool (*mxml_custload_cb_t)(void *cbdata, mxml_node_t *, const char *); +typedef char *(*mxml_custsave_cb_t)(void *cbdata, mxml_node_t *); mxml_node_t * mxmlNewCustom(mxml_node_t *parent, void *data, - mxml_custom_destroy_cb_t destroy); + mxml_custfree_cb_t free_cb, void *free_cbdata); int mxmlSetCustom(mxml_node_t *node, void *data, - mxml_custom_destroy_cb_t destroy); + mxml_custfree_cb_t free_cb, void *free_cbdata); void -mxmlSetCustomHandlers(mxml_custom_load_cb_t load, - mxml_custom_save_cb_t save); +mxmlOptionsSetCustomCallbacks(mxml_option_t *options, + mxml_custload_cb_t load_cb, + mxml_custsave_cb_t save_cb, + void *cbdata); ``` -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: +The load callback receives the callback data pointer, 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 8601 format), the load callback would look like +the following: ```c -typedef struct +typedef struct iso_date_time_s { unsigned year, /* Year */ month, /* Month */ @@ -1087,7 +1079,7 @@ typedef struct } iso_date_time_t; bool -load_custom(mxml_node_t *node, const char *data) +custom_load_cb(void *cbdata, mxml_node_t *node, const char *data) { iso_date_time_t *dt; struct tm tmdata; @@ -1149,11 +1141,10 @@ load_custom(mxml_node_t *node, const char *data) dt->unix = gmtime(&tmdata); /* - * Assign custom node data and destroy (free) function - * pointers... + * Assign custom node data and free callback function/data... */ - mxmlSetCustom(node, data, free); + mxmlSetCustom(node, data, custom_free_cb, cbdata); /* * Return with no errors... @@ -1176,7 +1167,7 @@ our ISO date/time type: ```c char * -save_custom(mxml_node_t *node) +custom_save_cb(void *cbdata, mxml_node_t *node) { char data[255]; iso_date_time_t *dt; @@ -1193,16 +1184,17 @@ save_custom(mxml_node_t *node) } ``` -You register the callback functions using the [mxmlSetCustomCallbacks](@@) -function: +You register these callback functions using the +[mxmlOptionsSetCustomCallbacks](@@) function: ```c -mxmlSetCustomCallbacks(load_custom, save_custom); +mxmlOptionsSetCustomCallbacks(options, custom_load_cb, + custom_save_cb, /*cbdata*/NULL); ``` SAX (Stream) Loading of Documents -================================= +--------------------------------- 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 @@ -1210,9 +1202,11 @@ 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: +The mxmlLoadXxx functions support a SAX option that is enabled by setting a +callback function and data pointer with the [mxmlOptionsSetSAXCallback](@@) +function. 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: ```c bool @@ -1310,11 +1304,14 @@ document from stdin and then shows the title and headings in the document would look like: ```c -mxml_node_t *doc, *title, *body, *heading; +mxml_options_t *options; +mxml_node_t *xml, *title, *body, *heading; -doc = mxmlLoadFd(/*top*/NULL, /*fd*/0, - /*load_cb*/NULL, /*load_cbdata*/NULL, - sax_cb, /*sax_cbdata*/NULL); +options = mxmlOptionsNew(); +mxmlOptionsSetSAXCallback(options, sax_cb, + /*cbdata*/NULL); + +xml = mxmlLoadFd(/*top*/NULL, options, /*fd*/0); title = mxmlFindElement(doc, doc, "title", NULL, NULL, MXML_DESCEND_ALL); @@ -1332,6 +1329,9 @@ if (body) heading = mxmlGetNextSibling(heading)) print_children(heading); } + +mxmlDelete(xml); +mxmlOptionsDelete(options); ``` The `print_children` function is: @@ -1361,15 +1361,146 @@ print_children(mxml_node_t *parent) ``` +User Data +--------- + +Each node has an associated user data pointer that can be used to store useful +information for your application. The memory used by the data pointer is *not* +managed by Mini-XML so it is up to you to free it as necessary. + +The [mxmlSetUserData](@@) function sets any user (application) data associated +with the node while the [mxmlGetUserData](@@) function gets any user +(application) data associated with the node: + +```c +void * +mxmlGetUserData(mxml_node_t *node); + +void +mxmlSetUserData(mxml_node_t *node, void *user_data); +``` + + +Memory Management +----------------- + +Nodes support reference counting to manage memory usage. The [mxmlRetain](@@) +and [mxmlRelease](@@) functions increment and decrement a node's reference +count, respectively. When the reference count goes to zero, [mxmlRelease](@@) +calls [mxmlDelete](@@) to actually free the memory used by the node tree. New +nodes start with a reference count of `1`. You can get a node's current +reference count using the [mxmlGetRefCount](@@) function. + +Strings can also support different kinds of memory management. The default is +to use the standard C library strdup and free functions. To use alternate an +alternate mechanism, call the [mxmlSetStringCallbacks](@@) function to set +string copy and free callbacks. The copy callback receives the callback data +pointer and the string to copy, and returns a new string that will persist for +the life of the XML data. The free callback receives the callback data pointer +and the copied string and potentially frees the memory used for it. For +example, the following code implements a simple string pool that eliminates +duplicate strings: + +```c +typedef struct string_pool_s +{ + size_t num_strings; // Number of strings + size_t alloc_strings; // Allocated strings + char **strings; // Array of strings +} string_pool_t; + +char * +copy_string(string_pool_t *pool, const char *s) +{ + size_t i; // Looping var + char *news; // Copy of string + + + // See if the string is already in the pool... + for (i = 0; i < pool->num_strings; i ++) + { + if (!strcmp(pool->strings[i], s)) + return (pool->strings[i]); + } + + // Not in the pool, add new string + if (pool->num_strings >= pool->alloc_strings) + { + // Expand the string pool... + char **temp; // New strings array + + temp = realloc(pool->strings, + (pool->alloc_strings + 32) * + sizeof(char *)); + + if (temp == NULL) + return (NULL); + + pool->alloc_strings += 32; + pool->strings = temp; + } + + if ((news = strdup(s)) != NULL) + pool->strings[pool->num_strings ++] = news; + + return (news); +} + +void +free_string(string_pool_t *pool, char *s) +{ + // Do nothing here... +} + +void +free_all_strings(string_pool_t *pool) +{ + size_t i; // Looping var + + + for (i = 0; i < pool->num_strings; i ++) + free(pool->strings[i]); + free(pool->strings); +} + +... + +// Setup the string pool... +string_pool_t pool = { 0, 0, NULL }; + +mxmlSetStringCallbacks((mxml_strcopy_cb_t)copy_string, + (mxml_strfree_cb_t)free_string, + &pool); + +// Load an XML file... +mxml_node_t *xml; + +xml = mxmlLoadFilename(/*top*/NULL, /*options*/NULL, + "example.xml"); + +// Process the XML file... +... + +// Free memory used by the XML file... +mxmlDelete(xml); + +// Free all strings in the pool... +free_all_strings(&pool); +``` + + Migrating from Mini-XML v3.x ============================ The following incompatible API changes were made in Mini-XML v4.0: +- Load and save callbacks and options are now managed using `mxml_options_t` + values. +- The mxmlSAXLoadXxx functions have been removed in favor of setting the SAX + callback function and data pointers of the `mxml_options_t` value prior to + calling the corresponding mxmlLoadXxx functions. - 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`. @@ -1378,8 +1509,6 @@ The following incompatible API changes were made in Mini-XML v4.0: - CDATA nodes ("``") 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](@@) @@ -1389,3 +1518,6 @@ The following incompatible API changes were made in Mini-XML v4.0: - Integer nodes (`MXML_TYPE_INTEGER`) now use the `long` type. - Text nodes (`MXML_TYPE_TEXT`) now use the `bool` type for the whitespace value. +- Custom node callbacks are now set using the + [mxmlOptionsSetCustomCallbacks](@@) function instead of the thread-global + mxmlSetCustomHandlers function. diff --git a/doc/mxml.3 b/doc/mxml.3 index 0fdfd7e..1dd6630 100644 --- a/doc/mxml.3 +++ b/doc/mxml.3 @@ -1,4 +1,4 @@ -.TH mxml 3 "Mini-XML API" "2024-03-16" "Mini-XML API" +.TH mxml 3 "Mini-XML API" "2024-03-20" "Mini-XML API" .SH NAME mxml \- Mini-XML API .SH INCLUDE FILE @@ -361,7 +361,7 @@ Get an attribute by index. .nf const char * mxmlElementGetAttrByIndex ( mxml_node_t *node, - int idx, + size_t idx, const char **name ); .fi @@ -410,50 +410,6 @@ void mxmlElementSetAttrf ( This function sets attribute \fBname\fR to the formatted value of \fBformat\fR for the element \fBnode\fR. If the named attribute already exists, the value of the attribute is replaced by the new formatted string value. -.SS mxmlEntityAddCallback -Add a callback to convert entities to Unicode. -.PP -.nf -bool mxmlEntityAddCallback ( - mxml_entity_cb_t cb, - void *cbdata -); -.fi -.PP -This function adds a callback to the current thread that converts named -XML character entities to Unicode characters. The callback function \fBcb\fR -accepts the callback data pointer \fBcbdata\fR and the entity name and returns a -Unicode character value or \fB-1\fR if the entity is not known. For example, the -following entity callback supports the "euro" entity: -.PP -\fB`\fRc -int my_entity_cb(void \fIcbdata, const char \fRname) -{ - if (!strcmp(name, "euro")) - return (0x20ac); - else - return (-1); -} -\fB`\fR -.SS mxmlEntityGetValue -Get the character corresponding to a named entity. -.PP -.nf -int mxmlEntityGetValue ( - const char *name -); -.fi -.PP -The entity name can also be a numeric constant. \fB-1\fR is returned if the -name is not known. -.SS mxmlEntityRemoveCallback -Remove a callback. -.PP -.nf -void mxmlEntityRemoveCallback ( - mxml_entity_cb_t cb -); -.fi .SS mxmlFindElement Find the named element. .PP @@ -782,181 +738,109 @@ Load a file descriptor into an XML node tree. .nf 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_options_t *options, + int fd ); .fi .PP This function loads the file descriptor \fBfd\fR into an XML node tree. The -nodes in the specified file are added to the specified node \fBtop\fR. If \fBNULL\fR -is provided, the XML file MUST be well-formed with a single parent processing -instruction node like \fB\fR at the start of the file. +nodes in the specified file are added to the specified node \fBtop\fR - if \fBNULL\fR +the XML file MUST be well-formed with a single parent processing instruction +node like \fB\fR at the start of the file. .PP -The load callback function \fBload_cb\fR is called to obtain the node type that -should be used for child nodes. If \fBNULL\fR, the \fBload_cbdata\fR argument points -to a \fBmmd_type_t\fR variable that specifies the value type or \fBMMD_TYPE_TEXT\fR -if that argument is also \fBNULL\fR. -.PP -The SAX callback function \fBsax_cb\fR and associated callback data \fBsax_cbdata\fR -are used to enable the Simple API for XML streaming mode. The callback is -called as the XML node tree is parsed. -.PP -Note: The most common programming error when using the Mini-XML library is -to load an XML file using the \fBMXML_TYPE_TEXT\fR node type, which returns -inline text as a series of whitespace-delimited words, instead of using the -\fBMXML_TYPE_OPAQUE\fR node type which returns the inline text as a single string -(including whitespace). +Load options are provides via the \fBoptions\fR argument. If \fBNULL\fR, all values +will be loaded into \fBMXML_TYPE_TEXT\fR nodes. Use the \fImxmlOptionsNew\fR +function to create options when loading XML data. .SS mxmlLoadFile Load a file into an XML node tree. .PP .nf 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_options_t *options, + FILE *fp ); .fi .PP This function loads the \fBFILE\fR pointer \fBfp\fR into an XML node tree. The -nodes in the specified file are added to the specified node \fBtop\fR. If \fBNULL\fR -is provided, the XML file MUST be well-formed with a single parent processing -instruction node like \fB\fR at the start of the file. +nodes in the specified file are added to the specified node \fBtop\fR - if \fBNULL\fR +the XML file MUST be well-formed with a single parent processing instruction +node like \fB\fR at the start of the file. .PP -The load callback function \fBload_cb\fR is called to obtain the node type that -should be used for child nodes. If \fBNULL\fR, the \fBload_cbdata\fR argument points -to a \fBmmd_type_t\fR variable that specifies the value type or \fBMMD_TYPE_TEXT\fR -if that argument is also \fBNULL\fR. -.PP -The SAX callback function \fBsax_cb\fR and associated callback data \fBsax_cbdata\fR -are used to enable the Simple API for XML streaming mode. The callback is -called as the XML node tree is parsed. -.PP -Note: The most common programming error when using the Mini-XML library is -to load an XML file using the \fBMXML_TYPE_TEXT\fR node type, which returns -inline text as a series of whitespace-delimited words, instead of using the -\fBMXML_TYPE_OPAQUE\fR node type which returns the inline text as a single string -(including whitespace). +Load options are provides via the \fBoptions\fR argument. If \fBNULL\fR, all values +will be loaded into \fBMXML_TYPE_TEXT\fR nodes. Use the \fImxmlOptionsNew\fR +function to create options when loading XML data. .SS mxmlLoadFilename Load a file into an XML node tree. .PP .nf 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 + mxml_options_t *options, + const char *filename ); .fi .PP This function loads the named file \fBfilename\fR into an XML node tree. The -nodes in the specified file are added to the specified node \fBtop\fR. If \fBNULL\fR -is provided, the XML file MUST be well-formed with a single parent processing -instruction node like \fB\fR at the start of the file. +nodes in the specified file are added to the specified node \fBtop\fR - if \fBNULL\fR +the XML file MUST be well-formed with a single parent processing instruction +node like \fB\fR at the start of the file. .PP -The load callback function \fBload_cb\fR is called to obtain the node type that -should be used for child nodes. If \fBNULL\fR, the \fBload_cbdata\fR argument points -to a \fBmmd_type_t\fR variable that specifies the value type or \fBMMD_TYPE_TEXT\fR -if that argument is also \fBNULL\fR. -.PP -The SAX callback function \fBsax_cb\fR and associated callback data \fBsax_cbdata\fR -are used to enable the Simple API for XML streaming mode. The callback is -called as the XML node tree is parsed. -.PP -Note: The most common programming error when using the Mini-XML library is -to load an XML file using the \fBMXML_TYPE_TEXT\fR node type, which returns -inline text as a series of whitespace-delimited words, instead of using the -\fBMXML_TYPE_OPAQUE\fR node type which returns the inline text as a single string -(including whitespace). +Load options are provides via the \fBoptions\fR argument. If \fBNULL\fR, all values +will be loaded into \fBMXML_TYPE_TEXT\fR nodes. Use the \fImxmlOptionsNew\fR +function to create options when loading XML data. .SS mxmlLoadIO Load an XML node tree using a read callback. .PP .nf 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 + mxml_options_t *options, + mxml_io_cb_t io_cb, + void *io_cbdata ); .fi .PP This function loads data into an XML node tree using a read callback. The -nodes in the specified file are added to the specified node \fBtop\fR. If \fBNULL\fR -is provided, the XML file MUST be well-formed with a single parent processing -instruction node like \fB\fR at the start of the file. +nodes in the specified file are added to the specified node \fBtop\fR - if \fBNULL\fR +the XML file MUST be well-formed with a single parent processing instruction +node like \fB\fR at the start of the file. .PP -The read callback function \fBread_cb\fR is called to read a number of bytes from -the source. The callback data pointer \fBread_cbdata\fR is passed to the read +Load options are provides via the \fBoptions\fR argument. If \fBNULL\fR, all values +will be loaded into \fBMXML_TYPE_TEXT\fR nodes. Use the \fImxmlOptionsNew\fR +function to create options when loading XML data. +.PP +The read callback function \fBio_cb\fR is called to read a number of bytes from +the source. The callback data pointer \fBio_cbdata\fR is passed to the read callback with a pointer to a buffer and the maximum number of bytes to read, for example: .PP \fB`\fRc -ssize_t my_read_cb(void \fIcbdata, void \fRbuffer, size_t bytes) +size_t my_io_cb(void \fIcbdata, void \fRbuffer, size_t bytes) { ... copy up to "bytes" bytes into buffer ... - ... return the number of bytes "read" or -1 on error ... + ... return the number of bytes "read" or 0 on error ... } -.nf - - The load callback function `load_cb` is called to obtain the node type that - should be used for child nodes. If `NULL`, the `load_cbdata` argument points - to a `mmd_type_t` variable that specifies the value type or `MMD_TYPE_TEXT` - if that argument is also `NULL`. - - The SAX callback function `sax_cb` and associated callback data `sax_cbdata` - are used to enable the Simple API for XML streaming mode. The callback is - called as the XML node tree is parsed. - - Note: The most common programming error when using the Mini-XML library is - to load an XML file using the `MXML_TYPE_TEXT` node type, which returns - inline text as a series of whitespace-delimited words, instead of using the - `MXML_TYPE_OPAQUE` node type which returns the inline text as a single string - (including whitespace). -.fi - +\fB`\fR .SS mxmlLoadString Load a string into an XML node tree. .PP .nf 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 + mxml_options_t *options, + const char *s ); .fi .PP This function loads the string into an XML node tree. The nodes in the -specified file are added to the specified node \fBtop\fR. If \fBNULL\fR is provided, -the XML file MUST be well-formed with a single parent processing instruction -node like \fB\fR at the start of the file. +specified file are added to the specified node \fBtop\fR - if \fBNULL\fR the XML file +MUST be well-formed with a single parent processing instruction node like +\fB\fR at the start of the file. .PP -The load callback function \fBload_cb\fR is called to obtain the node type that -should be used for child nodes. If \fBNULL\fR, the \fBload_cbdata\fR argument points -to a \fBmmd_type_t\fR variable that specifies the value type or \fBMMD_TYPE_TEXT\fR -if that argument is also \fBNULL\fR. -.PP -The SAX callback function \fBsax_cb\fR and associated callback data \fBsax_cbdata\fR -are used to enable the Simple API for XML streaming mode. The callback is -called as the XML node tree is parsed. -.PP -Note: The most common programming error when using the Mini-XML library is -to load an XML file using the \fBMXML_TYPE_TEXT\fR node type, which returns -inline text as a series of whitespace-delimited words, instead of using the -\fBMXML_TYPE_OPAQUE\fR node type which returns the inline text as a single string -(including whitespace). +Load options are provides via the \fBoptions\fR argument. If \fBNULL\fR, all values +will be loaded into \fBMXML_TYPE_TEXT\fR nodes. Use the \fImxmlOptionsNew\fR +function to create options when loading XML data. .SS mxmlNewCDATA Create a new CDATA node. .PP @@ -1023,14 +907,14 @@ Create a new custom data node. mxml_node_t * mxmlNewCustom ( mxml_node_t *parent, void *data, - mxml_custom_destroy_cb_t destroy + mxml_custfree_cb_t free_cb, + void *free_cbdata ); .fi .PP The new custom node is added to the end of the specified parent's child -list. The constant \fBMXML_NO_PARENT\fR can be used to specify that the new -element node has no parent. \fBNULL\fR can be passed when the data in the -node is not dynamically allocated or is separately managed. +list. The \fBfree_cb\fR argument specifies a function to call to free the custom +data when the node is deleted. .SS mxmlNewDeclaration Create a new declaraction node. .PP @@ -1201,6 +1085,359 @@ mxml_node_t * mxmlNewXML ( .PP The "version" argument specifies the version number to put in the ?xml directive node. If \fBNULL\fR, version "1.0" is assumed. +.SS mxmlOptionsDelete +Free load/save options. +.PP +.nf +void mxmlOptionsDelete ( + mxml_options_t *options +); +.fi +.SS mxmlOptionsNew +Allocate load/save options. +.PP +.nf +mxml_options_t * mxmlOptionsNew (void); +.fi +.PP +This function creates a new set of load/save options to use with the +\fImxmlLoadFd\fR, \fImxmlLoadFile\fR, \fImxmlLoadFilename\fR, +\fImxmlLoadIO\fR, \fImxmlLoadString\fR, \fImxmlSaveAllocString\fR, +\fImxmlSaveFd\fR, \fImxmlSaveFile\fR, \fImxmlSaveFilename\fR, +\fImxmlSaveIO\fR, and \fImxmlSaveString\fR functions. Options can be +reused for multiple calls to these functions and should be freed using the +\fImxmlOptionsDelete\fR function. +.PP +The default load/save options load values using the constant type +\fBMXML_TYPE_TEXT\fR and save XML data with a wrap margin of 72 columns. +The various \fBmxmlOptionsSet\fR functions are used to change the defaults, +for example: +.PP +\fB`\fRc +mxml_options_t \fIoptions = mxmlOptionsNew(); + +/\fR Load values as opaque strings */ +mxmlOptionsSetTypeValue(options, MXML_TYPE_OPAQUE); +.nf + + Note: The most common programming error when using the Mini-XML library is + to load an XML file using the `MXML_TYPE_TEXT` node type, which returns + inline text as a series of whitespace-delimited words, instead of using the + `MXML_TYPE_OPAQUE` node type which returns the inline text as a single string + (including whitespace). +.fi + +.SS mxmlOptionsSetCustomCallbacks +Set the custom data callbacks. +.PP +.nf +void mxmlOptionsSetCustomCallbacks ( + mxml_options_t *options, + mxml_custload_cb_t load_cb, + mxml_custsave_cb_t save_cb, + void *cbdata +); +.fi +.PP +This function sets the callbacks that are used for loading and saving custom +data types. The load callback \fBload_cb\fR accepts the callback data pointer +\fBcbdata\fR, a node pointer, and a data string and returns \fBtrue\fR on success and +\fBfalse\fR on error, for example: +.PP +\fB`\fRc +typedef struct +{ + unsigned year, /\fI Year \fR/ + month, /\fI Month \fR/ + day, /\fI Day \fR/ + hour, /\fI Hour \fR/ + minute, /\fI Minute \fR/ + second; /\fI Second \fR/ + time_t unix; /\fI UNIX time \fR/ +} iso_date_time_t; +.PP +void +my_custom_free_cb(void \fIcbdata, void \fRdata) +{ + free(data); +} +.PP +bool +my_custom_load_cb(void \fIcbdata, mxml_node_t \fRnode, const char \fIdata) +{ + iso_date_time_t \fRdt; + struct tm tmdata; +.PP + /\fI Allocate custom data structure ... \fR/ + dt = calloc(1, sizeof(iso_date_time_t)); +.PP + /\fI Parse the data string... \fR/ + if (sscanf(data, "%u-%u-%uT%u:%u:%uZ", &(dt->year), &(dt->month), + &(dt->day), &(dt->hour), &(dt->minute), &(dt->second)) != 6) + { + /\fI Unable to parse date and time numbers... \fR/ + free(dt); + return (false); + } +.PP + /\fI Range check values... \fR/ + if (dt->month +.URL 1 || dt- 1 || dt- +month > 12 || dt->day +.URL 1 || dt- 1 || dt- +day > 31 || + dt->hour +.URL 0 || dt- 0 || dt- +hour > 23 || dt->minute +.URL 0 || dt- 0 || dt- +minute > 59 || + dt->second +.URL 0 || dt- 0 || dt- +second > 60) + { + /\fI Date information is out of range... \fR/ + free(dt); + return (false); + } +.PP + /\fI Convert ISO time to UNIX time in seconds... \fR/ + 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; +.PP + dt->unix = gmtime(&tmdata); +.PP + /\fI Set custom data and free function... \fR/ + mxmlSetCustom(node, data, my_custom_free, /\fIcbdata\fR/NULL); +.PP + /\fI Return with no errors... \fR/ + return (true); +} +.nf + + The save callback `save_cb` accepts the callback data pointer `cbdata` and a + node pointer and returns a malloc'd string on success and `NULL` on error, + for example: + + ```c + char * + my_custom_save_cb(void *cbdata, mxml_node_t *node) + { + char data[255]; + iso_date_time_t *dt; + + /* Get the custom data structure */ + dt = (iso_date_time_t *)mxmlGetCustom(node); + + /* Generate string version of the date/time... */ + snprintf(data, sizeof(data), "%04u-%02u-%02uT%02u:%02u:%02uZ", + dt->year, dt->month, dt->day, dt->hour, dt->minute, dt->second); + + /* Duplicate the string and return... */ + return (strdup(data)); + } + +.fi + +.SS mxmlOptionsSetEntityCallback +Set the entity lookup callback to use when loading XML data. +.PP +.nf +void mxmlOptionsSetEntityCallback ( + mxml_options_t *options, + mxml_entity_cb_t cb, + void *cbdata +); +.fi +.PP +This function sets the callback that is used to lookup named XML character +entities when loading XML data. The callback function \fBcb\fR accepts the +callback data pointer \fBcbdata\fR and the entity name. The function returns a +Unicode character value or \fB-1\fR if the entity is not known. For example, the +following entity callback supports the "euro" entity: +.PP +\fB`\fRc +int my_entity_cb(void \fIcbdata, const char \fRname) +{ + if (!strcmp(name, "euro")) + return (0x20ac); + else + return (-1); +} +.nf + + Mini-XML automatically supports the "amp", "gt", "lt", and "quot" character + entities which are required by the base XML specification. +.fi + char *data) +{ + iso_date_time_t +.SS mxmlOptionsSetErrorCallback +Set the error message callback. +.PP +.nf +void mxmlOptionsSetErrorCallback ( + mxml_options_t *options, + mxml_error_cb_t cb, + void *cbdata +); +.fi +.PP +This function sets a function to use when reporting errors. The callback +\fBcb\fR accepts the data pointer \fBcbdata\fR and a string pointer containing the +error message: +.PP +\fB`\fRc +void my_error_cb(void \fIcbdata, const char \fRmessage) +{ + fprintf(stderr, "myprogram: %sn", message); +} +.nf + + The default error callback writes the error message to the `stderr` file. +.fi +ack supports the "euro" entity: +.PP +\fB`\fR +.SS mxmlOptionsSetSAXCallback +Set the SAX callback to use when reading XML data. +.PP +.nf +void mxmlOptionsSetSAXCallback ( + mxml_options_t *options, + mxml_sax_cb_t cb, + void *cbdata +); +.fi +.PP +This function sets a SAX callback to use when reading XML data. The SAX +callback function \fBcb\fR and associated callback data \fBcbdata\fR are used to +enable the Simple API for XML streaming mode. The callback is called as the +XML node tree is parsed and receives the \fBcbdata\fR pointer, the \fBmxml_node_t\fR +pointer, and an event code. The function returns \fBtrue\fR to continue +processing or \fBfalse\fR to stop: +.PP +\fB`\fRc +bool +sax_cb(void \fIcbdata, mxml_node_t \fRnode, + mxml_sax_event_t event) +{ + ... do something ... +.PP + /\fI Continue processing... \fR/ + return (true); +} +.nf + + 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 (``) + - `MXML_SAX_EVENT_ELEMENT_OPEN` - An open element was just read (``) + + 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. +.fi + /* Date information is out of range... +.SS mxmlOptionsSetTypeCallback +Set the type callback for child/value nodes. +.PP +.nf +void mxmlOptionsSetTypeCallback ( + mxml_options_t *options, + mxml_type_cb_t cb, + void *cbdata +); +.fi +.PP +The load callback function \fBcb\fR is called to obtain the node type child/value +nodes and receives the \fBcbdata\fR pointer and the \fBmxml_node_t\fR pointer, for +example: +.PP +\fB`\fRc +mxml_type_t +my_type_cb(void \fIcbdata, mxml_node_t \fRnode) +{ + const char \fItype; + + /\fR + \fI You can lookup attributes and/or use the element name, + \fR hierarchy, etc... + */ +.PP + type = mxmlElementGetAttr(node, "type"); + if (type == NULL) + type = mxmlGetElement(node); + if (type == NULL) + type = "text"; +.PP + 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); +} +\fB`\fR +.SS mxmlOptionsSetTypeValue +Set the type to use for all child/value nodes. +.PP +.nf +void mxmlOptionsSetTypeValue ( + mxml_options_t *options, + mxml_type_t type +); +.fi +.PP +This functions sets a constant node type to use for all child/value nodes. +.SS mxmlOptionsSetWhitespaceCallback +Set the whitespace callback. +.PP +.nf +void mxmlOptionsSetWhitespaceCallback ( + mxml_options_t *options, + mxml_ws_cb_t cb, + void *cbdata +); +.fi +.PP +This function sets the whitespace callback that is used when saving XML data. +The callback function \fBcb\fR specifies a function that returns a whitespace +string or \fBNULL\fR before and after each element. The function receives the +callback data pointer \fBcbdata\fR, the \fBmxml_node_t\fR pointer, and a "when" +value indicating where the whitespace is being added, for example: +.PP +\fB`\fRc +const char \fImy_whitespace_cb(void \fRcbdata, mxml_node_t *node, mxml_ws_t when) +{ + if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) + return ("n"); + else + return (NULL); +} +\fB`\fR +.SS mxmlOptionsSetWrapMargin +Set the wrap margin when saving XML data. +.PP +.nf +void mxmlOptionsSetWrapMargin ( + mxml_options_t *options, + int column +); +.fi +.PP +This function sets the wrap margin used when saving XML data. Wrapping is +disabled when \fBcolumn\fR is \fB0\fR. .SS mxmlRelease Release a node. .PP @@ -1237,8 +1474,7 @@ Save an XML tree to an allocated string. .nf char * mxmlSaveAllocString ( mxml_node_t *node, - mxml_save_cb_t save_cb, - void *save_cbdata + mxml_options_t *options ); .fi .PP @@ -1249,181 +1485,109 @@ should be freed using \fBfree\fR (or the string free callback set using \fBNULL\fR is returned if the node would produce an empty string or if the string cannot be allocated. .PP -The callback function \fBsave_cb\fR specifies a function that returns a -whitespace string or \fBNULL\fR before and after each element. The function -receives the callback data pointer \fBsave_cbdata\fR, the \fBmxml_node_t\fR pointer, -and a "when" value indicating where the whitespace is being added, for -example: -.PP -\fB`\fRc -const char \fImy_save_cb(void \fRcbdata, mxml_node_t *node, mxml_ws_t when) -{ - if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) - return ("n"); - else - return (NULL); -} -\fB`\fR +Save options are provides via the \fBoptions\fR argument. If \fBNULL\fR, the XML +output will be wrapped at column 72 with no additional whitespace. Use the +\fImxmlOptionsNew\fR function to create options for saving XML data. .SS mxmlSaveFd Save an XML tree to a file descriptor. .PP .nf bool mxmlSaveFd ( mxml_node_t *node, - int fd, - mxml_save_cb_t save_cb, - void *save_cbdata + mxml_options_t *options, + int fd ); .fi .PP This function saves the XML tree \fBnode\fR to a file descriptor. .PP -The callback function \fBsave_cb\fR specifies a function that returns a -whitespace string or \fBNULL\fR before and after each element. The function -receives the callback data pointer \fBsave_cbdata\fR, the \fBmxml_node_t\fR pointer, -and a "when" value indicating where the whitespace is being added, for -example: -.PP -\fB`\fRc -const char \fImy_save_cb(void \fRcbdata, mxml_node_t *node, mxml_ws_t when) -{ - if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) - return ("n"); - else - return (NULL); -} -\fB`\fR +Save options are provides via the \fBoptions\fR argument. If \fBNULL\fR, the XML +output will be wrapped at column 72 with no additional whitespace. Use the +\fImxmlOptionsNew\fR function to create options for saving XML data. .SS mxmlSaveFile Save an XML tree to a file. .PP .nf bool mxmlSaveFile ( mxml_node_t *node, - FILE *fp, - mxml_save_cb_t save_cb, - void *save_cbdata + mxml_options_t *options, + FILE *fp ); .fi .PP This function saves the XML tree \fBnode\fR to a stdio \fBFILE\fR. .PP -The callback function \fBsave_cb\fR specifies a function that returns a -whitespace string or \fBNULL\fR before and after each element. The function -receives the callback data pointer \fBsave_cbdata\fR, the \fBmxml_node_t\fR pointer, -and a "when" value indicating where the whitespace is being added, for -example: -.PP -\fB`\fRc -const char \fImy_save_cb(void \fRcbdata, mxml_node_t *node, mxml_ws_t when) -{ - if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) - return ("n"); - else - return (NULL); -} -\fB`\fR +Save options are provides via the \fBoptions\fR argument. If \fBNULL\fR, the XML +output will be wrapped at column 72 with no additional whitespace. Use the +\fImxmlOptionsNew\fR function to create options for saving XML data. .SS mxmlSaveFilename Save an XML tree to a file. .PP .nf bool mxmlSaveFilename ( mxml_node_t *node, - const char *filename, - mxml_save_cb_t save_cb, - void *save_cbdata + mxml_options_t *options, + const char *filename ); .fi .PP This function saves the XML tree \fBnode\fR to a named file. .PP -The callback function \fBsave_cb\fR specifies a function that returns a -whitespace string or \fBNULL\fR before and after each element. The function -receives the callback data pointer \fBsave_cbdata\fR, the \fBmxml_node_t\fR pointer, -and a "when" value indicating where the whitespace is being added, for -example: -.PP -\fB`\fRc -const char \fImy_save_cb(void \fRcbdata, mxml_node_t *node, mxml_ws_t when) -{ - if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) - return ("n"); - else - return (NULL); -} -\fB`\fR +Save options are provides via the \fBoptions\fR argument. If \fBNULL\fR, the XML +output will be wrapped at column 72 with no additional whitespace. Use the +\fImxmlOptionsNew\fR function to create options for saving XML data. .SS mxmlSaveIO Save an XML tree using a callback. .PP .nf bool mxmlSaveIO ( mxml_node_t *node, - mxml_write_cb_t write_cb, - void *write_cbdata, - mxml_save_cb_t save_cb, - void *save_cbdata + mxml_options_t *options, + mxml_io_cb_t io_cb, + void *io_cbdata ); .fi .PP This function saves the XML tree \fBnode\fR using a write callback function -\fBwrite_cb\fR. The write callback is called with the callback data pointer -\fBwrite_cbdata\fR, a buffer pointer, and the number of bytes to write, for +\fBio_cb\fR. The write callback is called with the callback data pointer +\fBio_cbdata\fR, a buffer pointer, and the number of bytes to write, for example: .PP \fB`\fRc -ssize_t my_write_cb(void \fIcbdata, const void \fRbuffer, size_t bytes) +size_t my_io_cb(void \fIcbdata, const void \fRbuffer, size_t bytes) { ... write/copy bytes from buffer to the output ... - ... return the number of bytes written/copied or -1 on error ... + ... return the number of bytes written/copied or 0 on error ... } .nf - The callback function `save_cb` specifies a function that returns a - whitespace string or `NULL` before and after each element. The function - receives the callback data pointer `save_cbdata`, the `mxml_node_t` pointer, - and a "when" value indicating where the whitespace is being added, for - example: - - ```c - const char *my_save_cb(void *cbdata, mxml_node_t *node, mxml_ws_t when) - { - if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) - return ("n"); - else - return (NULL); - } - + Save options are provides via the `options` argument. If `NULL`, the XML + output will be wrapped at column 72 with no additional whitespace. Use the + @link mxmlOptionsNew@ function to create options for saving XML data. .fi - +, "real")) + return (MXML_TYPE_REAL); + else + return (MXML_TYPE_TEXT); +} +\fB`\fR .SS mxmlSaveString Save an XML node tree to a string. .PP .nf size_t mxmlSaveString ( mxml_node_t *node, + mxml_options_t *options, char *buffer, - size_t bufsize, - mxml_save_cb_t save_cb, - void *save_cbdata + size_t bufsize ); .fi .PP -This function saves the XML tree \fBnode\fR to a string buffer. +This function saves the XML tree \fBnode\fR to a fixed-size string buffer. .PP -The callback function \fBsave_cb\fR specifies a function that returns a -whitespace string or \fBNULL\fR before and after each element. The function -receives the callback data pointer \fBsave_cbdata\fR, the \fBmxml_node_t\fR pointer, -and a "when" value indicating where the whitespace is being added, for -example: -.PP -\fB`\fRc -const char \fImy_save_cb(void \fRcbdata, mxml_node_t *node, mxml_ws_t when) -{ - if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) - return ("n"); - else - return (NULL); -} -\fB`\fR +Save options are provides via the \fBoptions\fR argument. If \fBNULL\fR, the XML +output will be wrapped at column 72 with no additional whitespace. Use the +\fImxmlOptionsNew\fR function to create options for saving XML data. .SS mxmlSetCDATA Set the data for a CDATA node. .PP @@ -1479,7 +1643,8 @@ Set the data and destructor of a custom data node. bool mxmlSetCustom ( mxml_node_t *node, void *data, - mxml_custom_destroy_cb_t destroy_cb + mxml_custfree_cb_t free_cb, + void *free_cbdata ); .fi .PP @@ -1630,17 +1795,6 @@ bool mxmlSetUserData ( void *data ); .fi -.SS mxmlSetWrapMargin -Set the wrap margin when saving XML data. -.PP -.nf -void mxmlSetWrapMargin ( - int column -); -.fi -.PP -This function sets the wrap margin used when saving XML data for the current -thread. Wrapping is disabled when \fBcolumn\fR is \fB0\fR. .SS mxmlWalkNext Walk to the next logical node in the tree. .PP @@ -1676,23 +1830,23 @@ The \fBtop\fR argument constrains the walk to that node's children. .nf typedef enum mxml_add_e mxml_add_t; .fi -.SS mxml_custom_destroy_cb_t +.SS mxml_custfree_cb_t Custom data destructor .PP .nf -typedef void(*)(void *) mxml_custom_destroy_cb_t; +typedef void(*)(void *cbdata void *custdata) mxml_custfree_cb_t; .fi -.SS mxml_custom_load_cb_t +.SS mxml_custload_cb_t Custom data load callback function .PP .nf -typedef bool(*)(void *cbdata mxml_node_t *node const char *s) mxml_custom_load_cb_t; +typedef bool(*)(void *cbdata mxml_node_t *node const char *s) mxml_custload_cb_t; .fi -.SS mxml_custom_save_cb_t +.SS mxml_custsave_cb_t Custom data save callback function .PP .nf -typedef char *(*)(void *cbdata mxml_node_t *node) mxml_custom_save_cb_t; +typedef char *(*)(void *cbdata mxml_node_t *node) mxml_custsave_cb_t; .fi .SS mxml_descend_t \fImxmlFindElement\fR, \fImxmlWalkNext\fR, and \fImxmlWalkPrev\fR descend values @@ -1713,34 +1867,28 @@ Error callback function typedef void(*)(void *cbdata const char *message) mxml_error_cb_t; .fi .SS mxml_index_t -An XML node index. +An XML node index .PP .nf typedef struct _mxml_index_s mxml_index_t; .fi -.SS mxml_load_cb_t -Load callback function +.SS mxml_io_cb_t +Read/write callback function .PP .nf -typedef mxml_type_t(*)(void *cbdata mxml_node_t *node) mxml_load_cb_t; +typedef size_t(*)(void *cbdata void *buffer size_t bytes) mxml_io_cb_t; .fi .SS mxml_node_t -An XML node. +An XML node .PP .nf typedef struct _mxml_node_s mxml_node_t; .fi -.SS mxml_read_cb_t -Read callback function +.SS mxml_options_t +XML options .PP .nf -typedef ssize_t(*)(void *cbdata void *buffer size_t bytes) mxml_read_cb_t; -.fi -.SS mxml_save_cb_t -Save callback function -.PP -.nf -typedef const char *(*)(void *cbdata mxml_node_t *node mxml_ws_t when) mxml_save_cb_t; +typedef struct _mxml_options_s mxml_options_t; .fi .SS mxml_sax_cb_t SAX callback function @@ -1766,17 +1914,23 @@ String free callback .nf typedef void(*)(void *cbdata char *s) mxml_strfree_cb_t; .fi +.SS mxml_type_cb_t +Type callback function +.PP +.nf +typedef mxml_type_t(*)(void *cbdata mxml_node_t *node) mxml_type_cb_t; +.fi .SS mxml_type_t The XML node type. .PP .nf typedef enum mxml_type_e mxml_type_t; .fi -.SS mxml_write_cb_t -Write callback function +.SS mxml_ws_cb_t +Whitespace callback function .PP .nf -typedef ssize_t(*)(void *cbdata const void *buffer size_t bytes) mxml_write_cb_t; +typedef const char *(*)(void *cbdata mxml_node_t *node mxml_ws_t when) mxml_ws_cb_t; .fi .SS mxml_ws_t Whitespace periods diff --git a/doc/mxml.epub b/doc/mxml.epub index 99721bc..b21df93 100644 Binary files a/doc/mxml.epub and b/doc/mxml.epub differ diff --git a/doc/mxml.html b/doc/mxml.html index e6dd81f..7eaa6c2 100644 --- a/doc/mxml.html +++ b/doc/mxml.html @@ -85,6 +85,12 @@ blockquote { padding: 10px; page-break-inside: avoid; } +blockquote :first-child { + margin-top: 0; +} +blockquote :first-child { + margin-bottom: 0; +} p code, li code, p.code, pre, ul.code li { font-family: monospace; hyphens: manual; @@ -258,29 +264,33 @@ span.string {
  • Legal Stuff
  • Using Mini-XML
  • -
  • More About Nodes
      +
    • Creating New XML Documents
    • -
    • Locating Data in an XML Document
        -
      • Finding Nodes
      • +
      • Iterating and Indexing the Tree
      • +
      • Advanced Usage
      • Migrating from Mini-XML v3.x
      • Functions
      • Data Types
      • Enumerations
          @@ -420,6 +436,8 @@ span.string {
        • "Find" and "walk" functions for easily locating and navigating trees of data.

        • +
        • Support for custom string memory management functions to implement string pools and other schemes for reducing memory usage.

          +

        Mini-XML doesn't do validation or other types of processing on the data based upon schema files or other sources of definition information.

        History

        @@ -432,7 +450,7 @@ span.string {

        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.

        +

        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,491 lines of code, compared to 175,808 lines of code for libxml2 version 2.11.7.

        Resources

        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.

        @@ -451,41 +469,102 @@ span.string {

        Note: The library name "mxml4" is a configure-time option. If you use the --disable-libmxml4-prefix configure option the library is named "mxml".

        +

        API Basics

        +

        Every piece of information in an XML file is stored in memory in "nodes". Nodes are represented by mxml_node_t pointers. Each node has an associated type, value(s), a parent node, sibling nodes (previous and next), potentially first and last child nodes, and an optional user data pointer.

        +

        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 which is represented as a mxml_type_t enumeration value:

        +
          +
        • MXML_TYPE_CDATA: CDATA such as <![CDATA[...]]>,

          +
        • +
        • MXML_TYPE_COMMENT: A comment such as <!-- my 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" encoding="utf-8"?>,

          +
        • +
        • MXML_TYPE_ELEMENT: An XML element with optional attributes such as <element name="value">,

          +
        • +
        • MXML_TYPE_INTEGER: A whitespace-delimited integer value such as 42,

          +
        • +
        • MXML_TYPE_OPAQUE: An opaque string value that preserves all whitespace such as All work and no play makes Johnny a dull boy.,

          +
        • +
        • MXML_TYPE_REAL: A whitespace-delimited floating point value such as 123.4, or

          +
        • +
        • MXML_TYPE_TEXT: A whitespace-delimited text (fragment) value such as Word.

          +
        • +
        +

        The parent, sibling, and child nodes are accessed using the mxmlGetParent, mxmlGetNextSibling, mxmlGetPreviousSibling, mxmlGetFirstChild, and mxmlGetLastChild functions.

        +

        The value(s) of a node are accessed using the mxmlGetCDATA, mxmlGetComment, mxmlGetDeclaration, mxmlGetDirective, mxmlGetElement, mxmlElementGetAttr, mxmlGetInteger, mxmlGetOpaque, mxmlGetReal, and mxmlGetText functions.

        Loading an XML File

        -

        You load an XML file using the mxmlLoadFile function:

        +

        You load an XML file using the mxmlLoadFilename 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);
        +mxmlLoadFilename(mxml_node_t *top, mxml_options_t *options,
        +                 const char *filename);
         
        -

        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:

        +

        Mini-XML also provides functions to load from a FILE pointer, a file descriptor, a string, or using a callback:

        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);
        +mxmlLoadFd(mxml_node_t *top, mxml_options_t *options,
        +           int fd);
         
         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);
        +mxmlLoadFile(mxml_node_t *top, mxml_options_t *options,
        +             FILE *fp);
         
         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);
        +mxmlLoadIO(mxml_node_t *top, mxml_options_t *options,
        +           mxml_io_cb_t io_cb, void *io_cbdata);
        +
        +mxml_node_t *
        +mxmlLoadString(mxml_node_t *top, mxml_options_t *options,
        +               const char *s);
         
        -

        Load Callbacks

        -

        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:

        +

        Each accepts a pointer to the top-most ("root") node (usually NULL) you want to add the XML data to, any load options, and the content to be loaded. For example, the following code will load an XML file called "example.xml" using the default load options:

        +
        mxml_node_t *xml;
        +
        +xml = mxmlLoadFilename(/*top*/NULL, /*options*/NULL,
        +                       "example.xml");
        +
        +

        Load Options

        +

        Load options are specified using a mxml_options_t pointer, which you create using the mxmlOptionsNew function:

        +
        mxml_options_t *options = mxmlOptionsNew();
        +
        +

        The default load options will treat any values in your XML as whitespace- delimited text (MXML_TYPE_TEXT). You can specify a different type of values using the mxmlOptionsSetTypeValue function. For example, the following will specify that values are opaque text strings, including whitespace (MXML_TYPE_OPAQUE):

        +
        mxmlOptionsSetTypeValue(options, MXML_TYPE_OPAQUE);
        +
        +

        For more complex XML documents, you can specify a callback that returns the type of value for a given element node using the mxmlOptionsSetTypeCallback function. For example, to specify a callback function called my_type_cb that has no callback data:

        +
        mxmlOptionsSetTypeValue(options, my_type_cb, /*cbdata*/NULL);
        +
        +

        The my_type_cb function accepts the callback data pointer (NULL in this case) and the mxml_node_t pointer for the current element and returns a mxml_type_t enumeration value specifying the value type for child nodes. For example, the following function looks at the "type" attribute and the element name to determine the value types of the node's children:

        mxml_type_t
         my_load_cb(void *cbdata, mxml_node_t *node)
         {
        @@ -512,171 +591,149 @@ my_load_cb(void *cbdata, mxml_node_t *node)
             return (MXML_TYPE_TEXT);
         }
         
        -

        To use this callback function, simply specify it when you call any of the load functions:

        -
        mxml_node_t *tree;
        +

        Finding Nodes

        +

        The mxmlFindPath function finds the (first) value node under a specific element using a 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 *directnode = mxmlFindPath(xml, "data/group/node");
         
        -tree = mxmlLoadFilename(/*top*/NULL, "filename.xml",
        -                        my_load_cb, /*load_cbdata*/NULL,
        -                        /*sax_cb*/NULL, /*sax_cbdata*/NULL);
        +mxml_node_t *wildnode = mxmlFindPath(xml, "data/*/node");
         
        -

        Nodes

        -

        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 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 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
        +

        The element, attr, and value arguments can be passed as NULL to act as wildcards, e.g.:

        +
        mxml_node_t *node;
        +
        +/* 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);
         
        -

        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);
        +

        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 previous examples) can be one of three constants:

          -
        • MXML_TYPE_CDATA : CDATA,

          +
        • 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_TYPE_COMMENT : A comment,

          +
        • 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_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.

          +
        • MXML_DESCEND_ALL: search child nodes first, then sibling nodes, and then parent nodes.

        -

        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);
        +

        Getting the Value(s) from Nodes

        +

        Once you have the node you can use one of the mxmlGetXxx functions to retrieve its value(s).

        +

        Element (MXML_TYPE_ELEMENT) nodes have an associated name and zero or more named attributes with (string) values. The mxmlGetElement function retrieves the element name while the mxmlElementGetAttr function retrieves the value string for a named attribute. For example, the following code looks for HTML heading elements and, when found, displays the "id" attribute for the heading:

        +
        const char *elemname = mxmlGetElement(node);
        +const char *id_value = mxmlElementGetAttr(node, "id");
         
        -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);
        +if ((*elemname == 'h' || *elemname == 'H') &&
        +    elemname[1] >= '1' && elemname[1] <= '6' &&
        +    id_value != NULL)
        +  printf("%s: %s\n", elemname, id_value);
         
        -

        The mxmlGetUserData function gets any user (application) data associated with the node:

        -
        void *
        -mxmlGetUserData(mxml_node_t *node);
        +

        The mxmlElementGetAttrByIndex and mxmlElementGetAttrCount functions allow you to iterate all attributes of an element. For example, the following code prints the element name and each of its attributes:

        +
        const char *elemname = mxmlGetElement(node);
        +printf("%s:\n", elemname);
        +
        +size_t i, count;
        +for (i = 0, count = mxmlElementGetAttrCount(node); i < count; i ++)
        +{
        +  const char *attrname, *attrvalue;
        +
        +  attrvalue = mxmlElementGetAttrByIndex(node, i, &attrname);
        +
        +  printf("    %s=\"%s\"\n", attrname, attrvalue);
        +}
         
        -

        Creating XML Documents

        -

        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");
        +

        CDATA (MXML_TYPE_CDATA) nodes have an associated string value consisting of the text between the <![CDATA[ and ]]> delimiters. The mxmlGetCDATA function retrieves the CDATA string pointer for a node. For example, the following code gets the CDATA string value:

        +
        const char *cdatavalue = mxmlGetCDATA(node);
         
        -

        We start by creating the declaration node common to all XML files using the mxmlNewXML function:

        -
        xml = mxmlNewXML("1.0");
        +

        Comment (MXML_TYPE_COMMENT) nodes have an associated string value consisting of the text between the <!-- and --> delimiters. The mxmlGetComment function retrieves the comment string pointer for a node. For example, the following code gets the comment string value:

        +
        const char *commentvalue = mxmlGetComment(node);
         
        -

        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");
        +

        Processing instruction (MXML_TYPE_DIRECTIVE) nodes have an associated string value consisting of the text between the <? and ?> delimiters. The mxmlGetDirective function retrieves the processing instruction string for a node. For example, the following code gets the processing instruction string value:

        +
        const char *instrvalue = mxmlGetDirective(node);
         
        -

        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");
        +

        Integer (MXML_TYPE_INTEGER) nodes have an associated long value. The mxmlGetInteger function retrieves the integer value for a node. For example, the following code gets the integer value:

        +
        long intvalue = mxmlGetInteger(node);
        +
        +

        Opaque string (MXML_TYPE_OPAQUE) nodes have an associated string value consisting of the text between elements. The mxmlGetOpaque function retrieves the opaque string pointer for a node. For example, the following code gets the opaque string value:

        +
        const char *opaquevalue = mxmlGetOpaque(node);
        +
        +

        Real number (MXML_TYPE_REAL) nodes have an associated double value. The mxmlGetReal function retrieves the real number for a node. For example, the following code gets the real value:

        +
        double realvalue = mxmlGetReal(node);
        +
        +

        Whitespace-delimited text string (MXML_TYPE_TEXT) nodes have an associated whitespace indicator and string value extracted from the text between elements. The mxmlGetText function retrieves the text string pointer and whitespace boolean value for a node. For example, the following code gets the text and whitespace indicator:

        +
        const char *textvalue;
        +bool whitespace;
        +
        +textvalue = mxmlGetText(node, &whitespace);
         
        -

        The resulting in-memory XML document can then be saved or processed just like one loaded from disk or a string.

        Saving an XML File

        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);
        +mxmlSaveFilename(mxml_node_t *node, mxml_options_t *options,
        +                 const char *filename);
         
        -

        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:

        +

        Mini-XML also provides functions to save to a FILE pointer, a file descriptor, a string, or using a callback:

        char *
        -mxmlSaveAllocString(mxml_node_t *node, mxml_save_cb_t cb,
        -                    void *cbdata);
        +mxmlSaveAllocString(mxml_node_t *node, mxml_options_t *options);
         
         bool
        -mxmlSaveFd(mxml_node_t *node, int fd, mxml_save_cb_t cb,
        -           void *cbdata);
        +mxmlSaveFd(mxml_node_t *node, mxml_options_t *options,
        +           int fd);
         
         bool
        -mxmlSaveFile(mxml_node_t *node, FILE *fp, mxml_save_cb_t cb,
        -             void *cbdata);
        +mxmlSaveFile(mxml_node_t *node, mxml_options_t *options,
        +             FILE *fp);
        +
        +bool
        +mxmlSaveIO(mxml_node_t *node, mxml_options_t *options,
        +           mxml_io_cb_t *io_cb, void *io_cbdata);
         
         size_t
        -mxmlSaveString(mxml_node_t *node, char *buffer, size_t bufsize,
        -               mxml_save_cb_t cb, void *cbdata);
        +mxmlSaveString(mxml_node_t *node, mxml_options_t *options,
        +               char *buffer, size_t bufsize);
         
        -

        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:

        -
        void mxmlSetWrapMargin(int column);
        +

        Each accepts a pointer to the top-most ("root") node, any save options, and (as needed) the destination. For example, the following code saves an XML file to the file "example.xml" with the default options:

        +
        mxmlSaveFile(xml, /*options*/NULL, "example.xml");
         
        -

        For example, the following code sets the margin to 132 columns:

        -
        mxmlSetWrapMargin(132);
        +

        Save Options

        +

        Save options are specified using a mxml_options_t pointer, which you create using the mxmlOptionsNew function:

        +
        mxml_options_t *options = mxmlOptionsNew();
         
        -

        while the following code disables wrapping by setting the margin to 0:

        -
        mxmlSetWrapMargin(0);
        +

        The default save options will wrap output lines at column 72 but not add any additional whitespace otherwise. You can change the wrap column using the mxmlOptionsSetWrapMargin function. For example, the following will set the wrap column to 0 which disables wrapping:

        +
        mxmlOptionsSetWrapMargin(options, 0);
         
        -

        Save Callbacks

        -

        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.

        +

        To add additional whitespace to the output, set a whitespace callback using the mxmlOptionsSetWhitespaceCallback function. A whitespace callback accepts a callback data pointer, the current node, and a whitespace position value of MXML_WS_BEFORE_OPEN, MXML_WS_AFTER_OPEN, MXML_WS_BEFORE_CLOSE, or MXML_WS_AFTER_CLOSE. The callback should return NULL if no whitespace is to be inserted or a string of spaces, tabs, carriage returns, and newlines to insert 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)
        @@ -742,21 +799,56 @@ whitespace_cb(void *cbdata, mxml_node_t *node, mxm
           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;
        +

        The following code will set the whitespace callback for the save options:

        +
        mxmlOptionsSetWhitespaceCallback(options, whitespace_cb, /*cbdata*/NULL);
        +
        +

        Freeing Memory

        +

        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. For example, the following code frees the XML data loaded by the previous examples:

        +
        mxmlDelete(xml);
        +
        +

        Creating New XML Documents

        +

        You can create new and update existing XML documents in memory using the various mxmlNewXxx functions. The following code will create the XML document described in the Using Mini-XML chapter:

        +
        mxml_node_t *xml;    /* <?xml version="1.0" charset="utf-8"?> */
        +mxml_node_t *data;   /* <data> */
        +mxml_node_t *node;   /* <node> */
        +mxml_node_t *group;  /* <group> */
         
        -fp = fopen("filename.xml", "w");
        -mxmlSaveFile(tree, fp, whitespace_cb, /*cbdata*/NULL);
        -fclose(fp);
        +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");
         
        -

        Memory Management

        -

        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);
        +

        We start by creating the processing instruction node common to all XML files using the mxmlNewXML function:

        +
        xml = mxmlNewXML("1.0");
         
        -

        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

        +

        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.

        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 mxmlElementClearAttr function:

        mxml_node_t *
        @@ -773,79 +865,76 @@ mxmlElementSetAttr(mxml_node_t *node, const 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 Nodes

        -

        CDATA (MXML_TYPE_CDATA) nodes are created using the mxmlNewCDATA and mxmlNewCDATAf functions:

        +

        CDATA (MXML_TYPE_CDATA) nodes are created using the mxmlNewCDATA and mxmlNewCDATAf functions and set using the mxmlSetCDATA and mxmlSetCDATAf 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);
        +
        +void
        +mxmlSetCDATA(mxml_node_t *node, const char *string);
        +
        +void
        +mxmlSetCDATAf(mxml_node_t *node, const char *format, ...);
         

        Comment Nodes

        -

        Comment (MXML_TYPE_COMMENT) nodes are created using the mxmlNewComment and mxmlNewCommentf functions, for example:

        -
        mxml_node_t *node = mxmlNewComment(" This is a comment ");
        +

        Comment (MXML_TYPE_COMMENT) nodes are created using the mxmlNewComment and mxmlNewCommentf functions and set using the mxmlSetComment and mxmlSetCommentf functions:

        +
        mxml_node_t *
        +mxmlNewComment(mxml_node_t *parent, const char *string);
         
        -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 " */
        +mxml_node_t *
        +mxmlNewCommentf(mxml_node_t *parent, const char *format, ...);
        +
        +void
        +mxmlSetComment(mxml_node_t *node, const char *string);
        +
        +void
        +mxmlSetCommentf(mxml_node_t *node, const char *format, ...);
         

        Processing Instruction Nodes

        -

        Processing instruction (MXML_TYPE_DIRECTIVE) nodes are created using the mxmlNewDirective and mxmlNewDirectivef functions:

        +

        Processing instruction (MXML_TYPE_DIRECTIVE) nodes are created using the mxmlNewDirective and mxmlNewDirectivef functions and set using the mxmlSetDirective and mxmlSetDirectivef 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 Nodes

        -

        Integer (MXML_TYPE_INTEGER) nodes are created using the mxmlNewInteger function:

        +

        Integer (MXML_TYPE_INTEGER) nodes are created using the mxmlNewInteger function and set using the mxmlSetInteger 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);
        +
        +void
        +mxmlSetInteger(mxml_node_t *node, long integer);
         

        Opaque String Nodes

        -

        Opaque string (MXML_TYPE_OPAQUE) nodes are created using the mxmlNewOpaque and mxmlNewOpaquef functions:

        +

        Opaque string (MXML_TYPE_OPAQUE) nodes are created using the mxmlNewOpaque and mxmlNewOpaquef functions and set using the mxmlSetOpaque and mxmlSetOpaquef functions:

        mxml_node_t *
         mxmlNewOpaque(mxml_node_t *parent, const char *opaque);
         
         mxml_node_t *
         mxmlNewOpaquef(mxml_node_t *parent, const char *format, ...);
        +
        +void
        +mxmlSetOpaque(mxml_node_t *node, const char *opaque);
        +
        +void
        +mxmlSetOpaquef(mxml_node_t *node, const char *format, ...);
         
        -

        The mxmlGetOpaque function retrieves the opaque string pointer for a node:

        -
        const char *
        -mxmlGetOpaque(mxml_node_t *node);
        +

        Real Number Nodes

        +

        Real number (MXML_TYPE_REAL) nodes are created using the mxmlNewReal function and set using the mxmlSetReal function:

        +
        mxml_node_t *
        +mxmlNewReal(mxml_node_t *parent, double real);
        +
        +void
        +mxmlSetReal(mxml_node_t *node, double real);
         

        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.

        +

        Whitespace-delimited text string (MXML_TYPE_TEXT) nodes are created using the mxmlNewText and mxmlNewTextf functions and set using the mxmlSetText and mxmlSetTextf 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);
        @@ -853,81 +942,16 @@ mxmlNewText(mxml_node_t *parent, bool whitespace,
         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 Nodes

        -

        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);
        -
        -

        Locating Data in an XML Document

        -

        Mini-XML provides many functions for enumerating, searching, and indexing XML documents.

        -

        Finding Nodes

        -

        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");
        +void
        +mxmlSetText(mxml_node_t *node, bool whitespace,
        +            const char *string);
        +
        +void
        +mxmlSetTextf(mxml_node_t *node, bool whitespace,
        +             const char *format, ...);
         
        -

        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.

          -
        • -
        +

        Iterating and Indexing the Tree

        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:

        mxml_node_t *
        @@ -938,7 +962,7 @@ 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:

        +

        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 Using Mini-XML chapter:

        mxml_node_t *node;
         
         for (node = xml;
        @@ -1012,31 +1036,34 @@ mxmlIndexGetCount(mxml_index_t *ind);
         
        void
         mxmlIndexDelete(mxml_index_t *ind);
         
        -

        Custom Data Types

        -

        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.

        +

        Advanced Usage

        +

        Custom Data Types

        +

        Mini-XML supports custom data types via load and save callback options. Only a single set of callbacks can be active at any time for a mxml_options_t pointer, 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 *);
        +

        Custom (MXML_TYPE_CUSTOM) nodes are created using the mxmlNewCustom function or using the custom load callback specified using the mxmlOptionsSetCustomCallbacks function:

        +
        typedef void (*mxml_custfree_cb_t)(void *cbdata, void *data);
        +typedef bool (*mxml_custload_cb_t)(void *cbdata, mxml_node_t *, const char *);
        +typedef char *(*mxml_custsave_cb_t)(void *cbdata, mxml_node_t *);
         
         mxml_node_t *
         mxmlNewCustom(mxml_node_t *parent, void *data,
        -              mxml_custom_destroy_cb_t destroy);
        +              mxml_custfree_cb_t free_cb, void *free_cbdata);
         
         int
         mxmlSetCustom(mxml_node_t *node, void *data,
        -              mxml_custom_destroy_cb_t destroy);
        +              mxml_custfree_cb_t free_cb, void *free_cbdata);
         
         void
        -mxmlSetCustomHandlers(mxml_custom_load_cb_t load,
        -                      mxml_custom_save_cb_t save);
        +mxmlOptionsSetCustomCallbacks(mxml_option_t *options,
        +                              mxml_custload_cb_t load_cb,
        +                              mxml_custsave_cb_t save_cb,
        +                              void *cbdata);
         
        -

        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
        +

        The load callback receives the callback data pointer, 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 8601 format), the load callback would look like the following:

        +
        typedef struct iso_date_time_s
         {
           unsigned year,    /* Year */
                    month,   /* Month */
        @@ -1048,7 +1075,7 @@ mxmlSetCustomHandlers(mxml_custom_load_cb_t load,
         } iso_date_time_t;
         
         bool
        -load_custom(mxml_node_t *node, const char *data)
        +custom_load_cb(void *cbdata, mxml_node_t *node, const char *data)
         {
           iso_date_time_t *dt;
           struct tm tmdata;
        @@ -1110,11 +1137,10 @@ load_custom(mxml_node_t *node, const /*
        -  * Assign custom node data and destroy (free) function
        -  * pointers...
        +  * Assign custom node data and free callback function/data...
           */
         
        -  mxmlSetCustom(node, data, free);
        +  mxmlSetCustom(node, data, custom_free_cb, cbdata);
         
          /*
           * Return with no errors...
        @@ -1126,7 +1152,7 @@ load_custom(mxml_node_t *node, const char *
        -save_custom(mxml_node_t *node)
        +custom_save_cb(void *cbdata, mxml_node_t *node)
         {
           char data[255];
           iso_date_time_t *dt;
        @@ -1142,12 +1168,13 @@ save_custom(mxml_node_t *node)
           return (strdup(data));
         }
         
        -

        You register the callback functions using the mxmlSetCustomCallbacks function:

        -
        mxmlSetCustomCallbacks(load_custom, save_custom);
        +

        You register these callback functions using the mxmlOptionsSetCustomCallbacks function:

        +
        mxmlOptionsSetCustomCallbacks(options, custom_load_cb,
        +                              custom_save_cb, /*cbdata*/NULL);
         
        -

        SAX (Stream) Loading of Documents

        +

        SAX (Stream) Loading of Documents

        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:

        +

        The mxmlLoadXxx functions support a SAX option that is enabled by setting a callback function and data pointer with the mxmlOptionsSetSAXCallback function. 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)
        @@ -1231,11 +1258,14 @@ sax_cb(void *cbdata, mxml_node_t *node,
         }
         

        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;
        +
        mxml_options_t *options;
        +mxml_node_t *xml, *title, *body, *heading;
         
        -doc = mxmlLoadFd(/*top*/NULL, /*fd*/0,
        -                 /*load_cb*/NULL, /*load_cbdata*/NULL,
        -                 sax_cb, /*sax_cbdata*/NULL);
        +options = mxmlOptionsNew();
        +mxmlOptionsSetSAXCallback(options, sax_cb,
        +                          /*cbdata*/NULL);
        +
        +xml = mxmlLoadFd(/*top*/NULL, options, /*fd*/0);
         
         title = mxmlFindElement(doc, doc, "title", NULL, NULL,
                                 MXML_DESCEND_ALL);
        @@ -1253,6 +1283,9 @@ body = mxmlFindElement(doc, doc, "body", N
                heading = mxmlGetNextSibling(heading))
             print_children(heading);
         }
        +
        +mxmlDelete(xml);
        +mxmlOptionsDelete(options);
         

        The print_children function is:

        void
        @@ -1277,15 +1310,114 @@ print_children(mxml_node_t *parent)
           putchar('\n');
         }
         
        +

        User Data

        +

        Each node has an associated user data pointer that can be used to store useful information for your application. The memory used by the data pointer is not managed by Mini-XML so it is up to you to free it as necessary.

        +

        The mxmlSetUserData function sets any user (application) data associated with the node while the mxmlGetUserData function gets any user (application) data associated with the node:

        +
        void *
        +mxmlGetUserData(mxml_node_t *node);
        +
        +void
        +mxmlSetUserData(mxml_node_t *node, void *user_data);
        +
        +

        Memory Management

        +

        Nodes support reference counting to manage memory usage. The mxmlRetain and mxmlRelease functions increment and decrement a node's reference count, respectively. When the reference count goes to zero, mxmlRelease calls mxmlDelete to actually free the memory used by the node tree. New nodes start with a reference count of 1. You can get a node's current reference count using the mxmlGetRefCount function.

        +

        Strings can also support different kinds of memory management. The default is to use the standard C library strdup and free functions. To use alternate an alternate mechanism, call the mxmlSetStringCallbacks function to set string copy and free callbacks. The copy callback receives the callback data pointer and the string to copy, and returns a new string that will persist for the life of the XML data. The free callback receives the callback data pointer and the copied string and potentially frees the memory used for it. For example, the following code implements a simple string pool that eliminates duplicate strings:

        +
        typedef struct string_pool_s
        +{
        +  size_t num_strings;   // Number of strings
        +  size_t alloc_strings; // Allocated strings
        +  char   **strings;      // Array of strings
        +} string_pool_t;
        +
        +char *
        +copy_string(string_pool_t *pool, const char *s)
        +{
        +  size_t i;     // Looping var
        +  char   *news; // Copy of string
        +
        +
        +  // See if the string is already in the pool...
        +  for (i = 0; i < pool->num_strings; i ++)
        +  {
        +    if (!strcmp(pool->strings[i], s))
        +      return (pool->strings[i]);
        +  }
        +
        +  // Not in the pool, add new string
        +  if (pool->num_strings >= pool->alloc_strings)
        +  {
        +    // Expand the string pool...
        +    char **temp; // New strings array
        +
        +    temp = realloc(pool->strings,
        +                   (pool->alloc_strings + 32) *
        +                       sizeof(char *));
        +
        +    if (temp == NULL)
        +      return (NULL);
        +
        +    pool->alloc_strings += 32;
        +    pool->strings = temp;
        +  }
        +
        +  if ((news = strdup(s)) != NULL)
        +    pool->strings[pool->num_strings ++] = news;
        +
        +  return (news);
        +}
        +
        +void
        +free_string(string_pool_t *pool, char *s)
        +{
        +  // Do nothing here...
        +}
        +
        +void
        +free_all_strings(string_pool_t *pool)
        +{
        +  size_t i; // Looping var
        +
        +
        +  for (i = 0; i < pool->num_strings; i ++)
        +    free(pool->strings[i]);
        +  free(pool->strings);
        +}
        +
        +...
        +
        +// Setup the string pool...
        +string_pool_t pool = { 0, 0, NULL };
        +
        +mxmlSetStringCallbacks((mxml_strcopy_cb_t)copy_string,
        +                       (mxml_strfree_cb_t)free_string,
        +                       &pool);
        +
        +// Load an XML file...
        +mxml_node_t *xml;
        +
        +xml = mxmlLoadFilename(/*top*/NULL, /*options*/NULL,
        +                       "example.xml");
        +
        +// Process the XML file...
        +...
        +
        +// Free memory used by the XML file...
        +mxmlDelete(xml);
        +
        +// Free all strings in the pool...
        +free_all_strings(&pool);
        +

        Migrating from Mini-XML v3.x

        The following incompatible API changes were made in Mini-XML v4.0:

          +
        • Load and save callbacks and options are now managed using mxml_options_t values.

          +
        • +
        • The mxmlSAXLoadXxx functions have been removed in favor of setting the SAX callback function and data pointers of the mxml_options_t value prior to calling the corresponding mxmlLoadXxx functions.

          +
        • 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.

          @@ -1296,8 +1428,6 @@ print_children(mxml_node_t *parent)
        • 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.

          @@ -1308,6 +1438,8 @@ print_children(mxml_node_t *parent)
        • Text nodes (MXML_TYPE_TEXT) now use the bool type for the whitespace value.

        • +
        • Custom node callbacks are now set using the mxmlOptionsSetCustomCallbacks function instead of the thread-global mxmlSetCustomHandlers function.

          +

        Functions

        mxmlAdd

        @@ -1377,7 +1509,7 @@ attribute does not exist.

        mxmlElementGetAttrByIndex

        Get an attribute by index.

        -const char *mxmlElementGetAttrByIndex(mxml_node_t *node, int idx, const char **name);

        +const char *mxmlElementGetAttrByIndex(mxml_node_t *node, size_t idx, const char **name);

        Parameters

        @@ -1444,58 +1576,6 @@ void mxmlElementSetAttrf(mxml_node_t *node, const cha

        This function sets attribute name to the formatted value of format for the element node. If the named attribute already exists, the value of the attribute is replaced by the new formatted string value.

        -

        mxmlEntityAddCallback

        -

        Add a callback to convert entities to Unicode.

        -

        -bool mxmlEntityAddCallback(mxml_entity_cb_t cb, void *cbdata);

        -

        Parameters

        -
        node
        - - - - -
        cbCallback function to add
        cbdataCallback data
        -

        Return Value

        -

        true on success, false on failure

        -

        Discussion

        -

        This function adds a callback to the current thread that converts named -XML character entities to Unicode characters. The callback function cb -accepts the callback data pointer cbdata and the entity name and returns a -Unicode character value or -1 if the entity is not known. For example, the -following entity callback supports the "euro" entity:
        -
        -`c -int my_entity_cb(void cbdata, const char name) -{ - if (!strcmp(name, "euro")) - return (0x20ac); - else - return (-1); -} -`

        -

        mxmlEntityGetValue

        -

        Get the character corresponding to a named entity.

        -

        -int mxmlEntityGetValue(const char *name);

        -

        Parameters

        - - - -
        nameEntity name
        -

        Return Value

        -

        Character value or -1 on error

        -

        Discussion

        -

        The entity name can also be a numeric constant. -1 is returned if the -name is not known.

        -

        mxmlEntityRemoveCallback

        -

        Remove a callback.

        -

        -void mxmlEntityRemoveCallback(mxml_entity_cb_t cb);

        -

        Parameters

        - - - -
        cbCallback function to remove

        mxmlFindElement

        Find the named element.

        @@ -1910,224 +1990,138 @@ first time.

        mxmlLoadFd

        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);

        +mxml_node_t *mxmlLoadFd(mxml_node_t *top, mxml_options_t *options, int fd);

        Parameters

        + + - - - - - - - -
        top Top node
        optionsOptions
        fd File descriptor to read from
        load_cbLoad callback function or NULL
        load_cbdataLoad callback data
        sax_cbSAX callback function or NULL
        sax_cbdataSAX callback data

        Return Value

        First node or NULL if the file could not be read.

        Discussion

        This function loads the file descriptor fd into an XML node tree. The -nodes in the specified file are added to the specified node top. If NULL -is provided, the XML file MUST be well-formed with a single parent processing -instruction node like <?xml version="1.0"?> at the start of the file.
        +nodes in the specified file are added to the specified node top - if NULL +the XML file MUST be well-formed with a single parent processing instruction +node like <?xml version="1.0"?> at the start of the file.

        -The load callback function load_cb is called to obtain the node type that -should be used for child nodes. If NULL, the load_cbdata argument points -to a mmd_type_t variable that specifies the value type or MMD_TYPE_TEXT -if that argument is also NULL.
        -
        -The SAX callback function sax_cb and associated callback data sax_cbdata -are used to enable the Simple API for XML streaming mode. The callback is -called as the XML node tree is parsed.
        -
        -Note: The most common programming error when using the Mini-XML library is -to load an XML file using the MXML_TYPE_TEXT node type, which returns -inline text as a series of whitespace-delimited words, instead of using the -MXML_TYPE_OPAQUE node type which returns the inline text as a single string -(including whitespace).

        +Load options are provides via the options argument. If NULL, all values +will be loaded into MXML_TYPE_TEXT nodes. Use the mxmlOptionsNew +function to create options when loading XML data.

        mxmlLoadFile

        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);

        +mxml_node_t *mxmlLoadFile(mxml_node_t *top, mxml_options_t *options, FILE *fp);

        Parameters

        + + - - - - - - - -
        top Top node
        optionsOptions
        fp File to read from
        load_cbLoad callback function or NULL
        load_cbdataLoad callback data
        sax_cbSAX callback function or NULL
        sax_cbdataSAX callback data

        Return Value

        First node or NULL if the file could not be read.

        Discussion

        This function loads the FILE pointer fp into an XML node tree. The -nodes in the specified file are added to the specified node top. If NULL -is provided, the XML file MUST be well-formed with a single parent processing -instruction node like <?xml version="1.0"?> at the start of the file.
        +nodes in the specified file are added to the specified node top - if NULL +the XML file MUST be well-formed with a single parent processing instruction +node like <?xml version="1.0"?> at the start of the file.

        -The load callback function load_cb is called to obtain the node type that -should be used for child nodes. If NULL, the load_cbdata argument points -to a mmd_type_t variable that specifies the value type or MMD_TYPE_TEXT -if that argument is also NULL.
        -
        -The SAX callback function sax_cb and associated callback data sax_cbdata -are used to enable the Simple API for XML streaming mode. The callback is -called as the XML node tree is parsed.
        -
        -Note: The most common programming error when using the Mini-XML library is -to load an XML file using the MXML_TYPE_TEXT node type, which returns -inline text as a series of whitespace-delimited words, instead of using the -MXML_TYPE_OPAQUE node type which returns the inline text as a single string -(including whitespace).

        +Load options are provides via the options argument. If NULL, all values +will be loaded into MXML_TYPE_TEXT nodes. Use the mxmlOptionsNew +function to create options when loading XML data.

        mxmlLoadFilename

        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);

        +mxml_node_t *mxmlLoadFilename(mxml_node_t *top, mxml_options_t *options, const char *filename);

        Parameters

        + + - - - - - - - -
        top Top node
        optionsOptions
        filename File to read from
        load_cbLoad callback function or NULL
        load_cbdataLoad callback data
        sax_cbSAX callback function or NULL
        sax_cbdataSAX callback data

        Return Value

        First node or NULL if the file could not be read.

        Discussion

        This function loads the named file filename into an XML node tree. The -nodes in the specified file are added to the specified node top. If NULL -is provided, the XML file MUST be well-formed with a single parent processing -instruction node like <?xml version="1.0"?> at the start of the file.
        +nodes in the specified file are added to the specified node top - if NULL +the XML file MUST be well-formed with a single parent processing instruction +node like <?xml version="1.0"?> at the start of the file.

        -The load callback function load_cb is called to obtain the node type that -should be used for child nodes. If NULL, the load_cbdata argument points -to a mmd_type_t variable that specifies the value type or MMD_TYPE_TEXT -if that argument is also NULL.
        -
        -The SAX callback function sax_cb and associated callback data sax_cbdata -are used to enable the Simple API for XML streaming mode. The callback is -called as the XML node tree is parsed.
        -
        -Note: The most common programming error when using the Mini-XML library is -to load an XML file using the MXML_TYPE_TEXT node type, which returns -inline text as a series of whitespace-delimited words, instead of using the -MXML_TYPE_OPAQUE node type which returns the inline text as a single string -(including whitespace).

        +Load options are provides via the options argument. If NULL, all values +will be loaded into MXML_TYPE_TEXT nodes. Use the mxmlOptionsNew +function to create options when loading XML data.

        mxmlLoadIO

        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);

        +mxml_node_t *mxmlLoadIO(mxml_node_t *top, mxml_options_t *options, mxml_io_cb_t io_cb, void *io_cbdata);

        Parameters

        - + + + - + - - - - - - - -
        top Top node
        read_cb
        optionsOptions
        io_cb Read callback function
        read_cbdata
        io_cbdata Read callback data
        load_cbLoad callback function or NULL
        load_cbdataLoad callback data
        sax_cbSAX callback function or NULL
        sax_cbdataSAX callback data

        Return Value

        First node or NULL if the file could not be read.

        Discussion

        This function loads data into an XML node tree using a read callback. The -nodes in the specified file are added to the specified node top. If NULL -is provided, the XML file MUST be well-formed with a single parent processing -instruction node like <?xml version="1.0"?> at the start of the file.
        +nodes in the specified file are added to the specified node top - if NULL +the XML file MUST be well-formed with a single parent processing instruction +node like <?xml version="1.0"?> at the start of the file.

        -The read callback function read_cb is called to read a number of bytes from -the source. The callback data pointer read_cbdata is passed to the read +Load options are provides via the options argument. If NULL, all values +will be loaded into MXML_TYPE_TEXT nodes. Use the mxmlOptionsNew +function to create options when loading XML data.
        +
        +The read callback function io_cb is called to read a number of bytes from +the source. The callback data pointer io_cbdata is passed to the read callback with a pointer to a buffer and the maximum number of bytes to read, for example:

        `c -ssize_t my_read_cb(void cbdata, void buffer, size_t bytes) +size_t my_io_cb(void cbdata, void buffer, size_t bytes) { ... copy up to "bytes" bytes into buffer ... - ... return the number of bytes "read" or -1 on error ... + ... return the number of bytes "read" or 0 on error ... } -

        -
        -The load callback function `load_cb` is called to obtain the node type that
        -should be used for child nodes.  If `NULL`, the `load_cbdata` argument points
        -to a `mmd_type_t` variable that specifies the value type or `MMD_TYPE_TEXT`
        -if that argument is also `NULL`.
        -
        -The SAX callback function `sax_cb` and associated callback data `sax_cbdata`
        -are used to enable the Simple API for XML streaming mode.  The callback is
        -called as the XML node tree is parsed.
        -
        -Note: The most common programming error when using the Mini-XML library is
        -to load an XML file using the `MXML_TYPE_TEXT` node type, which returns
        -inline text as a series of whitespace-delimited words, instead of using the
        -`MXML_TYPE_OPAQUE` node type which returns the inline text as a single string
        -(including whitespace).
        -

        +`

        mxmlLoadString

        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);

        +mxml_node_t *mxmlLoadString(mxml_node_t *top, mxml_options_t *options, const char *s);

        Parameters

        + + - - - - - - - -
        top Top node
        optionsOptions
        s String to load
        load_cbLoad callback function or NULL
        load_cbdataLoad callback data
        sax_cbSAX callback function or NULL
        sax_cbdataSAX callback data

        Return Value

        First node or NULL if the string has errors.

        Discussion

        This function loads the string into an XML node tree. The nodes in the -specified file are added to the specified node top. If NULL is provided, -the XML file MUST be well-formed with a single parent processing instruction -node like <?xml version="1.0"?> at the start of the file.
        +specified file are added to the specified node top - if NULL the XML file +MUST be well-formed with a single parent processing instruction node like +<?xml version="1.0"?> at the start of the file.

        -The load callback function load_cb is called to obtain the node type that -should be used for child nodes. If NULL, the load_cbdata argument points -to a mmd_type_t variable that specifies the value type or MMD_TYPE_TEXT -if that argument is also NULL.
        -
        -The SAX callback function sax_cb and associated callback data sax_cbdata -are used to enable the Simple API for XML streaming mode. The callback is -called as the XML node tree is parsed.
        -
        -Note: The most common programming error when using the Mini-XML library is -to load an XML file using the MXML_TYPE_TEXT node type, which returns -inline text as a series of whitespace-delimited words, instead of using the -MXML_TYPE_OPAQUE node type which returns the inline text as a single string -(including whitespace).

        +Load options are provides via the options argument. If NULL, all values +will be loaded into MXML_TYPE_TEXT nodes. Use the mxmlOptionsNew +function to create options when loading XML data.

        mxmlNewCDATA

        Create a new CDATA node.

        @@ -2208,23 +2202,24 @@ nul-terminated and is formatted into the new node.

        mxmlNewCustom

        Create a new custom data node.

        -mxml_node_t *mxmlNewCustom(mxml_node_t *parent, void *data, mxml_custom_destroy_cb_t destroy);

        +mxml_node_t *mxmlNewCustom(mxml_node_t *parent, void *data, mxml_custfree_cb_t free_cb, void *free_cbdata);

        Parameters

        - - + + + +
        parent Parent node or MXML_NO_PARENT
        data Pointer to data
        destroyFunction to destroy data
        free_cbFree callback function or NULL if none needed
        free_cbdataFree callback data

        Return Value

        New node

        Discussion

        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.

        +list. The free_cb argument specifies a function to call to free the custom +data when the node is deleted.

        mxmlNewDeclaration

        Create a new declaraction node.

        @@ -2448,6 +2443,368 @@ string must be nul-terminated and is formatted into the new node.

        Discussion

        The "version" argument specifies the version number to put in the ?xml directive node. If NULL, version "1.0" is assumed.

        +

        mxmlOptionsDelete

        +

        Free load/save options.

        +

        +void mxmlOptionsDelete(mxml_options_t *options);

        +

        Parameters

        + + + +
        optionsOptions
        +

        mxmlOptionsNew

        +

        Allocate load/save options.

        +

        +mxml_options_t *mxmlOptionsNew(void);

        +

        Return Value

        +

        Options

        +

        Discussion

        +

        This function creates a new set of load/save options to use with the +mxmlLoadFd, mxmlLoadFile, mxmlLoadFilename, +mxmlLoadIO, mxmlLoadString, mxmlSaveAllocString, +mxmlSaveFd, mxmlSaveFile, mxmlSaveFilename, +mxmlSaveIO, and mxmlSaveString functions. Options can be +reused for multiple calls to these functions and should be freed using the +mxmlOptionsDelete function.
        +
        +The default load/save options load values using the constant type +MXML_TYPE_TEXT and save XML data with a wrap margin of 72 columns. +The various mxmlOptionsSet functions are used to change the defaults, +for example:
        +
        +`c +mxml_options_t options = mxmlOptionsNew(); + +/ Load values as opaque strings */ +mxmlOptionsSetTypeValue(options, MXML_TYPE_OPAQUE); +

        +
        +Note: The most common programming error when using the Mini-XML library is
        +to load an XML file using the `MXML_TYPE_TEXT` node type, which returns
        +inline text as a series of whitespace-delimited words, instead of using the
        +`MXML_TYPE_OPAQUE` node type which returns the inline text as a single string
        +(including whitespace).
        +

        +

        mxmlOptionsSetCustomCallbacks

        +

        Set the custom data callbacks.

        +

        +void mxmlOptionsSetCustomCallbacks(mxml_options_t *options, mxml_custload_cb_t load_cb, mxml_custsave_cb_t save_cb, void *cbdata);

        +

        Parameters

        + + + + + + + + + +
        optionsOptions
        load_cbCustom load callback function
        save_cbCustom save callback function
        cbdataCustom callback data
        +

        Discussion

        +

        This function sets the callbacks that are used for loading and saving custom +data types. The load callback load_cb accepts the callback data pointer +cbdata, a node pointer, and a data string and returns true on success and +false on error, for example:
        +
        +`c +typedef struct +{ + unsigned year, / Year / + month, / Month / + day, / Day / + hour, / Hour / + minute, / Minute / + second; / Second / + time_t unix; / UNIX time / +} iso_date_time_t;
        +
        +void +my_custom_free_cb(void cbdata, void data) +{ + free(data); +}
        +
        +bool +my_custom_load_cb(void cbdata, mxml_node_t node, const char data) +{ + iso_date_time_t dt; + struct tm tmdata;
        +
        + / Allocate custom data structure ... / + dt = calloc(1, sizeof(iso_date_time_t));
        +
        + / Parse 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 parse date and time numbers... / + 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);
        +
        + / Set custom data and free function... / + mxmlSetCustom(node, data, my_custom_free, /cbdata/NULL);
        +
        + / Return with no errors... / + return (true); +} +

        +
        +The save callback `save_cb` accepts the callback data pointer `cbdata` and a
        +node pointer and returns a malloc'd string on success and `NULL` on error,
        +for example:
        +
        +```c
        +char *
        +my_custom_save_cb(void *cbdata, mxml_node_t *node)
        +{
        +  char data[255];
        +  iso_date_time_t *dt;
        +
        +  /* Get the custom data structure */
        +  dt = (iso_date_time_t *)mxmlGetCustom(node);
        +
        +  /* Generate string version of the date/time... */
        +  snprintf(data, sizeof(data), "%04u-%02u-%02uT%02u:%02u:%02uZ",
        +           dt->year, dt->month, dt->day, dt->hour, dt->minute, dt->second);
        +
        +  /* Duplicate the string and return... */
        +  return (strdup(data));
        +}
        +
        +

        +

        mxmlOptionsSetEntityCallback

        +

        Set the entity lookup callback to use when loading XML data.

        +

        +void mxmlOptionsSetEntityCallback(mxml_options_t *options, mxml_entity_cb_t cb, void *cbdata);

        +

        Parameters

        + + + + + + + +
        optionsOptions
        cbEntity callback function
        cbdataEntity callback data
        +

        Discussion

        +

        This function sets the callback that is used to lookup named XML character +entities when loading XML data. The callback function cb accepts the +callback data pointer cbdata and the entity name. The function returns a +Unicode character value or -1 if the entity is not known. For example, the +following entity callback supports the "euro" entity:
        +
        +`c +int my_entity_cb(void cbdata, const char name) +{ + if (!strcmp(name, "euro")) + return (0x20ac); + else + return (-1); +} +

        +
        +Mini-XML automatically supports the "amp", "gt", "lt", and "quot" character
        +entities which are required by the base XML specification.
        + char *data) +{ + iso_date_time_t

        +

        mxmlOptionsSetErrorCallback

        +

        Set the error message callback.

        +

        +void mxmlOptionsSetErrorCallback(mxml_options_t *options, mxml_error_cb_t cb, void *cbdata);

        +

        Parameters

        + + + + + + + +
        optionsOptions
        cbError callback function
        cbdataError callback data
        +

        Discussion

        +

        This function sets a function to use when reporting errors. The callback +cb accepts the data pointer cbdata and a string pointer containing the +error message:
        +
        +`c +void my_error_cb(void cbdata, const char message) +{ + fprintf(stderr, "myprogram: %sn", message); +} +

        +
        +The default error callback writes the error message to the `stderr` file.
        +ack supports the "euro" entity:
        +
        +`

        +

        mxmlOptionsSetSAXCallback

        +

        Set the SAX callback to use when reading XML data.

        +

        +void mxmlOptionsSetSAXCallback(mxml_options_t *options, mxml_sax_cb_t cb, void *cbdata);

        +

        Parameters

        + + + + + + + +
        optionsOptions
        cbSAX callback function
        cbdataSAX callback data
        +

        Discussion

        +

        This function sets a SAX callback to use when reading XML data. The SAX +callback function cb and associated callback data cbdata are used to +enable the Simple API for XML streaming mode. The callback is called as the +XML node tree is parsed and receives the cbdata pointer, the mxml_node_t +pointer, and an event code. The function returns true to continue +processing or false to stop:
        +
        +`c +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.
        + /* Date information is out of range...

        +

        mxmlOptionsSetTypeCallback

        +

        Set the type callback for child/value nodes.

        +

        +void mxmlOptionsSetTypeCallback(mxml_options_t *options, mxml_type_cb_t cb, void *cbdata);

        +

        Parameters

        + + + + + + + +
        optionsOptions
        cbType callback function
        cbdataType callback data
        +

        Discussion

        +

        The load callback function cb is called to obtain the node type child/value +nodes and receives the cbdata pointer and the mxml_node_t pointer, for +example:
        +
        +`c +mxml_type_t +my_type_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); +} +`

        +

        mxmlOptionsSetTypeValue

        +

        Set the type to use for all child/value nodes.

        +

        +void mxmlOptionsSetTypeValue(mxml_options_t *options, mxml_type_t type);

        +

        Parameters

        + + + + + +
        optionsOptions
        typeValue node type
        +

        Discussion

        +

        This functions sets a constant node type to use for all child/value nodes.

        +

        mxmlOptionsSetWhitespaceCallback

        +

        Set the whitespace callback.

        +

        +void mxmlOptionsSetWhitespaceCallback(mxml_options_t *options, mxml_ws_cb_t cb, void *cbdata);

        +

        Parameters

        + + + + + + + +
        optionsOptions
        cbWhitespace callback function
        cbdataWhitespace callback data
        +

        Discussion

        +

        This function sets the whitespace callback that is used when saving XML data. +The callback function cb specifies a function that returns a whitespace +string or NULL before and after each element. The function receives the +callback data pointer cbdata, the mxml_node_t pointer, and a "when" +value indicating where the whitespace is being added, for example:
        +
        +`c +const char my_whitespace_cb(void cbdata, mxml_node_t *node, mxml_ws_t when) +{ + if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) + return ("n"); + else + return (NULL); +} +`

        +

        mxmlOptionsSetWrapMargin

        +

        Set the wrap margin when saving XML data.

        +

        +void mxmlOptionsSetWrapMargin(mxml_options_t *options, int column);

        +

        Parameters

        + + + + + +
        optionsOptions
        columnWrap column
        +

        Discussion

        +

        This function sets the wrap margin used when saving XML data. Wrapping is +disabled when column is 0.

        mxmlRelease

        Release a node.

        @@ -2488,15 +2845,13 @@ int mxmlRetain(mxml_node_t *node);

        mxmlSaveAllocString

        Save an XML tree to an allocated string.

        -char *mxmlSaveAllocString(mxml_node_t *node, mxml_save_cb_t save_cb, void *save_cbdata);

        +char *mxmlSaveAllocString(mxml_node_t *node, mxml_options_t *options);

        Parameters

        - - - - + +
        node Node to write
        save_cbWhitespace callback function
        save_cbdataWhitespace callback data
        optionsOptions

        Return Value

        Allocated string or NULL

        @@ -2508,212 +2863,135 @@ should be freed using free (or the string free callback set using NULL is returned if the node would produce an empty string or if the string cannot be allocated.

        -The callback function save_cb specifies a function that returns a -whitespace string or NULL before and after each element. The function -receives the callback data pointer save_cbdata, the mxml_node_t pointer, -and a "when" value indicating where the whitespace is being added, for -example:
        -
        -`c -const char my_save_cb(void cbdata, mxml_node_t *node, mxml_ws_t when) -{ - if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) - return ("n"); - else - return (NULL); -} -`

        +Save options are provides via the options argument. If NULL, the XML +output will be wrapped at column 72 with no additional whitespace. Use the +mxmlOptionsNew function to create options for saving XML data.

        mxmlSaveFd

        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);

        +bool mxmlSaveFd(mxml_node_t *node, mxml_options_t *options, int fd);

        Parameters

        + + - - - -
        node Node to write
        optionsOptions
        fd File descriptor to write to
        save_cbWhitespace callback function
        save_cbdataWhitespace callback data

        Return Value

        true on success, false on error.

        Discussion

        This function saves the XML tree node to a file descriptor.

        -The callback function save_cb specifies a function that returns a -whitespace string or NULL before and after each element. The function -receives the callback data pointer save_cbdata, the mxml_node_t pointer, -and a "when" value indicating where the whitespace is being added, for -example:
        -
        -`c -const char my_save_cb(void cbdata, mxml_node_t *node, mxml_ws_t when) -{ - if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) - return ("n"); - else - return (NULL); -} -`

        +Save options are provides via the options argument. If NULL, the XML +output will be wrapped at column 72 with no additional whitespace. Use the +mxmlOptionsNew function to create options for saving XML data.

        mxmlSaveFile

        Save an XML tree to a file.

        -bool mxmlSaveFile(mxml_node_t *node, FILE *fp, mxml_save_cb_t save_cb, void *save_cbdata);

        +bool mxmlSaveFile(mxml_node_t *node, mxml_options_t *options, FILE *fp);

        Parameters

        + + - - - -
        node Node to write
        optionsOptions
        fp File to write to
        save_cbWhitespace callback function
        save_cbdataWhitespace callback data

        Return Value

        true on success, false on error.

        Discussion

        This function saves the XML tree node to a stdio FILE.

        -The callback function save_cb specifies a function that returns a -whitespace string or NULL before and after each element. The function -receives the callback data pointer save_cbdata, the mxml_node_t pointer, -and a "when" value indicating where the whitespace is being added, for -example:
        -
        -`c -const char my_save_cb(void cbdata, mxml_node_t *node, mxml_ws_t when) -{ - if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) - return ("n"); - else - return (NULL); -} -`

        +Save options are provides via the options argument. If NULL, the XML +output will be wrapped at column 72 with no additional whitespace. Use the +mxmlOptionsNew function to create options for saving XML data.

        mxmlSaveFilename

        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);

        +bool mxmlSaveFilename(mxml_node_t *node, mxml_options_t *options, const char *filename);

        Parameters

        + + - - - -
        node Node to write
        optionsOptions
        filename File to write to
        save_cbWhitespace callback function
        save_cbdataWhitespace callback data

        Return Value

        true on success, false on error.

        Discussion

        This function saves the XML tree node to a named file.

        -The callback function save_cb specifies a function that returns a -whitespace string or NULL before and after each element. The function -receives the callback data pointer save_cbdata, the mxml_node_t pointer, -and a "when" value indicating where the whitespace is being added, for -example:
        -
        -`c -const char my_save_cb(void cbdata, mxml_node_t *node, mxml_ws_t when) -{ - if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) - return ("n"); - else - return (NULL); -} -`

        +Save options are provides via the options argument. If NULL, the XML +output will be wrapped at column 72 with no additional whitespace. Use the +mxmlOptionsNew function to create options for saving XML data.

        mxmlSaveIO

        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);

        +bool mxmlSaveIO(mxml_node_t *node, mxml_options_t *options, mxml_io_cb_t io_cb, void *io_cbdata);

        Parameters

        - + + + - + - - - -
        node Node to write
        write_cb
        optionsOptions
        io_cb Write callback function
        write_cbdata
        io_cbdata Write callback data
        save_cbWhitespace callback function
        save_cbdataWhitespace callback data

        Return Value

        true on success, false on error.

        Discussion

        This function saves the XML tree node using a write callback function -write_cb. The write callback is called with the callback data pointer -write_cbdata, a buffer pointer, and the number of bytes to write, for +io_cb. The write callback is called with the callback data pointer +io_cbdata, a buffer pointer, and the number of bytes to write, for example:

        `c -ssize_t my_write_cb(void cbdata, const void buffer, size_t bytes) +size_t my_io_cb(void cbdata, const void buffer, size_t bytes) { ... write/copy bytes from buffer to the output ... - ... return the number of bytes written/copied or -1 on error ... + ... return the number of bytes written/copied or 0 on error ... }

         
        -The callback function `save_cb` specifies a function that returns a
        -whitespace string or `NULL` before and after each element.  The function
        -receives the callback data pointer `save_cbdata`, the `mxml_node_t` pointer,
        -and a "when" value indicating where the whitespace is being added, for
        -example:
        -
        -```c
        -const char *my_save_cb(void *cbdata, mxml_node_t *node, mxml_ws_t when)
        -{
        -  if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE)
        -    return ("n");
        +Save options are provides via the `options` argument.  If `NULL`, the XML
        +output will be wrapped at column 72 with no additional whitespace.  Use the
        +@link mxmlOptionsNew@ function to create options for saving XML data.
        +, "real")) + return (MXML_TYPE_REAL); else - return (NULL); + return (MXML_TYPE_TEXT); } -
        -

        +`

        mxmlSaveString

        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);

        +size_t mxmlSaveString(mxml_node_t *node, mxml_options_t *options, char *buffer, size_t bufsize);

        Parameters

        + + - - - -
        node Node to write
        optionsOptions
        buffer String buffer
        bufsize Size of string buffer
        save_cbWhitespace callback function
        save_cbdataWhitespace callback function

        Return Value

        Size of string

        Discussion

        -

        This function saves the XML tree node to a string buffer.
        +

        This function saves the XML tree node to a fixed-size string buffer.

        -The callback function save_cb specifies a function that returns a -whitespace string or NULL before and after each element. The function -receives the callback data pointer save_cbdata, the mxml_node_t pointer, -and a "when" value indicating where the whitespace is being added, for -example:
        -
        -`c -const char my_save_cb(void cbdata, mxml_node_t *node, mxml_ws_t when) -{ - if (when == MXML_WS_BEFORE_OPEN || when == MXML_WS_AFTER_CLOSE) - return ("n"); - else - return (NULL); -} -`

        +Save options are provides via the options argument. If NULL, the XML +output will be wrapped at column 72 with no additional whitespace. Use the +mxmlOptionsNew function to create options for saving XML data.

        mxmlSetCDATA

        Set the data for a CDATA node.

        @@ -2783,15 +3061,17 @@ bool mxmlSetCommentf(mxml_node_t *node, const char *f

        mxmlSetCustom

        Set the data and destructor of a custom data node.

        -bool mxmlSetCustom(mxml_node_t *node, void *data, mxml_custom_destroy_cb_t destroy_cb);

        +bool mxmlSetCustom(mxml_node_t *node, void *data, mxml_custfree_cb_t free_cb, void *free_cbdata);

        Parameters

        - - + + + +
        node Node to set
        data New data pointer
        destroy_cbNew destructor function
        free_cbFree callback function
        free_cbdataFree callback data

        Return Value

        true on success, false on failure

        @@ -2997,18 +3277,6 @@ bool mxmlSetUserData(mxml_node_t *node, void *data);<

        Return Value

        true on success, false on failure

        -

        mxmlSetWrapMargin

        -

        Set the wrap margin when saving XML data.

        -

        -void mxmlSetWrapMargin(int column);

        -

        Parameters

        - - - -
        columnColumn for wrapping, 0 to disable wrapping
        -

        Discussion

        -

        This function sets the wrap margin used when saving XML data for the current -thread. Wrapping is disabled when column is 0.

        mxmlWalkNext

        Walk to the next logical node in the tree.

        @@ -3053,20 +3321,20 @@ The top argument constrains the walk to that node's children.

        typedef enum mxml_add_e mxml_add_t;

        -

        mxml_custom_destroy_cb_t

        +

        mxml_custfree_cb_t

        Custom data destructor

        -typedef void (*mxml_custom_destroy_cb_t)(void *); +typedef void (*mxml_custfree_cb_t)(void *cbdata void *custdata);

        -

        mxml_custom_load_cb_t

        +

        mxml_custload_cb_t

        Custom data load callback function

        -typedef bool (*mxml_custom_load_cb_t)(void *cbdata mxml_node_t *node const char *s); +typedef bool (*mxml_custload_cb_t)(void *cbdata mxml_node_t *node const char *s);

        -

        mxml_custom_save_cb_t

        +

        mxml_custsave_cb_t

        Custom data save callback function

        -typedef char *(*mxml_custom_save_cb_t)(void *cbdata mxml_node_t *node); +typedef char *(*mxml_custsave_cb_t)(void *cbdata mxml_node_t *node);

        mxml_descend_t

        mxmlFindElement, mxmlWalkNext, and mxmlWalkPrev descend values

        @@ -3084,29 +3352,24 @@ typedef int (*mxml_entity_cb_t)(void *cbdata const char *name); typedef void (*mxml_error_cb_t)(void *cbdata const char *message);

        mxml_index_t

        -

        An XML node index.

        +

        An XML node index

        typedef struct _mxml_index_s mxml_index_t;

        -

        mxml_load_cb_t

        -

        Load callback function

        +

        mxml_io_cb_t

        +

        Read/write callback function

        -typedef mxml_type_t (*mxml_load_cb_t)(void *cbdata mxml_node_t *node); +typedef size_t (*mxml_io_cb_t)(void *cbdata void *buffer size_t bytes);

        mxml_node_t

        -

        An XML node.

        +

        An XML node

        typedef struct _mxml_node_s mxml_node_t;

        -

        mxml_read_cb_t

        -

        Read callback function

        +

        mxml_options_t

        +

        XML options

        -typedef ssize_t (*mxml_read_cb_t)(void *cbdata void *buffer size_t bytes); -

        -

        mxml_save_cb_t

        -

        Save callback function

        -

        -typedef const char *(*mxml_save_cb_t)(void *cbdata mxml_node_t *node mxml_ws_t when); +typedef struct _mxml_options_s mxml_options_t;

        mxml_sax_cb_t

        SAX callback function

        @@ -3128,15 +3391,20 @@ typedef char *(*mxml_strcopy_cb_t)(void *cbdata const char *s);

        typedef void (*mxml_strfree_cb_t)(void *cbdata char *s);

        +

        mxml_type_cb_t

        +

        Type callback function

        +

        +typedef mxml_type_t (*mxml_type_cb_t)(void *cbdata mxml_node_t *node); +

        mxml_type_t

        The XML node type.

        typedef enum mxml_type_e mxml_type_t;

        -

        mxml_write_cb_t

        -

        Write callback function

        +

        mxml_ws_cb_t

        +

        Whitespace callback function

        -typedef ssize_t (*mxml_write_cb_t)(void *cbdata const void *buffer size_t bytes); +typedef const char *(*mxml_ws_cb_t)(void *cbdata mxml_node_t *node mxml_ws_t when);

        mxml_ws_t

        Whitespace periods