diff --git a/doc/body.md b/doc/body.md index 04391b6..87495e0 100644 --- a/doc/body.md +++ b/doc/body.md @@ -1,11 +1,12 @@ --- -title: Mini-XML 3.3 Programming Manual +title: Mini-XML 4.0 Programming Manual author: Michael R Sweet -copyright: Copyright © 2003-2022, All Rights Reserved. -version: 3.3 +copyright: Copyright © 2003-2024, All Rights Reserved. +version: 4.0 ... -# Introduction +Introduction +============ Mini-XML is a small XML parsing library that you can use to read XML data files or strings in your application without requiring large non-standard libraries. @@ -18,7 +19,7 @@ Mini-XML provides the following functionality: - SAX (streamed) reading of XML files and strings to minimize memory usage. - Supports arbitrary element names, attributes, and attribute values with no preset limits, just available memory. -- Supports integer, real, opaque ("cdata"), and text data types in "leaf" +- Supports integer, real, opaque ("CDATA"), and text data types in "leaf" nodes. - Functions for creating and managing trees of data. - "Find" and "walk" functions for easily locating and navigating trees of @@ -28,7 +29,8 @@ 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 +History +------- Mini-XML was initially developed for the [Gutenprint](http://gutenprint.sf.net/) project to replace the rather large and unwieldy `libxml2` library with @@ -49,30 +51,35 @@ 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 4,300 lines of code, compared to 196,141 lines of code for libxml2 -version 2.9.9. +at a whopping 4,371 lines of code, compared to 175,808 lines of code for libxml2 +version 2.11.7. -## Resources +Resources +--------- The Mini-XML home page can be found at . From there you can download the current version of Mini-XML, access the issue tracker, and find other resources. -## Legal Stuff +Legal Stuff +----------- -The Mini-XML library is copyright © 2003-2021 by Michael R Sweet and is provided -under the Apache License Version 2.0 with an exception to allow linking against -GPL2/LGPL2-only software. See the files "LICENSE" and "NOTICE" for more -information. +The Mini-XML library is copyright © 2003-2024 by Michael R Sweet and is provided +under the Apache License Version 2.0 with an (optional) exception to allow +linking against GPL2/LGPL2-only software. See the files "LICENSE" and "NOTICE" +for more information. -# Using Mini-XML +Using Mini-XML +============== Mini-XML provides a single header file which you include: - #include +```c +#include +``` The Mini-XML library is included with your program using the `-lmxml` option: @@ -84,13 +91,16 @@ proper compiler and linker options for your installation: gcc `pkg-config --cflags mxml` -o myprogram myprogram.c `pkg-config --libs mxml` -## Loading an XML File +Loading an XML File +------------------- You load an XML file using the `mxmlLoadFile` function: - mxml_node_t * - mxmlLoadFile(mxml_node_t *top, FILE *fp, - mxml_type_t (*cb)(mxml_node_t *)); +```c +mxml_node_t * +mxmlLoadFile(mxml_node_t *top, FILE *fp, + mxml_type_t (*cb)(mxml_node_t *)); +``` The `cb` argument specifies a function that assigns child (value) node types for each element in the document. The callback can be a function you provide or one @@ -98,22 +108,26 @@ of the standard functions provided with Mini-XML. For example, to load the XML file "filename.xml" containing text strings you can use the `MXML_OPAQUE_CALLBACK` function: - FILE *fp; - mxml_node_t *tree; +```c +FILE *fp; +mxml_node_t *tree; - fp = fopen("filename.xml", "r"); - tree = mxmlLoadFile(NULL, fp, MXML_OPAQUE_CALLBACK); - fclose(fp); +fp = fopen("filename.xml", "r"); +tree = mxmlLoadFile(NULL, fp, MXML_OPAQUE_CALLBACK); +fclose(fp); +``` Mini-XML also provides functions to load from a file descriptor or string: - mxml_node_t * - mxmlLoadFd(mxml_node_t *top, int fd, - mxml_type_t (*cb)(mxml_node_t *)); +```c +mxml_node_t * +mxmlLoadFd(mxml_node_t *top, int fd, + mxml_type_t (*cb)(mxml_node_t *)); - mxml_node_t * - mxmlLoadString(mxml_node_t *top, const char *s, - mxml_type_t (*cb)(mxml_node_t *)); +mxml_node_t * +mxmlLoadString(mxml_node_t *top, const char *s, + mxml_type_t (*cb)(mxml_node_t *)); +``` ### Load Callbacks @@ -130,50 +144,55 @@ defines several standard callbacks for simple XML data files: You can provide your own callback functions for more complex XML documents. Your callback function will receive a pointer to the current element node and must return the value type of the immediate children for that element node: -`MXML_CUSTOM`, `MXML_INTEGER`, `MXML_OPAQUE`, `MXML_REAL`, or `MXML_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. +`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 - type_cb(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 (!strcmp(type, "integer")) - return (MXML_INTEGER); - else if (!strcmp(type, "opaque")) - return (MXML_OPAQUE); - else if (!strcmp(type, "real")) - return (MXML_REAL); - else - return (MXML_TEXT); - } +```c +mxml_type_t +type_cb(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 (!strcmp(type, "integer")) + return (MXML_TYPE_INTEGER); + else if (!strcmp(type, "opaque")) + return (MXML_TYPE_OPAQUE); + else if (!strcmp(type, "real")) + return (MXML_TYPE_REAL); + else + return (MXML_TYPE_TEXT); +} +``` To use this callback function, simply use the name when you call any of the load functions: - FILE *fp; - mxml_node_t *tree; +```c +FILE *fp; +mxml_node_t *tree; - fp = fopen("filename.xml", "r"); - tree = mxmlLoadFile(NULL, fp, type_cb); - fclose(fp); +fp = fopen("filename.xml", "r"); +tree = mxmlLoadFile(NULL, fp, type_cb); +fclose(fp); +``` -## Nodes +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, @@ -215,93 +234,107 @@ child or parent node. The `mxmlGetType` function gets the type of a node: - mxml_type_t - mxmlGetType(mxml_node_t *node); - -- `MXML_CUSTOM` : A custom value defined by your application, -- `MXML_ELEMENT` : An XML element, CDATA, comment, or processing instruction, -- `MXML_INTEGER` : A whitespace-delimited integer value, -- `MXML_OPAQUE` : An opaque string value that preserves all whitespace, -- `MXML_REAL` : A whitespace-delimited floating point value, or -- `MXML_TEXT` : A whitespace-delimited text (fragment) value. - -> Note: CDATA, comment, and processing directive nodes are currently stored in -> memory as special elements. This will be changed in a future major release of -> Mini-XML. +```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: - mxml_node_t * - mxmlGetFirstChild(mxml_node_t *node); +```c +mxml_node_t * +mxmlGetFirstChild(mxml_node_t *node); - mxml_node_t * - mxmlGetLastChild(mxml_node_t *node); +mxml_node_t * +mxmlGetLastChild(mxml_node_t *node); - mxml_node_t * - mxmlGetNextSibling(mxml_node_t *node); +mxml_node_t * +mxmlGetNextSibling(mxml_node_t *node); - mxml_node_t * - mxmlGetParent(mxml_node_t *node); +mxml_node_t * +mxmlGetParent(mxml_node_t *node); - mxml_node_t * - mxmlGetPrevSibling(mxml_node_t *node); +mxml_node_t * +mxmlGetPrevSibling(mxml_node_t *node); +``` The `mxmlGetUserData` function gets any user (application) data associated with the node: - void * - mxmlGetUserData(mxml_node_t *node); +```c +void * +mxmlGetUserData(mxml_node_t *node); +``` -## Creating XML Documents +Creating XML Documents +---------------------- You can create and update XML documents in memory using the various `mxmlNew` functions. The following code will create the XML document described in the previous section: - mxml_node_t *xml; /* */ - mxml_node_t *data; /* */ - mxml_node_t *node; /* */ - mxml_node_t *group; /* */ +```c +mxml_node_t *xml; /* */ +mxml_node_t *data; /* */ +mxml_node_t *node; /* */ +mxml_node_t *group; /* */ - xml = mxmlNewXML("1.0"); +xml = mxmlNewXML("1.0"); - data = mxmlNewElement(xml, "data"); +data = mxmlNewElement(xml, "data"); - node = mxmlNewElement(data, "node"); - mxmlNewText(node, 0, "val1"); - node = mxmlNewElement(data, "node"); - mxmlNewText(node, 0, "val2"); - node = mxmlNewElement(data, "node"); - mxmlNewText(node, 0, "val3"); + 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"); + group = mxmlNewElement(data, "group"); - node = mxmlNewElement(group, "node"); - mxmlNewText(node, 0, "val4"); - node = mxmlNewElement(group, "node"); - mxmlNewText(node, 0, "val5"); - node = mxmlNewElement(group, "node"); - mxmlNewText(node, 0, "val6"); + 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, 0, "val7"); - node = mxmlNewElement(data, "node"); - mxmlNewText(node, 0, "val8"); + node = mxmlNewElement(data, "node"); + mxmlNewText(node, false, "val7"); + node = mxmlNewElement(data, "node"); + mxmlNewText(node, false, "val8"); +``` We start by creating the declaration node common to all XML files using the `mxmlNewXML` function: - xml = mxmlNewXML("1.0"); +```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`): - data = mxmlNewElement(xml, "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 @@ -309,43 +342,52 @@ parent node \(`node`). The second argument specifies whether whitespace appears before the text - 0 or false in this case. The last argument specifies the actual text to add: - node = mxmlNewElement(data, "node"); - mxmlNewText(node, 0, "val1"); +```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. -## Saving an XML File +Saving an XML File +------------------ You save an XML file using the `mxmlSaveFile` function: - int - mxmlSaveFile(mxml_node_t *node, FILE *fp, - mxml_save_cb_t cb); +```c +bool +mxmlSaveFile(mxml_node_t *node, FILE *fp, + mxml_save_cb_t cb); +``` The `cb` argument specifies a function that returns the whitespace (if any) that is inserted before and after each element node. The `MXML_NO_CALLBACK` constant tells Mini-XML to not include any extra whitespace. For example, so save an XML file to the file "filename.xml" with no extra whitespace: - FILE *fp; +```c +FILE *fp; - fp = fopen("filename.xml", "w"); - mxmlSaveFile(xml, fp, MXML_NO_CALLBACK); - fclose(fp); +fp = fopen("filename.xml", "w"); +mxmlSaveFile(xml, fp, MXML_NO_CALLBACK); +fclose(fp); +``` Mini-XML also provides functions to save to a file descriptor or strings: - char * - mxmlSaveAllocString(mxml_node_t *node, mxml_save_cb_t cb); +```c +char * +mxmlSaveAllocString(mxml_node_t *node, mxml_save_cb_t cb); - int - mxmlSaveFd(mxml_node_t *node, int fd, mxml_save_cb_t cb); +bool +mxmlSaveFd(mxml_node_t *node, int fd, mxml_save_cb_t cb); - int - mxmlSaveString(mxml_node_t *node, char *buffer, int bufsize, - mxml_save_cb_t cb); +size_t +mxmlSaveString(mxml_node_t *node, char *buffer, size_t bufsize, + mxml_save_cb_t cb); +``` ### Controlling Line Wrapping @@ -354,15 +396,21 @@ 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); +```c +void mxmlSetWrapMargin(int column); +``` For example, the following code sets the margin to 132 columns: - mxmlSetWrapMargin(132); +```c +mxmlSetWrapMargin(132); +``` while the following code disables wrapping by setting the margin to 0: - mxmlSetWrapMargin(0); +```c +mxmlSetWrapMargin(0); +``` ### Save Callbacks @@ -378,89 +426,96 @@ tabs, carriage returns, and newlines) otherwise. The following whitespace callback can be used to add whitespace to XHTML output to make it more readable in a standard text editor: - const char * - whitespace_cb(mxml_node_t *node, int where) - { - const char *element; - - /* - * We can conditionally break to a new line before or after - * any element. These are just common HTML elements... - */ - - element = mxmlGetElement(node); - - if (!strcmp(element, "html") || - !strcmp(element, "head") || - !strcmp(element, "body") || - !strcmp(element, "pre") || - !strcmp(element, "p") || - !strcmp(element, "h1") || - !strcmp(element, "h2") || - !strcmp(element, "h3") || - !strcmp(element, "h4") || - !strcmp(element, "h5") || - !strcmp(element, "h6")) - { - /* - * Newlines before open and after close... - */ - - if (where == MXML_WS_BEFORE_OPEN || - where == MXML_WS_AFTER_CLOSE) - return ("\n"); - } - else if (!strcmp(element, "dl") || - !strcmp(element, "ol") || - !strcmp(element, "ul")) - { - /* - * Put a newline before and after list elements... - */ - - return ("\n"); - } - else if (!strcmp(element, "dd") || - !strcmp(element, "dt") || - !strcmp(element, "li")) - { - /* - * Put a tab before
  • 's,
    's, and
    's, and a - * newline after them... - */ - - if (where == MXML_WS_BEFORE_OPEN) - return ("\t"); - else if (where == MXML_WS_AFTER_CLOSE) - return ("\n"); - } - - /* - * Otherwise return NULL for no added whitespace... - */ - - return (NULL); - } +```c +const char * +whitespace_cb(mxml_node_t *node, int where) +{ + const char *element; + + /* + * We can conditionally break to a new line before or after + * any element. These are just common HTML elements... + */ + + element = mxmlGetElement(node); + + if (!strcmp(element, "html") || + !strcmp(element, "head") || + !strcmp(element, "body") || + !strcmp(element, "pre") || + !strcmp(element, "p") || + !strcmp(element, "h1") || + !strcmp(element, "h2") || + !strcmp(element, "h3") || + !strcmp(element, "h4") || + !strcmp(element, "h5") || + !strcmp(element, "h6")) + { + /* + * Newlines before open and after close... + */ + + if (where == MXML_WS_BEFORE_OPEN || + where == MXML_WS_AFTER_CLOSE) + return ("\n"); + } + else if (!strcmp(element, "dl") || + !strcmp(element, "ol") || + !strcmp(element, "ul")) + { + /* + * Put a newline before and after list elements... + */ + + return ("\n"); + } + else if (!strcmp(element, "dd") || + !strcmp(element, "dt") || + !strcmp(element, "li")) + { + /* + * Put a tab before
  • 's,
    's, and
    's, and a + * newline after them... + */ + + if (where == MXML_WS_BEFORE_OPEN) + return ("\t"); + else if (where == MXML_WS_AFTER_CLOSE) + return ("\n"); + } + + /* + * Otherwise return NULL for no added whitespace... + */ + + return (NULL); +} +``` To use this callback function, simply use the name when you call any of the save functions: - FILE *fp; - mxml_node_t *tree; +```c +FILE *fp; +mxml_node_t *tree; - fp = fopen("filename.xml", "w"); - mxmlSaveFile(tree, fp, whitespace_cb); - fclose(fp); +fp = fopen("filename.xml", "w"); +mxmlSaveFile(tree, fp, whitespace_cb); +fclose(fp); +``` -## Memory Management +Memory Management +----------------- Once you are done with the XML data, use the `mxmlDelete` function to recursively free the memory that is used for a particular node or the entire tree: - void - mxmlDelete(mxml_node_t *tree); +```c +void +mxmlDelete(mxml_node_t *tree); +``` You can also use reference counting to manage memory usage. The `mxmlRetain` and `mxmlRelease` functions increment and decrement a node's use count, @@ -469,57 +524,66 @@ calls `mxmlDelete` to actually free the memory used by the node tree. New nodes start with a use count of 1. -# More About Nodes +More About Nodes +================ -## Element Nodes +Element Nodes +------------- -Element \(`MXML_ELEMENT`) nodes are created using the `mxmlNewElement` function. -Element attributes are set using the `mxmlElementSetAttr` and +Element \(`MXML_TYPE_ELEMENT`) nodes are created using the `mxmlNewElement` +function. Element attributes are set using the `mxmlElementSetAttr` and `mxmlElementSetAttrf` functions and cleared using the `mxmlElementDeleteAttr` function: - mxml_node_t * - mxmlNewElement(mxml_node_t *parent, const char *name); +```c +mxml_node_t * +mxmlNewElement(mxml_node_t *parent, const char *name); - void - mxmlElementDeleteAttr(mxml_node_t *node, const char *name); +void +mxmlElementDeleteAttr(mxml_node_t *node, const char *name); - void - mxmlElementSetAttr(mxml_node_t *node, const char *name, - const char *value); +void +mxmlElementSetAttr(mxml_node_t *node, const char *name, + const char *value); - void - mxmlElementSetAttrf(mxml_node_t *node, const char *name, - const char *format, ...); +void +mxmlElementSetAttrf(mxml_node_t *node, const char *name, + const char *format, ...); +``` Child nodes are added using the various `mxmlNew` functions. The top (root) node must be an element, usually created by the `mxmlNewXML` function: - mxml_node_t * - mxmlNewXML(const char *version); +```c +mxml_node_t * +mxmlNewXML(const char *version); +``` The `mxmlGetElement` function retrieves the element name, the `mxmlElementGetAttr` function retrieves the value string for a named attribute associated with the element. The `mxmlElementGetAttrByIndex` and `mxmlElementGetAttrCount` functions retrieve attributes by index: - const char * - mxmlGetElement(mxml_node_t *node); +```c +const char * +mxmlGetElement(mxml_node_t *node); - const char * - mxmlElementGetAttr(mxml_node_t *node, const char *name); +const char * +mxmlElementGetAttr(mxml_node_t *node, const char *name); - 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); - int - mxmlElementGetAttrCount(mxml_node_t *node); +size_t +mxmlElementGetAttrCount(mxml_node_t *node); +``` -## CDATA Nodes +CDATA Nodes +----------- -CDATA \(`MXML_ELEMENT`) nodes are created using the `mxmlNewCDATA` function: +CDATA \(`MXML_TYPE_CDATA`) nodes are created using the `mxmlNewCDATA` function: mxml_node_t *mxmlNewCDATA(mxml_node_t *parent, const char *string); @@ -528,165 +592,207 @@ The `mxmlGetCDATA` function retrieves the CDATA string pointer for a node: const char *mxmlGetCDATA(mxml_node_t *node); -## Comment Nodes +Comment Nodes +------------- -Because comments are currently stored as element nodes, comment -\(`MXML_ELEMENT`) nodes are created using the `mxmlNewElement` function by -including the surrounding "!--" and "--" characters in the element name, for -example: +Comment \(`MXML_TYPE_COMMENT`) nodes are created using the `mxmlNewComment` +function, for example: - mxml_node_t *node = mxmlNewElement("!-- This is a comment --"); +```c +mxml_node_t *node = mxmlNewComment(" This is a comment "); +``` -Similarly, the `mxmlGetElement` function retrieves the comment string pointer -for a node, which includes the surrounding "!--" and "--" characters. +Similarly, the `mxmlGetComment` function retrieves the comment string pointer +for a node: - const char *comment = mxmlGetElement(node); - /* returns "!-- This is a comment --" */ +```c +const char *comment = mxmlGetComment(node); +/* returns " This is a comment " */ +``` -## Processing Instruction Nodes +Processing Instruction Nodes +---------------------------- -Because processing instructions are currently stored as element nodes, -processing instruction \(`MXML_ELEMENT`) nodes are created using the -`mxmlNewElement` function including the surrounding "?" characters: +Processing instruction \(`MXML_TYPE_DIRECTIVE`) nodes are created using the +`mxmlNewDirective` function: - mxml_node_t *node = mxmlNewElement("?xml-stylesheet type=\"text/css\" href=\"style.css\"?"); +```c +mxml_node_t *node = mxmlNewDirective("xml-stylesheet type=\"text/css\" href=\"style.css\""); +``` -The `mxmlGetElement` function retrieves the processing instruction string for a -node, including the surrounding "?" characters: +The `mxmlGetDirective` function retrieves the processing instruction string for a +node: - const char *instr = mxmlGetElement(node); - /* returned "?xml-stylesheet type=\"text/css\" href=\"style.css\"?" */ +```c +const char *instr = mxmlGetElement(node); +/* returns "xml-stylesheet type=\"text/css\" href=\"style.css\"" */ +``` -## Integer Nodes +Integer Nodes +------------- -Integer \(`MXML_INTEGER`) nodes are created using the `mxmlNewInteger` function: +Integer \(`MXML_TYPE_INTEGER`) nodes are created using the `mxmlNewInteger` +function: - mxml_node_t * - mxmlNewInteger(mxml_node_t *parent, int integer); +```c +mxml_node_t * +mxmlNewInteger(mxml_node_t *parent, long integer); +``` The `mxmlGetInteger` function retrieves the integer value for a node: - int - mxmlGetInteger(mxml_node_t *node); +```c +long +mxmlGetInteger(mxml_node_t *node); +``` -## Opaque String Nodes -Opaque string \(`MXML_OPAQUE`) nodes are created using the `mxmlNewOpaque` +Opaque String Nodes +------------------- + +Opaque string \(`MXML_TYPE_OPAQUE`) nodes are created using the `mxmlNewOpaque` function: - mxml_node_t * - mxmlNewOpaque(mxml_node_t *parent, const char *opaque); +```c +mxml_node_t * +mxmlNewOpaque(mxml_node_t *parent, const char *opaque); +``` The `mxmlGetOpaque` function retrieves the opaque string pointer for a node: - const char * - mxmlGetOpaque(mxml_node_t *node); +```c +const char * +mxmlGetOpaque(mxml_node_t *node); +``` -## Text Nodes +Text Nodes +---------- -Whitespace-delimited text string \(`MXML_TEXT`) nodes are created using the +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 flag value. +string and (leading) whitespace boolean value. - mxml_node_t * - mxmlNewText(mxml_node_t *parent, int whitespace, - const char *string); +```c +mxml_node_t * +mxmlNewText(mxml_node_t *parent, bool whitespace, + const char *string); - mxml_node_t * - mxmlNewTextf(mxml_node_t *parent, int whitespace, - const char *format, ...); +mxml_node_t * +mxmlNewTextf(mxml_node_t *parent, bool whitespace, + const char *format, ...); +``` The `mxmlGetText` function retrieves the text string pointer and whitespace -flag value for a node: +boolean value for a node: - const char * - mxmlGetText(mxml_node_t *node, int *whitespace); +```c +const char * +mxmlGetText(mxml_node_t *node, bool *whitespace); +``` -## Real Number Nodes +Real Number Nodes +-------------------- -Real number \(`MXML_REAL`) nodes are created using the `mxmlNewReal` function: +Real number \(`MXML_TYPE_REAL`) nodes are created using the `mxmlNewReal` +function: - mxml_node_t * - mxmlNewReal(mxml_node_t *parent, double real); +```c +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); +```c +double +mxmlGetReal(mxml_node_t *node); +``` -# Locating Data in an XML Document +Locating Data in an XML Document +================================ Mini-XML provides many functions for enumerating, searching, and indexing XML documents. -## Finding Nodes +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); +```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: - mxml_node_t *value = mxmlFindPath(xml, "data/group/node"); +```c +mxml_node_t *value = mxmlFindPath(xml, "data/group/node"); - mxml_node_t *value = mxmlFindPath(xml, "data/*/node"); +mxml_node_t *value = mxmlFindPath(xml, "data/*/node"); +``` The `mxmlFindElement` function can be used to find a named element, optionally matching an attribute and value: - mxml_node_t * - mxmlFindElement(mxml_node_t *node, mxml_node_t *top, - const char *element, const char *attr, - const char *value, int descend); +```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.: - /* Find the first "a" element */ - node = mxmlFindElement(tree, tree, "a", NULL, NULL, - MXML_DESCEND); +```c +/* Find the first "a" element */ +node = mxmlFindElement(tree, tree, "a", NULL, NULL, + MXML_DESCEND); - /* Find the first "a" element with "href" attribute */ - node = mxmlFindElement(tree, tree, "a", "href", NULL, - MXML_DESCEND); +/* Find the first "a" element with "href" attribute */ +node = mxmlFindElement(tree, tree, "a", "href", NULL, + MXML_DESCEND); - /* Find the first "a" element with "href" to a URL */ - node = mxmlFindElement(tree, tree, "a", "href", - "http://michaelrsweet.github.io/", - MXML_DESCEND); +/* Find the first "a" element with "href" to a URL */ +node = mxmlFindElement(tree, tree, "a", "href", + "http://michaelrsweet.github.io/", + MXML_DESCEND); - /* Find the first element with a "src" attribute*/ - node = mxmlFindElement(tree, tree, NULL, "src", NULL, - MXML_DESCEND); +/* Find the first element with a "src" attribute*/ +node = mxmlFindElement(tree, tree, NULL, "src", NULL, + MXML_DESCEND); - /* Find the first element with a "src" = "foo.jpg" */ - node = mxmlFindElement(tree, tree, NULL, "src", "foo.jpg", - MXML_DESCEND); +/* Find the first element with a "src" = "foo.jpg" */ +node = mxmlFindElement(tree, tree, NULL, "src", "foo.jpg", + MXML_DESCEND); +``` You can also iterate with the same function: - mxml_node_t *node; +```c +mxml_node_t *node; - for (node = mxmlFindElement(tree, tree, "element", NULL, - NULL, MXML_DESCEND); - node != NULL; - node = mxmlFindElement(node, tree, "element", NULL, - NULL, MXML_DESCEND)) - { - ... do something ... - } +for (node = mxmlFindElement(tree, tree, "element", NULL, + NULL, MXML_DESCEND); + node != NULL; + node = mxmlFindElement(node, tree, "element", NULL, + NULL, MXML_DESCEND)) +{ + ... do something ... +} +``` The `descend` argument \(`MXML_DESCEND` in the examples above) can be one of three constants: @@ -704,66 +810,76 @@ three constants: nodes. -## Iterating Nodes +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 * - mxmlWalkNext(mxml_node_t *node, mxml_node_t *top, - int descend); +```c +mxml_node_t * +mxmlWalkNext(mxml_node_t *node, mxml_node_t *top, + int descend); - mxml_node_t * - mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top, - int descend); +mxml_node_t * +mxmlWalkPrev(mxml_node_t *node, mxml_node_t *top, + int descend); +``` Depending on the value of the `descend` argument, these functions will automatically traverse child, sibling, and parent nodes until the `top` node is reached. For example, the following code will iterate over all of the nodes in the sample XML document in the previous section: - mxml_node_t *node; +```c +mxml_node_t *node; - for (node = xml; - node != NULL; - node = mxmlWalkNext(node, xml, MXML_DESCEND)) - { - ... do something ... - } +for (node = xml; + node != NULL; + node = mxmlWalkNext(node, xml, MXML_DESCEND)) +{ + ... do something ... +} +``` The nodes will be returned in the following order: - - - - val1 - - val2 - - val3 - - - val4 - - val5 - - val6 - - val7 - - val8 - - -## Indexing +```c + + + +val1 + +val2 + +val3 + + +val4 + +val5 + +val6 + +val7 + +val8 +``` + + +Indexing +-------- The `mxmlIndexNew` function allows you to create an index of nodes for faster searching and enumeration: - mxml_index_t * - mxmlIndexNew(mxml_node_t *node, const char *element, - const char *attr); +```c +mxml_index_t * +mxmlIndexNew(mxml_node_t *node, const char *element, + const char *attr); +``` The `element` and `attr` arguments control which elements are included in the index. If `element` is not `NULL` then only elements with the specified name @@ -774,211 +890,259 @@ in the index. For example, the following code creates an index of all "id" values in an XML document: - mxml_index_t *ind = mxmlIndexNew(xml, NULL, "id"); +```c +mxml_index_t *ind = mxmlIndexNew(xml, NULL, "id"); +``` Once the index is created, the `mxmlIndexFind` function can be used to find a matching node: - mxml_node_t * - mxmlIndexFind(mxml_index_t *ind, const char *element, - const char *value); +```c +mxml_node_t * +mxmlIndexFind(mxml_index_t *ind, const char *element, + const char *value); +``` For example, the following code will find the element whose "id" string is "42": - mxml_node_t *node = mxmlIndexFind(ind, NULL, "42"); +```c +mxml_node_t *node = mxmlIndexFind(ind, NULL, "42"); +``` Alternately, the `mxmlIndexReset` and `mxmlIndexEnum` functions can be used to enumerate the nodes in the index: - mxml_node_t * - mxmlIndexReset(mxml_index_t *ind); +```c +mxml_node_t * +mxmlIndexReset(mxml_index_t *ind); - mxml_node_t * - mxmlIndexEnum(mxml_index_t *ind); +mxml_node_t * +mxmlIndexEnum(mxml_index_t *ind); +``` Typically these functions will be used in a `for` loop: - mxml_node_t *node; +```c +mxml_node_t *node; - for (node = mxmlIndexReset(ind); - node != NULL; - node = mxmlIndexEnum(ind)) - { - ... do something ... - } +for (node = mxmlIndexReset(ind); + node != NULL; + node = mxmlIndexEnum(ind)) +{ + ... do something ... +} +``` The `mxmlIndexCount` function returns the number of nodes in the index: - int - mxmlIndexGetCount(mxml_index_t *ind); +```c +size_t +mxmlIndexGetCount(mxml_index_t *ind); +``` Finally, the `mxmlIndexDelete` function frees all memory associated with the index: - void - mxmlIndexDelete(mxml_index_t *ind); +```c +void +mxmlIndexDelete(mxml_index_t *ind); +``` -# Custom Data Types +Custom Data Types +================= -Mini-XML supports custom data types via per-thread load and save callbacks. +Mini-XML supports +-------------------- 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_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_CUSTOM`) nodes are created using the `mxmlNewCustom` function or -using a custom per-thread load callbacks specified using the +multiple +-------------------- data types as needed. The `MXML_TYPE_CUSTOM` node type +identifies +-------------------- data nodes. + +The `mxmlGetCustom` function retrieves the +-------------------- value pointer for a node. + +```c +const void * +mxmlGetCustom(mxml_node_t *node); +``` + +Custom \(`MXML_TYPE_CUSTOM`) nodes are created using the `mxmlNewCustom` +function or using a +-------------------- per-thread load callbacks specified using the `mxmlSetCustomHandlers` function: - typedef void (*mxml_custom_destroy_cb_t)(void *); - typedef int (*mxml_custom_load_cb_t)(mxml_node_t *, const char *); - typedef char *(*mxml_custom_save_cb_t)(mxml_node_t *); - - mxml_node_t * - mxmlNewCustom(mxml_node_t *parent, void *data, - mxml_custom_destroy_cb_t destroy); - - int - mxmlSetCustom(mxml_node_t *node, void *data, - mxml_custom_destroy_cb_t destroy); - - void - mxmlSetCustomHandlers(mxml_custom_load_cb_t load, - mxml_custom_save_cb_t save); +```c +typedef void (*mxml_ +--------------------_destroy_cb_t)(void *); +typedef bool (*mxml_ +--------------------_load_cb_t)(mxml_node_t *, const char *); +typedef char *(*mxml_ +--------------------_save_cb_t)(mxml_node_t *); + +mxml_node_t * +mxmlNewCustom(mxml_node_t *parent, void *data, + mxml_ +--------------------_destroy_cb_t destroy); + +int +mxmlSetCustom(mxml_node_t *node, void *data, + mxml_ +--------------------_destroy_cb_t destroy); + +void +mxmlSetCustomHandlers(mxml_ +--------------------_load_cb_t load, + mxml_ +--------------------_save_cb_t save); +``` The load callback receives a pointer to the current data node and a string of opaque character data from the XML source with character entities converted to the corresponding UTF-8 characters. For example, if we wanted to support a -custom date/time type whose value is encoded as "yyyy-mm-ddThh:mm:ssZ" (ISO -format), the load callback would look like the following: - - typedef struct - { - unsigned year, /* Year */ - month, /* Month */ - day, /* Day */ - hour, /* Hour */ - minute, /* Minute */ - second; /* Second */ - time_t unix; /* UNIX time */ - } iso_date_time_t; - - int - load_custom(mxml_node_t *node, const char *data) - { - iso_date_time_t *dt; - struct tm tmdata; - - /* - * Allocate data structure... - */ - dt = calloc(1, sizeof(iso_date_time_t)); - - /* - * Try reading 6 unsigned integers from the data string... - */ - - if (sscanf(data, "%u-%u-%uT%u:%u:%uZ", &(dt->year), - &(dt->month), &(dt->day), &(dt->hour), - &(dt->minute), &(dt->second)) != 6) - { - /* - * Unable to read numbers, free the data structure and - * return an error... - */ - - free(dt); - - return (-1); - } - - /* - * 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 (-1); - } - - /* - * Convert ISO time to UNIX time in seconds... - */ - - tmdata.tm_year = dt->year - 1900; - tmdata.tm_mon = dt->month - 1; - tmdata.tm_day = dt->day; - tmdata.tm_hour = dt->hour; - tmdata.tm_min = dt->minute; - tmdata.tm_sec = dt->second; - - dt->unix = gmtime(&tmdata); - - /* - * Assign custom node data and destroy (free) function - * pointers... - */ - - mxmlSetCustom(node, data, free); - - /* - * Return with no errors... - */ - - return (0); - } +-------------------- date/time type whose value is encoded as "yyyy-mm-ddThh:mm:ssZ" (ISO +format), the load callback would look like the following: -The function itself can return 0 on success or -1 if it is unable to decode the -custom data or the data contains an error. Custom data nodes contain a `void` -pointer to the allocated custom data for the node and a pointer to a destructor -function which will free the custom data when the node is deleted. In this -example, we use the standard `free` function since everything is contained in a -single calloc'd block. +```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; + +bool +load_ +--------------------(mxml_node_t *node, const char *data) +{ + iso_date_time_t *dt; + struct tm tmdata; + + /* + * Allocate data structure... + */ + + dt = calloc(1, sizeof(iso_date_time_t)); + + /* + * Try reading 6 unsigned integers from the data string... + */ + + if (sscanf(data, "%u-%u-%uT%u:%u:%uZ", &(dt->year), + &(dt->month), &(dt->day), &(dt->hour), + &(dt->minute), &(dt->second)) != 6) + { + /* + * Unable to read numbers, free the data structure and + * return an error... + */ + + free(dt); + + return (false); + } + + /* + * Range check values... + */ + + if (dt->month < 1 || dt->month > 12 || + dt->day < 1 || dt->day > 31 || + dt->hour < 0 || dt->hour > 23 || + dt->minute < 0 || dt->minute > 59 || + dt->second < 0 || dt->second > 60) + { + /* + * Date information is out of range... + */ + + free(dt); + + return (false); + } + + /* + * Convert ISO time to UNIX time in seconds... + */ + + tmdata.tm_year = dt->year - 1900; + tmdata.tm_mon = dt->month - 1; + tmdata.tm_day = dt->day; + tmdata.tm_hour = dt->hour; + tmdata.tm_min = dt->minute; + tmdata.tm_sec = dt->second; + + dt->unix = gmtime(&tmdata); + + /* + * Assign +-------------------- node data and destroy (free) function + * pointers... + */ + + mxmlSetCustom(node, data, free); + + /* + * Return with no errors... + */ + + return (true); +} +``` + +The function itself can return `true` on success or `false` if it is unable to +decode the +-------------------- data or the data contains an error. Custom data nodes contain +a `void` pointer to the allocated +-------------------- data for the node and a pointer to a +destructor function which will free the +-------------------- data when the node is deleted. +In this example, we use the standard `free` function since everything is +contained in a single calloc'd block. The save callback receives the node pointer and returns an allocated string -containing the custom data value. The following save callback could be used for +containing the +-------------------- data value. The following save callback could be used for our ISO date/time type: - char * - save_custom(mxml_node_t *node) - { - char data[255]; - iso_date_time_t *dt; +```c +char * +save_ +--------------------(mxml_node_t *node) +{ + char data[255]; + iso_date_time_t *dt; - dt = (iso_date_time_t *)mxmlGetCustom(node); + dt = (iso_date_time_t *)mxmlGetCustom(node); - snprintf(data, sizeof(data), - "%04u-%02u-%02uT%02u:%02u:%02uZ", - dt->year, dt->month, dt->day, dt->hour, - dt->minute, dt->second); + snprintf(data, sizeof(data), + "%04u-%02u-%02uT%02u:%02u:%02uZ", + dt->year, dt->month, dt->day, dt->hour, + dt->minute, dt->second); - return (strdup(data)); - } + return (strdup(data)); +} +``` You register the callback functions using the `mxmlSetCustomHandlers` function: - mxmlSetCustomHandlers(load_custom, save_custom); +```c +mxmlSetCustomHandlers(load_ +--------------------, save_ +--------------------); +``` -# 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 @@ -989,146 +1153,184 @@ processing. The `mxmlSAXLoadFd`, `mxmlSAXLoadFile`, and `mxmlSAXLoadString` functions provide the SAX loading APIs: - mxml_node_t * - mxmlSAXLoadFd(mxml_node_t *top, int fd, - mxml_type_t (*cb)(mxml_node_t *), - mxml_sax_cb_t sax, void *sax_data); +```c +mxml_node_t * +mxmlSAXLoadFd(mxml_node_t *top, int fd, + mxml_type_t (*cb)(mxml_node_t *), + mxml_sax_cb_t sax, void *sax_data); - mxml_node_t * - mxmlSAXLoadFile(mxml_node_t *top, FILE *fp, - mxml_type_t (*cb)(mxml_node_t *), - mxml_sax_cb_t sax, void *sax_data); +mxml_node_t * +mxmlSAXLoadFile(mxml_node_t *top, FILE *fp, + mxml_type_t (*cb)(mxml_node_t *), + mxml_sax_cb_t sax, void *sax_data); - mxml_node_t * - mxmlSAXLoadString(mxml_node_t *top, const char *s, - mxml_type_t (*cb)(mxml_node_t *), - mxml_sax_cb_t sax, void *sax_data); +mxml_node_t * +mxmlSAXLoadString(mxml_node_t *top, const char *s, + mxml_type_t (*cb)(mxml_node_t *), + mxml_sax_cb_t sax, void *sax_data); +``` Each function works like the corresponding `mxmlLoad` function but uses a callback to process each node as it is read. The callback function receives the node, an event code, and a user data pointer you supply: - void - sax_cb(mxml_node_t *node, mxml_sax_event_t event, - void *data) - { - ... do something ... - } +```c +void +sax_cb(mxml_node_t *node, mxml_sax_event_t event, + void *data) +{ + ... do something ... +} +``` The event will be one of the following: -- `MXML_SAX_CDATA`: CDATA was just read. -- `MXML_SAX_COMMENT`: A comment was just read. -- `MXML_SAX_DATA`: Data (custom, integer, opaque, real, or text) was just read. -- `MXML_SAX_DIRECTIVE`: A processing directive/instruction was just read. -- `MXML_SAX_ELEMENT_CLOSE` - A close element was just read \(``) -- `MXML_SAX_ELEMENT_OPEN` - An open element was just read \(``) +- `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. For example, the following SAX callback will retain all nodes, effectively simulating a normal in-memory load: - void - sax_cb(mxml_node_t *node, mxml_sax_event_t event, - void *data) - { - if (event != MXML_SAX_ELEMENT_CLOSE) - mxmlRetain(node); - } +```c +void +sax_cb(mxml_node_t *node, mxml_sax_event_t event, + void *data) +{ + if (event != MXML_SAX_ELEMENT_CLOSE) + mxmlRetain(node); +} +``` More typically the SAX callback will only retain a small portion of the document that is needed for post-processing. For example, the following SAX callback -will retain the title and headings in an XHTML file. It also retains the (parent) elements like ``, ``, and ``, and processing -directives like `` and ``: - - void - sax_cb(mxml_node_t *node, mxml_sax_event_t event, - void *data) +will retain the title and headings in an XHTML file. It also retains the +(parent) elements like ``, ``, and ``, and processing +directives like `` and declarations like ``: + +```c +void +sax_cb(mxml_node_t *node, mxml_sax_event_t event, + void *data) +{ + if (event == MXML_SAX_ELEMENT_OPEN) + { + /* + * Retain headings and titles... + */ + + const char *element = mxmlGetElement(node); + + if (!strcmp(element, "html") || + !strcmp(element, "head") || + !strcmp(element, "title") || + !strcmp(element, "body") || + !strcmp(element, "h1") || + !strcmp(element, "h2") || + !strcmp(element, "h3") || + !strcmp(element, "h4") || + !strcmp(element, "h5") || + !strcmp(element, "h6")) + mxmlRetain(node); + } + else if (event == MXML_SAX_DECLARATION) + mxmlRetain(node); + else if (event == MXML_SAX_DIRECTIVE) + mxmlRetain(node); + else if (event == MXML_SAX_DATA) + { + if (mxmlGetRefCount(mxmlGetParent(node)) > 1) { - if (event == MXML_SAX_ELEMENT_OPEN) - { - /* - * Retain headings and titles... - */ - - const char *element = mxmlGetElement(node); - - if (!strcmp(element, "html") || - !strcmp(element, "head") || - !strcmp(element, "title") || - !strcmp(element, "body") || - !strcmp(element, "h1") || - !strcmp(element, "h2") || - !strcmp(element, "h3") || - !strcmp(element, "h4") || - !strcmp(element, "h5") || - !strcmp(element, "h6")) - mxmlRetain(node); - } - else if (event == MXML_SAX_DIRECTIVE) - mxmlRetain(node); - else if (event == MXML_SAX_DATA) - { - if (mxmlGetRefCount(mxmlGetParent(node)) > 1) - { - /* - * If the parent was retained, then retain this data - * node as well. - */ - - mxmlRetain(node); - } - } + /* + * If the parent was retained, then retain this data + * node as well. + */ + + mxmlRetain(node); } + } +} +``` The resulting skeleton document tree can then be searched just like one loaded using the `mxmlLoad` functions. 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; +```c +mxml_node_t *doc, *title, *body, *heading; - doc = mxmlSAXLoadFd(NULL, 0, MXML_TEXT_CALLBACK, sax_cb, - NULL); +doc = mxmlSAXLoadFd(NULL, 0, MXML_TEXT_CALLBACK, sax_cb, + NULL); - title = mxmlFindElement(doc, doc, "title", NULL, NULL, - MXML_DESCEND); +title = mxmlFindElement(doc, doc, "title", NULL, NULL, + MXML_DESCEND); - if (title) - print_children(title); +if (title) + print_children(title); - body = mxmlFindElement(doc, doc, "body", NULL, NULL, - MXML_DESCEND); +body = mxmlFindElement(doc, doc, "body", NULL, NULL, + MXML_DESCEND); - if (body) - { - for (heading = mxmlGetFirstChild(body); - heading; - heading = mxmlGetNextSibling(heading)) - print_children(heading); - } +if (body) +{ + for (heading = mxmlGetFirstChild(body); + heading; + heading = mxmlGetNextSibling(heading)) + print_children(heading); +} +``` The `print_children` function is: - void - print_children(mxml_node_t *parent) - { - mxml_node_t *node; - const char *text; - int whitespace; - - for (node = mxmlGetFirstChild(parent); - node != NULL; - node = mxmlGetNextSibling(node)) - { - text = mxmlGetText(node, &whitespace); - - if (whitespace) - putchar(' '); - - fputs(text, stdout); - } - - putchar('\n'); - } +```c +void +print_children(mxml_node_t *parent) +{ + mxml_node_t *node; + const char *text; + bool whitespace; + + for (node = mxmlGetFirstChild(parent); + node != NULL; + node = mxmlGetNextSibling(node)) + { + text = mxmlGetText(node, &whitespace); + + if (whitespace) + putchar(' '); + + fputs(text, stdout); + } + + putchar('\n'); +} +``` + + +Migrating from Mini-XML v3.x +============================ + +The following incompatible API changes were made in Mini-XML v4.0: + +- SAX events are not named `MXML_SAX_EVENT_foo` instead of `MXML_SAX_foo`. +- Node types are now named `MXML_TYPE_foo` instead of `MXML_foo`. +- Functions that returned `0` on success and `-1` on error now return `true` on + success and `false` on error. +- CDATA nodes ("``") now have their own type (`MXML_TYPE_CDATA`). +- Comment nodes ("``") now have their own type + (`MXML_TYPE_COMMENT`). +- Declaration nodes ("``") now have their own type + (`MXML_TYPE_DECLARATION`). +- Processing instruction/directive nodes ("``") now have their own type + (`MXML_TYPE_DIRECTIVE`). +- Integer nodes (`MXML_TYPE_INTEGER`) now use the `long` type. +- Text nodes (`MXML_TYPE_TEXT`) now use the `bool` type for the whitespace + value. diff --git a/doc/mxml.3 b/doc/mxml.3 index 119bad7..963a10a 100644 --- a/doc/mxml.3 +++ b/doc/mxml.3 @@ -1,4 +1,4 @@ -.TH mxml 3 "Mini-XML API" "2022-07-25" "Mini-XML API" +.TH mxml 3 "Mini-XML API" "2024-03-02" "Mini-XML API" .SH NAME mxml \- Mini-XML API .SH INCLUDE FILE @@ -178,57 +178,85 @@ is used for a particular node or the entire tree: .SS mxml_sax_event_e SAX event type. .TP 5 -MXML_SAX_CDATA +MXML_SAX_EVENT_CDATA .br CDATA node .TP 5 -MXML_SAX_COMMENT +MXML_SAX_EVENT_COMMENT .br Comment node .TP 5 -MXML_SAX_DATA +MXML_SAX_EVENT_DATA .br Data node .TP 5 -MXML_SAX_DIRECTIVE +MXML_SAX_EVENT_DECLARATION .br -Processing directive node +Declaration node .TP 5 -MXML_SAX_ELEMENT_CLOSE +MXML_SAX_EVENT_DIRECTIVE +.br +Processing instruction node +.TP 5 +MXML_SAX_EVENT_ELEMENT_CLOSE .br Element closed .TP 5 -MXML_SAX_ELEMENT_OPEN +MXML_SAX_EVENT_ELEMENT_OPEN .br Element opened .SS mxml_type_e The XML node type. .TP 5 -MXML_CUSTOM +MXML_TYPE_CDATA +.br +CDATA value (" +.URL [CDATA[...]] [CDATA[...]] +") +.TP 5 +MXML_TYPE_COMMENT .br -Custom data +Comment (" +.URL !--...-- !--...-- +") .TP 5 -MXML_ELEMENT +MXML_TYPE_CUSTOM +.br +Custom data +.TP 5 +MXML_TYPE_DECLARATION +.br +Declaration (" +.URL !... !... +") +.TP 5 +MXML_TYPE_DIRECTIVE +.br +Processing instruction (" +.URL ?...? ?...? +") +.TP 5 +MXML_TYPE_ELEMENT .br XML element with attributes .TP 5 -MXML_IGNORE +MXML_TYPE_IGNORE .br -Ignore/throw away node +Ignore/throw away node .TP 5 -MXML_INTEGER +MXML_TYPE_INTEGER .br Integer value .TP 5 -MXML_OPAQUE +MXML_TYPE_OPAQUE .br Opaque string .TP 5 -MXML_REAL +MXML_TYPE_REAL .br Real value .TP 5 -MXML_TEXT +MXML_TYPE_TEXT .br Text fragment .SH FUNCTIONS @@ -270,8 +298,6 @@ void mxmlElementDeleteAttr ( const char *name ); .fi -.PP - .SS mxmlElementGetAttr Get an attribute. .PP @@ -297,18 +323,14 @@ const char * mxmlElementGetAttrByIndex ( .PP The index ("idx") is 0-based. \fBNULL\fR is returned if the specified index is out of range. - - .SS mxmlElementGetAttrCount Get the number of element attributes. .PP .nf -int mxmlElementGetAttrCount ( +size_t mxmlElementGetAttrCount ( mxml_node_t *node ); .fi -.PP - .SS mxmlElementSetAttr Set an attribute. .PP @@ -340,13 +362,11 @@ If the named attribute already exists, the value of the attribute is replaced by the new formatted string. The formatted string value is copied into the element node. This function does nothing if the node is not an element. - - .SS mxmlEntityAddCallback Add a callback to convert entities to Unicode. .PP .nf -int mxmlEntityAddCallback ( +bool mxmlEntityAddCallback ( mxml_entity_cb_t cb ); .fi @@ -369,7 +389,7 @@ int mxmlEntityGetValue ( ); .fi .PP -The entity name can also be a numeric constant. -1 is returned if the +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. @@ -417,8 +437,6 @@ considered a wildcard for one or more levels of elements. For example, .PP The first child node of the found node is returned if the given node has children and the first child is a value node. - - .SS mxmlGetCDATA Get the value for a CDATA node. .PP @@ -429,8 +447,16 @@ const char * mxmlGetCDATA ( .fi .PP \fBNULL\fR is returned if the node is not a CDATA element. - - +.SS mxmlGetComment +Get the value for a comment node. +.PP +.nf +const char * mxmlGetComment ( + mxml_node_t *node +); +.fi +.PP +\fBNULL\fR is returned if the node is not a comment. .SS mxmlGetCustom Get the value for a custom node. .PP @@ -442,8 +468,26 @@ const void * mxmlGetCustom ( .PP \fBNULL\fR is returned if the node (or its first child) is not a custom value node. - - +.SS mxmlGetDeclaration +Get the value for a declaration node. +.PP +.nf +const char * mxmlGetDeclaration ( + mxml_node_t *node +); +.fi +.PP +\fBNULL\fR is returned if the node is not a declaration. +.SS mxmlGetDirective +Get the value for a processing instruction node. +.PP +.nf +const char * mxmlGetDirective ( + mxml_node_t *node +); +.fi +.PP +\fBNULL\fR is returned if the node is not a processing instruction. .SS mxmlGetElement Get the name for an element node. .PP @@ -454,8 +498,6 @@ const char * mxmlGetElement ( .fi .PP \fBNULL\fR is returned if the node is not an element node. - - .SS mxmlGetFirstChild Get the first child of an element node. .PP @@ -467,21 +509,17 @@ mxml_node_t * mxmlGetFirstChild ( .PP \fBNULL\fR is returned if the node is not an element node or if the node has no children. - - .SS mxmlGetInteger Get the integer value from the specified node or its -first child. + first child. .PP .nf -int mxmlGetInteger ( +long mxmlGetInteger ( mxml_node_t *node ); .fi .PP -0 is returned if the node (or its first child) is not an integer value node. - - +\fB0\fR is returned if the node (or its first child) is not an integer value node. .SS mxmlGetLastChild Get the last child of an element node. .PP @@ -493,8 +531,6 @@ mxml_node_t * mxmlGetLastChild ( .PP \fBNULL\fR is returned if the node is not an element node or if the node has no children. - - .SS mxmlGetNextSibling .PP @@ -514,8 +550,6 @@ const char * mxmlGetOpaque ( .PP \fBNULL\fR is returned if the node (or its first child) is not an opaque value node. - - .SS mxmlGetParent Get the parent node. .PP @@ -526,8 +560,6 @@ mxml_node_t * mxmlGetParent ( .fi .PP \fBNULL\fR is returned for a root node. - - .SS mxmlGetPrevSibling Get the previous node for the current parent. .PP @@ -538,8 +570,6 @@ mxml_node_t * mxmlGetPrevSibling ( .fi .PP \fBNULL\fR is returned if this is the first child for the current parent. - - .SS mxmlGetReal Get the real value for a node or its first child. .PP @@ -550,13 +580,11 @@ double mxmlGetReal ( .fi .PP 0.0 is returned if the node (or its first child) is not a real value node. - - .SS mxmlGetRefCount Get the current reference (use) count for a node. .PP .nf -int mxmlGetRefCount ( +size_t mxmlGetRefCount ( mxml_node_t *node ); .fi @@ -564,15 +592,13 @@ int mxmlGetRefCount ( The initial reference count of new nodes is 1. Use the \fImxmlRetain\fR and \fImxmlRelease\fR functions to increment and decrement a node's reference count. - -\. .SS mxmlGetText Get the text value for a node or its first child. .PP .nf const char * mxmlGetText ( mxml_node_t *node, - int *whitespace + bool *whitespace ); .fi .PP @@ -580,12 +606,10 @@ const char * mxmlGetText ( The "whitespace" argument can be \fBNULL\fR. .PP Note: Text nodes consist of whitespace-delimited words. You will only get -single words of text when reading an XML file with \fBMXML_TEXT\fR nodes. +single words of text when reading an XML file with \fBMXML_TYPE_TEXT\fR nodes. If you want the entire string between elements in the XML file, you MUST read -the XML file with \fBMXML_OPAQUE\fR nodes and get the resulting strings +the XML file with \fBMXML_TYPE_OPAQUE\fR nodes and get the resulting strings using the \fImxmlGetOpaque\fR function instead. - - .SS mxmlGetType Get the node type. .PP @@ -595,9 +619,7 @@ mxml_type_t mxmlGetType ( ); .fi .PP -\fBMXML_IGNORE\fR is returned if "node" is \fBNULL\fR. - - +\fBMXML_TYPE_IGNORE\fR is returned if "node" is \fBNULL\fR. .SS mxmlGetUserData Get the user data pointer for a node. .PP @@ -606,8 +628,6 @@ void * mxmlGetUserData ( mxml_node_t *node ); .fi -.PP - .SS mxmlIndexDelete Delete an index. .PP @@ -651,8 +671,6 @@ int mxmlIndexGetCount ( mxml_index_t *ind ); .fi -.PP - .SS mxmlIndexNew Create a new index. .PP @@ -671,7 +689,7 @@ sorted by element name and optionally by attribute value if the "attr" argument is not NULL. .SS mxmlIndexReset Reset the enumeration/find pointer in the index and -return the first node in the index. + return the first node in the index. .PP .nf mxml_node_t * mxmlIndexReset ( @@ -698,8 +716,8 @@ single parent node like .URL ?xml ?xml for the entire file. The callback function returns the value type that should be used for child nodes. -The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_OPAQUE_CALLBACK\fR, -\fBMXML_REAL_CALLBACK\fR, and \fBMXML_TEXT_CALLBACK\fR are defined for +The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_TYPE_OPAQUE_CALLBACK\fR, +\fBMXML_REAL_CALLBACK\fR, and \fBMXML_TYPE_TEXT_CALLBACK\fR are defined for loading child (data) nodes of the specified type. .PP Note: The most common programming error when using the Mini-XML library is @@ -724,8 +742,8 @@ single parent node like .URL ?xml ?xml for the entire file. The callback function returns the value type that should be used for child nodes. -The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_OPAQUE_CALLBACK\fR, -\fBMXML_REAL_CALLBACK\fR, and \fBMXML_TEXT_CALLBACK\fR are defined for +The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_TYPE_OPAQUE_CALLBACK\fR, +\fBMXML_REAL_CALLBACK\fR, and \fBMXML_TYPE_TEXT_CALLBACK\fR are defined for loading child (data) nodes of the specified type. .PP Note: The most common programming error when using the Mini-XML library is @@ -750,8 +768,8 @@ single parent node like .URL ?xml ?xml for the entire string. The callback function returns the value type that should be used for child nodes. -The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_OPAQUE_CALLBACK\fR, -\fBMXML_REAL_CALLBACK\fR, and \fBMXML_TEXT_CALLBACK\fR are defined for +The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_TYPE_OPAQUE_CALLBACK\fR, +\fBMXML_REAL_CALLBACK\fR, and \fBMXML_TYPE_TEXT_CALLBACK\fR are defined for loading child (data) nodes of the specified type. .PP Note: The most common programming error when using the Mini-XML library is @@ -773,9 +791,51 @@ The new CDATA 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 CDATA node has no parent. The data string must be nul-terminated and is copied into the new node. CDATA nodes currently use the -\fBMXML_ELEMENT\fR type. - - +\fBMXML_TYPE_ELEMENT\fR type. +.SS mxmlNewCDATAf +Create a new formatted CDATA node. +.PP +.nf +mxml_node_t * mxmlNewCDATAf ( + mxml_node_t *parent, + const char *format, + ... +); +.fi +.PP +The new CDATA 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 opaque string node has no parent. The format string must be +nul-terminated and is formatted into the new node. +.SS mxmlNewComment +Create a new comment node. +.PP +.nf +mxml_node_t * mxmlNewComment ( + mxml_node_t *parent, + const char *comment +); +.fi +.PP +The new comment 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 +comment node has no parent. The comment string must be nul-terminated and +is copied into the new node. +.SS mxmlNewCommentf +Create a new formatted comment string node. +.PP +.nf +mxml_node_t * mxmlNewCommentf ( + mxml_node_t *parent, + const char *format, + ... +); +.fi +.PP +The new comment string 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 opaque string node has no parent. The format string must be +nul-terminated and is formatted into the new node. .SS mxmlNewCustom Create a new custom data node. .PP @@ -791,8 +851,64 @@ 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. - - +.SS mxmlNewDeclaration +Create a new declaraction node. +.PP +.nf +mxml_node_t * mxmlNewDeclaration ( + mxml_node_t *parent, + const char *declaration +); +.fi +.PP +The new declaration 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 +declaration node has no parent. The declaration string must be nul- +terminated and is copied into the new node. +.SS mxmlNewDeclarationf +Create a new formatted declaration node. +.PP +.nf +mxml_node_t * mxmlNewDeclarationf ( + mxml_node_t *parent, + const char *format, + ... +); +.fi +.PP +The new declaration 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 opaque string node has no parent. The format string must be +nul-terminated and is formatted into the new node. +.SS mxmlNewDirective +Create a new processing instruction node. +.PP +.nf +mxml_node_t * mxmlNewDirective ( + mxml_node_t *parent, + const char *directive +); +.fi +.PP +The new processing instruction 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 processing instruction node has no parent. The data string must +be nul-terminated and is copied into the new node. +.SS mxmlNewDirectivef +Create a new formatted processing instruction node. +.PP +.nf +mxml_node_t * mxmlNewDirectivef ( + mxml_node_t *parent, + const char *format, + ... +); +.fi +.PP +The new processing instruction 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 opaque string node has no parent. The format string must be +nul-terminated and is formatted into the new node. .SS mxmlNewElement Create a new element node. .PP @@ -812,7 +928,7 @@ Create a new integer node. .nf mxml_node_t * mxmlNewInteger ( mxml_node_t *parent, - int integer + long integer ); .fi .PP @@ -867,7 +983,7 @@ Create a new text fragment node. .nf mxml_node_t * mxmlNewText ( mxml_node_t *parent, - int whitespace, + bool whitespace, const char *string ); .fi @@ -883,7 +999,7 @@ Create a new formatted text fragment node. .nf mxml_node_t * mxmlNewTextf ( mxml_node_t *parent, - int whitespace, + bool whitespace, const char *format, ... ); @@ -904,9 +1020,7 @@ mxml_node_t * mxmlNewXML ( .fi .PP The "version" argument specifies the version number to put in the -?xml element node. If \fBNULL\fR, version "1.0" is assumed. - - +?xml directive node. If \fBNULL\fR, version "1.0" is assumed. .SS mxmlRelease Release a node. .PP @@ -918,8 +1032,6 @@ int mxmlRelease ( .PP When the reference count reaches zero, the node (and any children) is deleted via \fImxmlDelete\fR. - - .SS mxmlRemove Remove a node from its parent. .PP @@ -939,11 +1051,9 @@ int mxmlRetain ( mxml_node_t *node ); .fi -.PP - .SS mxmlSAXLoadFd Load a file descriptor into an XML node tree -using a SAX callback. + using a SAX callback. .PP .nf mxml_node_t * mxmlSAXLoadFd ( @@ -961,18 +1071,16 @@ single parent node like .URL ?xml ?xml for the entire file. The callback function returns the value type that should be used for child nodes. -The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_OPAQUE_CALLBACK\fR, -\fBMXML_REAL_CALLBACK\fR, and \fBMXML_TEXT_CALLBACK\fR are defined for +The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_TYPE_OPAQUE_CALLBACK\fR, +\fBMXML_REAL_CALLBACK\fR, and \fBMXML_TYPE_TEXT_CALLBACK\fR are defined for loading child nodes of the specified type. .PP The SAX callback must call \fImxmlRetain\fR for any nodes that need to be kept for later use. Otherwise, nodes are deleted when the parent node is closed or after each data, comment, CDATA, or directive node. - - .SS mxmlSAXLoadFile Load a file into an XML node tree -using a SAX callback. + using a SAX callback. .PP .nf mxml_node_t * mxmlSAXLoadFile ( @@ -990,18 +1098,16 @@ single parent node like .URL ?xml ?xml for the entire file. The callback function returns the value type that should be used for child nodes. -The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_OPAQUE_CALLBACK\fR, -\fBMXML_REAL_CALLBACK\fR, and \fBMXML_TEXT_CALLBACK\fR are defined for +The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_TYPE_OPAQUE_CALLBACK\fR, +\fBMXML_REAL_CALLBACK\fR, and \fBMXML_TYPE_TEXT_CALLBACK\fR are defined for loading child nodes of the specified type. .PP The SAX callback must call \fImxmlRetain\fR for any nodes that need to be kept for later use. Otherwise, nodes are deleted when the parent node is closed or after each data, comment, CDATA, or directive node. - - .SS mxmlSAXLoadString Load a string into an XML node tree -using a SAX callback. + using a SAX callback. .PP .nf mxml_node_t * mxmlSAXLoadString ( @@ -1019,15 +1125,13 @@ single parent node like .URL ?xml ?xml for the entire string. The callback function returns the value type that should be used for child nodes. -The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_OPAQUE_CALLBACK\fR, -\fBMXML_REAL_CALLBACK\fR, and \fBMXML_TEXT_CALLBACK\fR are defined for +The constants \fBMXML_INTEGER_CALLBACK\fR, \fBMXML_TYPE_OPAQUE_CALLBACK\fR, +\fBMXML_REAL_CALLBACK\fR, and \fBMXML_TYPE_TEXT_CALLBACK\fR are defined for loading child nodes of the specified type. .PP The SAX callback must call \fImxmlRetain\fR for any nodes that need to be kept for later use. Otherwise, nodes are deleted when the parent node is closed or after each data, comment, CDATA, or directive node. - - .SS mxmlSaveAllocString Save an XML tree to an allocated string. .PP @@ -1045,14 +1149,14 @@ would produce an empty string or if the string cannot be allocated. .PP The callback argument specifies a function that returns a whitespace string or \fBNULL\fR before and after each element. If \fBMXML_NO_CALLBACK\fR -is specified, whitespace will only be added before \fBMXML_TEXT\fR nodes +is specified, whitespace will only be added before \fBMXML_TYPE_TEXT\fR nodes with leading whitespace and before attribute names inside opening element tags. .SS mxmlSaveFd Save an XML tree to a file descriptor. .PP .nf -int mxmlSaveFd ( +bool mxmlSaveFd ( mxml_node_t *node, int fd, mxml_save_cb_t cb @@ -1061,14 +1165,14 @@ int mxmlSaveFd ( .PP The callback argument specifies a function that returns a whitespace string or NULL before and after each element. If \fBMXML_NO_CALLBACK\fR -is specified, whitespace will only be added before \fBMXML_TEXT\fR nodes +is specified, whitespace will only be added before \fBMXML_TYPE_TEXT\fR nodes with leading whitespace and before attribute names inside opening element tags. .SS mxmlSaveFile Save an XML tree to a file. .PP .nf -int mxmlSaveFile ( +bool mxmlSaveFile ( mxml_node_t *node, FILE *fp, mxml_save_cb_t cb @@ -1077,17 +1181,17 @@ int mxmlSaveFile ( .PP The callback argument specifies a function that returns a whitespace string or NULL before and after each element. If \fBMXML_NO_CALLBACK\fR -is specified, whitespace will only be added before \fBMXML_TEXT\fR nodes +is specified, whitespace will only be added before \fBMXML_TYPE_TEXT\fR nodes with leading whitespace and before attribute names inside opening element tags. .SS mxmlSaveString Save an XML node tree to a string. .PP .nf -int mxmlSaveString ( +size_t mxmlSaveString ( mxml_node_t *node, char *buffer, - int bufsize, + size_t bufsize, mxml_save_cb_t cb ); .fi @@ -1098,27 +1202,54 @@ into the specified buffer. .PP The callback argument specifies a function that returns a whitespace string or NULL before and after each element. If \fBMXML_NO_CALLBACK\fR -is specified, whitespace will only be added before \fBMXML_TEXT\fR nodes +is specified, whitespace will only be added before \fBMXML_TYPE_TEXT\fR nodes with leading whitespace and before attribute names inside opening element tags. .SS mxmlSetCDATA -Set the element name of a CDATA node. +Set the data for a CDATA node. .PP .nf -int mxmlSetCDATA ( +bool mxmlSetCDATA ( mxml_node_t *node, const char *data ); .fi .PP -The node is not changed if it (or its first child) is not a CDATA element node. - - +The node is not changed if it (or its first child) is not a CDATA node. +.SS mxmlSetCDATAf +Set the data for a CDATA to a formatted string. +.PP +.nf +bool mxmlSetCDATAf ( + mxml_node_t *node, + const char *format, + ... +); +.fi +.SS mxmlSetComment +Set a comment to a literal string. +.PP +.nf +bool mxmlSetComment ( + mxml_node_t *node, + const char *comment +); +.fi +.SS mxmlSetCommentf +Set a comment to a formatted string. +.PP +.nf +bool mxmlSetCommentf ( + mxml_node_t *node, + const char *format, + ... +); +.fi .SS mxmlSetCustom Set the data and destructor of a custom data node. .PP .nf -int mxmlSetCustom ( +bool mxmlSetCustom ( mxml_node_t *node, void *data, mxml_custom_destroy_cb_t destroy @@ -1126,8 +1257,6 @@ int mxmlSetCustom ( .fi .PP The node is not changed if it (or its first child) is not a custom node. - - .SS mxmlSetCustomHandlers Set the handling functions for custom data. .PP @@ -1143,11 +1272,49 @@ return 0 on success and non-zero on error. .PP The save function accepts a node pointer and must return a malloc'd string on success and \fBNULL\fR on error. +.SS mxmlSetDeclaration +Set a comment to a literal string. +.PP +.nf +bool mxmlSetDeclaration ( + mxml_node_t *node, + const char *declaration +); +.fi +.SS mxmlSetDeclarationf +Set a comment to a formatted string. +.PP +.nf +bool mxmlSetDeclarationf ( + mxml_node_t *node, + const char *format, + ... +); +.fi +.SS mxmlSetDirective +Set a directive to a literal string. +.PP +.nf +bool mxmlSetDirective ( + mxml_node_t *node, + const char *directive +); +.fi +.SS mxmlSetDirectivef +Set a directive to a formatted string. +.PP +.nf +bool mxmlSetDirectivef ( + mxml_node_t *node, + const char *format, + ... +); +.fi .SS mxmlSetElement Set the name of an element node. .PP .nf -int mxmlSetElement ( +bool mxmlSetElement ( mxml_node_t *node, const char *name ); @@ -1166,9 +1333,9 @@ void mxmlSetErrorCallback ( Set the value of an integer node. .PP .nf -int mxmlSetInteger ( +bool mxmlSetInteger ( mxml_node_t *node, - int integer + long integer ); .fi .PP @@ -1177,7 +1344,7 @@ The node is not changed if it (or its first child) is not an integer node. Set the value of an opaque node. .PP .nf -int mxmlSetOpaque ( +bool mxmlSetOpaque ( mxml_node_t *node, const char *opaque ); @@ -1188,7 +1355,7 @@ The node is not changed if it (or its first child) is not an opaque node. Set the value of an opaque string node to a formatted string. .PP .nf -int mxmlSetOpaquef ( +bool mxmlSetOpaquef ( mxml_node_t *node, const char *format, ... @@ -1196,13 +1363,11 @@ int mxmlSetOpaquef ( .fi .PP The node is not changed if it (or its first child) is not an opaque node. - - .SS mxmlSetReal Set the value of a real number node. .PP .nf -int mxmlSetReal ( +bool mxmlSetReal ( mxml_node_t *node, double real ); @@ -1213,9 +1378,9 @@ The node is not changed if it (or its first child) is not a real number node. Set the value of a text node. .PP .nf -int mxmlSetText ( +bool mxmlSetText ( mxml_node_t *node, - int whitespace, + bool whitespace, const char *string ); .fi @@ -1225,9 +1390,9 @@ The node is not changed if it (or its first child) is not a text node. Set the value of a text node to a formatted string. .PP .nf -int mxmlSetTextf ( +bool mxmlSetTextf ( mxml_node_t *node, - int whitespace, + bool whitespace, const char *format, ... ); @@ -1238,13 +1403,11 @@ The node is not changed if it (or its first child) is not a text node. Set the user data pointer for a node. .PP .nf -int mxmlSetUserData ( +bool mxmlSetUserData ( mxml_node_t *node, void *data ); .fi -.PP - .SS mxmlSetWrapMargin Set the wrap margin when saving XML data. .PP @@ -1255,8 +1418,6 @@ void mxmlSetWrapMargin ( .fi .PP Wrapping is disabled when "column" is 0. - - .SS mxmlWalkNext Walk to the next logical node in the tree. .PP @@ -1296,7 +1457,7 @@ typedef void(*)(void *) mxml_custom_destroy_cb_t; Custom data load callback function .PP .nf -typedef int(*)(mxml_node_t *, const char *) mxml_custom_load_cb_t; +typedef bool(*)(mxml_node_t *, const char *) mxml_custom_load_cb_t; .fi .SS mxml_custom_save_cb_t Custom data save callback function diff --git a/doc/mxml.epub b/doc/mxml.epub index 4d062b8..dd23593 100644 Binary files a/doc/mxml.epub and b/doc/mxml.epub differ diff --git a/doc/mxml.html b/doc/mxml.html index 105ff1e..3414cf1 100644 --- a/doc/mxml.html +++ b/doc/mxml.html @@ -1,13 +1,13 @@ -Mini-XML 3.3 Programming Manual +Mini-XML 4.0 Programming Manual - - + +