mxml(3)

NAME
mxml - Mini-XML API

INCLUDE FILE
#include <mxml.h>

LIBRARY
-lmxml

DESCRIPTION
Mini-XML is a small XML parsing library that you can use to read XML and XML-like data files in your application without requiring large non-standard libraries. Mini-XML only requires an ANSI C compatible compiler (GCC works, as do most vendors' ANSI C compilers) and a "make" program.

Mini-XML provides the following functionality:

•   Reading of UTF-8 and UTF-16 and writing of UTF-8 encoded XML files and strings.

•   Data is stored in a linked-list tree structure, preserving the XML data hierarchy.

•   Supports arbitrary element names, attributes, and attribute values with no preset limits, just available memory.

•   Supports integer, real, opaque ("CDATA"), and text data types in "leaf" nodes.

•   Functions for creating, indexing, and managing trees of data.

•   "Find" and "walk" functions for easily locating and navigating trees of data.

Mini-XML doesn't do validation or other types of processing on the data based upon schema files or other sources of definition information, nor does it support character entities other than those required by the XML specification.

USING MINI-XML
Mini-XML provides a single header file which you include:
    #include <mxml.h>

Nodes are defined by the "mxml_node_t" structure; the "type" member defines the node type (element, integer, opaque, real, or text) which determines which value you want to look at in the "value" union. New nodes can be created using the "mxmlNewElement()", "mxmlNewInteger()", "mxmlNewOpaque()", "mxmlNewReal()", and "mxmlNewText()" functions. Only elements can have child nodes, and the top node must be an element, usually "?xml".

You load an XML file using the "mxmlLoadFile()" function:
    FILE *fp;
    mxml_node_t *tree;
    fp = fopen("filename.xml", "r");
    tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
    fclose(fp);

Similarly, you save an XML file using the "mxmlSaveFile()" function:
    FILE *fp;
    mxml_node_t *tree;
    fp = fopen("filename.xml", "w");
    mxmlSaveFile(tree, fp, MXML_NO_CALLBACK);
    fclose(fp);

The "mxmlLoadString()", "mxmlSaveAllocString()", and "mxmlSaveString()" functions load XML node trees from and save XML node trees to strings:
    char buffer[8192];
    char *ptr;
    mxml_node_t *tree;
    ...
    tree = mxmlLoadString(NULL, buffer, MXML_NO_CALLBACK);
    ...
    mxmlSaveString(tree, buffer, sizeof(buffer),
                   MXML_NO_CALLBACK);
    ...
    ptr = mxmlSaveAllocString(tree, MXML_NO_CALLBACK);

You can find a named element/node using the "mxmlFindElement()" function:
    mxml_node_t *node = mxmlFindElement(tree, tree, "name",
                                        "attr", "value",
                                        MXML_DESCEND);

The "name", "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);
    /* 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://www.easysw.com/~mike/mxml/",
                           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);

You can also iterate with the same function:
    mxml_node_t *node;
    for (node = mxmlFindElement(tree, tree, "name", NULL,
                                NULL, MXML_DESCEND);
         node != NULL;
         node = mxmlFindElement(node, tree, "name", NULL,
                                NULL, MXML_DESCEND))
    {
      ... do something ...
    }

To find the value of a specific node in the tree, use the "mxmlFindPath()" function:
    mxml_node_t *value = mxmlFindPath(tree, "path/to/*/foo/bar");

The "mxmlGetInteger()", "mxmlGetOpaque()", "mxmlGetReal()", and "mxmlGetText()" functions retrieve the value from a node:
    mxml_node_t *node;
    long intvalue = mxmlGetInteger(node);
    const char *opaquevalue = mxmlGetOpaque(node);
    double realvalue = mxmlGetReal(node);
    bool whitespacevalue;
    const char *textvalue = mxmlGetText(node, &whitespacevalue);

Finally, 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:
    mxmlDelete(tree);

ENUMERATIONS

  mxml_add_e
mxmlAdd add values

MXML_ADD_AFTER
    Add node after specified node

MXML_ADD_BEFORE
    Add node before specified node

  mxml_descend_e
mxmlFindElement, mxmlWalkNext, and mxmlWalkPrev descend values

MXML_DESCEND_ALL
    Descend when finding/walking

MXML_DESCEND_FIRST
    Descend for first find

MXML_DESCEND_NONE
    Don't descend when finding/walking

  mxml_sax_event_e
SAX event type.

MXML_SAX_EVENT_CDATA
    CDATA node

MXML_SAX_EVENT_COMMENT
    Comment node

MXML_SAX_EVENT_DATA
    Data node

MXML_SAX_EVENT_DECLARATION
    Declaration node

MXML_SAX_EVENT_DIRECTIVE
    Processing instruction node

MXML_SAX_EVENT_ELEMENT_CLOSE
    Element closed

MXML_SAX_EVENT_ELEMENT_OPEN
    Element opened

  mxml_type_e
The XML node type.

MXML_TYPE_CDATA
    CDATA value (" ")

MXML_TYPE_COMMENT
    Comment (" ")

MXML_TYPE_CUSTOM
    Custom data

MXML_TYPE_DECLARATION
    Declaration (" ")

MXML_TYPE_DIRECTIVE
    Processing instruction (" ")

MXML_TYPE_ELEMENT
    XML element with attributes

MXML_TYPE_IGNORE
    Ignore/throw away node

MXML_TYPE_INTEGER
    Integer value

MXML_TYPE_OPAQUE
    Opaque string

MXML_TYPE_REAL
    Real value

MXML_TYPE_TEXT
    Text fragment

  mxml_ws_e
Whitespace periods

MXML_WS_AFTER_CLOSE
    Callback for after close tag

MXML_WS_AFTER_OPEN
    Callback for after open tag

MXML_WS_BEFORE_CLOSE
    Callback for before close tag

MXML_WS_BEFORE_OPEN
    Callback for before open tag

FUNCTIONS

  mxmlAdd
Add a node to a tree.

void mxmlAdd (
    mxml_node_t *parent,
    mxml_add_t add,
    mxml_node_t *child,
    mxml_node_t *node
);

This function adds the specified node node to the parent. If the child argument is not NULL, the new node is added before or after the specified child depending on the value of the add argument. If the child argument is NULL, the new node is placed at the beginning of the child list (MXML_ADD_BEFORE) or at the end of the child list (MXML_ADD_AFTER).

  mxmlDelete
Delete a node and all of its children.

void mxmlDelete (
    mxml_node_t *node
);

This function deletes the node node and all of its children. If the specified node has a parent, this function first removes the node from its parent using the mxmlRemove function.

  mxmlElementClearAttr
Remove an attribute from an element.

void mxmlElementClearAttr (
    mxml_node_t *node,
    const char *name
);

This function removes the attribute name from the element node.

  mxmlElementGetAttr
Get the value of an attribute.

const char * mxmlElementGetAttr (
    mxml_node_t *node,
    const char *name
);

This function gets the value for the attribute name from the element node. NULL is returned if the node is not an element or the named attribute does not exist.

  mxmlElementGetAttrByIndex
Get an attribute by index.

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

This function returned the Nth (idx) attribute for element node. The attribute name is optionallly returned in the name argument. NULL is returned if node is not an element or the specified index is out of range.

  mxmlElementGetAttrCount
Get the number of element attributes.

size_t  mxmlElementGetAttrCount (
    mxml_node_t *node
);

This function returns the number of attributes for the element node. 0 is returned if the node is not an element or there are no attributes for the element.

  mxmlElementSetAttr
Set an attribute for an element.

void mxmlElementSetAttr (
    mxml_node_t *node,
    const char *name,
    const char *value
);

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

  mxmlElementSetAttrf
Set an attribute with a formatted value.

void mxmlElementSetAttrf (
    mxml_node_t *node,
    const char *name,
    const char *format,
    ...
);

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.

  mxmlFindElement
Find the named element.

mxml_node_t * mxmlFindElement (
    mxml_node_t *node,
    mxml_node_t *top,
    const char *element,
    const char *attr,
    const char *value,
    mxml_descend_t descend
);

This function finds the named element element in XML tree top starting at node node. The search is constrained by element name element, attribute name attr, and attribute value value - NULL names or values are treated as wildcards, so different kinds of searches can be implemented by looking for all elements of a given name or all elements with a specific attribute.

The descend argument determines whether the search descends into child nodes; normally you will use MXML_DESCEND_FIRST for the initial search and MXML_DESCEND_NONE to find additional direct descendents of the node.

  mxmlFindPath
Find a node with the given path.

mxml_node_t * mxmlFindPath (
    mxml_node_t *top,
    const char *path
);

This function finds a node in XML tree top using a slash-separated list of element names in path. The name "" is considered a wildcard for one or more levels of elements, for example, "foo/one/two", "bar/two/one", "/one", and so forth.

The first child node of the found node is returned if the given node has children and the first child is a value node.

  mxmlGetCDATA
Get the value for a CDATA node.

const char * mxmlGetCDATA (
    mxml_node_t *node
);

This function gets the string value of a CDATA node. NULL is returned if the node is not a CDATA element.

  mxmlGetComment
Get the value for a comment node.

const char * mxmlGetComment (
    mxml_node_t *node
);

This function gets the string value of a comment node. NULL is returned if the node is not a comment.

  mxmlGetCustom
Get the value for a custom node.

const void * mxmlGetCustom (
    mxml_node_t *node
);

This function gets the binary value of a custom node. NULL is returned if the node (or its first child) is not a custom value node.

  mxmlGetDeclaration
Get the value for a declaration node.

const char * mxmlGetDeclaration (
    mxml_node_t *node
);

This function gets the string value of a declaraction node. NULL is returned if the node is not a declaration.

  mxmlGetDirective
Get the value for a processing instruction node.

const char * mxmlGetDirective (
    mxml_node_t *node
);

This function gets the string value of a processing instruction. NULL is returned if the node is not a processing instruction.

  mxmlGetElement
Get the name for an element node.

const char * mxmlGetElement (
    mxml_node_t *node
);

This function gets the name of an element node. NULL is returned if the node is not an element node.

  mxmlGetFirstChild
Get the first child of a node.

mxml_node_t * mxmlGetFirstChild (
    mxml_node_t *node
);

This function gets the first child of a node. NULL is returned if the node has no children.

  mxmlGetInteger
Get the integer value from the specified node or its
                     first child.

long  mxmlGetInteger (
    mxml_node_t *node
);

This function gets the value of an integer node. 0 is returned if the node (or its first child) is not an integer value node.

  mxmlGetLastChild
Get the last child of a node.

mxml_node_t * mxmlGetLastChild (
    mxml_node_t *node
);

This function gets the last child of a node. NULL is returned if the node has no children.

  mxmlGetNextSibling

mxml_node_t * mxmlGetNextSibling (
    mxml_node_t *node
);

  mxmlGetOpaque
Get an opaque string value for a node or its first child.

const char * mxmlGetOpaque (
    mxml_node_t *node
);

This function gets the string value of an opaque node. NULL is returned if the node (or its first child) is not an opaque value node.

  mxmlGetParent
Get the parent node.

mxml_node_t * mxmlGetParent (
    mxml_node_t *node
);

This function gets the parent of a node. NULL is returned for a root node.

  mxmlGetPrevSibling
Get the previous node for the current parent.

mxml_node_t * mxmlGetPrevSibling (
    mxml_node_t *node
);

This function gets the previous node for the current parent. NULL is returned if this is the first child for the current parent.

  mxmlGetReal
Get the real value for a node or its first child.

double  mxmlGetReal (
    mxml_node_t *node
);

This function gets the value of a real value node. 0.0 is returned if the node (or its first child) is not a real value node.

  mxmlGetRefCount
Get the current reference (use) count for a node.

size_t  mxmlGetRefCount (
    mxml_node_t *node
);

The initial reference count of new nodes is 1. Use the mxmlRetain and mxmlRelease functions to increment and decrement a node's reference count.

  mxmlGetText
Get the text value for a node or its first child.

const char * mxmlGetText (
    mxml_node_t *node,
    bool *whitespace
);

This function gets the string and whitespace values of a text node. NULL and false are returned if the node (or its first child) is not a text node. The whitespace argument can be NULL if you don't want to know the whitespace value.

Note: Text nodes consist of whitespace-delimited words. You will only get single words of text when reading an XML file with MXML_TYPE_TEXT nodes. If you want the entire string between elements in the XML file, you MUST read the XML file with MXML_TYPE_OPAQUE nodes and get the resulting strings using the mxmlGetOpaque function instead.

  mxmlGetType
Get the node type.

mxml_type_t  mxmlGetType (
    mxml_node_t *node
);

This function gets the type of node. MXML_TYPE_IGNORE is returned if node is NULL.

  mxmlGetUserData
Get the user data pointer for a node.

void * mxmlGetUserData (
    mxml_node_t *node
);

This function gets the user data pointer associated with node.

  mxmlIndexDelete
Delete an index.

void mxmlIndexDelete (
    mxml_index_t *ind
);

  mxmlIndexEnum
Return the next node in the index.

mxml_node_t * mxmlIndexEnum (
    mxml_index_t *ind
);

This function returns the next node in index ind.

You should call mxmlIndexReset prior to using this function to get the first node in the index. Nodes are returned in the sorted order of the index.

  mxmlIndexFind
Find the next matching node.

mxml_node_t * mxmlIndexFind (
    mxml_index_t *ind,
    const char *element,
    const char *value
);

This function finds the next matching node in index ind.

You should call mxmlIndexReset prior to using this function for the first time with a particular set of element and value strings. Passing NULL for both element and value is equivalent to calling mxmlIndexEnum.

  mxmlIndexGetCount
Get the number of nodes in an index.

size_t  mxmlIndexGetCount (
    mxml_index_t *ind
);

  mxmlIndexNew
Create a new index.

mxml_index_t * mxmlIndexNew (
    mxml_node_t *node,
    const char *element,
    const char *attr
);

This function creates a new index for XML tree node.

The index will contain all nodes that contain the named element and/or attribute. If both element and attr are NULL, then the index will contain a sorted list of the elements in the node tree. Nodes are sorted by element name and optionally by attribute value if the attr argument is not NULL.

  mxmlIndexReset
Reset the enumeration/find pointer in the index and
                     return the first node in the index.

mxml_node_t * mxmlIndexReset (
    mxml_index_t *ind
);

This function resets the enumeration/find pointer in index ind and should be called prior to using mxmlIndexEnum or mxmlIndexFind for the first time.

  mxmlLoadFd
Load a file descriptor into an XML node tree.

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

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

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,
    mxml_options_t *options,
    FILE *fp
);

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

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,
    mxml_options_t *options,
    const char *filename
);

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

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_options_t *options,
    mxml_io_cb_t io_cb,
    void *io_cbdata
);

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

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 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 0 on error ... } `

  mxmlLoadString
Load a string into an XML node tree.

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

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

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.

mxml_node_t * mxmlNewCDATA (
    mxml_node_t *parent,
    const char *data
);

The new CDATA node is added to the end of the specified parent's child list. The constant NULL 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.

  mxmlNewCDATAf
Create a new formatted CDATA node.

mxml_node_t * mxmlNewCDATAf (
    mxml_node_t *parent,
    const char *format,
    ...
);

The new CDATA node is added to the end of the specified parent's child list. The constant NULL 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.

  mxmlNewComment
Create a new comment node.

mxml_node_t * mxmlNewComment (
    mxml_node_t *parent,
    const char *comment
);

The new comment node is added to the end of the specified parent's child list. The constant NULL 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.

  mxmlNewCommentf
Create a new formatted comment string node.

mxml_node_t * mxmlNewCommentf (
    mxml_node_t *parent,
    const char *format,
    ...
);

The new comment string node is added to the end of the specified parent's child list. The constant NULL 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.

  mxmlNewCustom
Create a new custom data node.

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

The new custom node is added to the end of the specified parent's child 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.

mxml_node_t * mxmlNewDeclaration (
    mxml_node_t *parent,
    const char *declaration
);

The new declaration node is added to the end of the specified parent's child list. The constant NULL 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.

  mxmlNewDeclarationf
Create a new formatted declaration node.

mxml_node_t * mxmlNewDeclarationf (
    mxml_node_t *parent,
    const char *format,
    ...
);

The new declaration node is added to the end of the specified parent's child list. The constant NULL 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.

  mxmlNewDirective
Create a new processing instruction node.

mxml_node_t * mxmlNewDirective (
    mxml_node_t *parent,
    const char *directive
);

The new processing instruction node is added to the end of the specified parent's child list. The constant NULL 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.

  mxmlNewDirectivef
Create a new formatted processing instruction node.

mxml_node_t * mxmlNewDirectivef (
    mxml_node_t *parent,
    const char *format,
    ...
);

The new processing instruction node is added to the end of the specified parent's child list. The constant NULL 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.

  mxmlNewElement
Create a new element node.

mxml_node_t * mxmlNewElement (
    mxml_node_t *parent,
    const char *name
);

The new element node is added to the end of the specified parent's child list. The constant NULL can be used to specify that the new element node has no parent.

  mxmlNewInteger
Create a new integer node.

mxml_node_t * mxmlNewInteger (
    mxml_node_t *parent,
    long integer
);

The new integer node is added to the end of the specified parent's child list. The constant NULL can be used to specify that the new integer node has no parent.

  mxmlNewOpaque
Create a new opaque string.

mxml_node_t * mxmlNewOpaque (
    mxml_node_t *parent,
    const char *opaque
);

The new opaque string node is added to the end of the specified parent's child list. The constant NULL can be used to specify that the new opaque string node has no parent. The opaque string must be nul-terminated and is copied into the new node.

  mxmlNewOpaquef
Create a new formatted opaque string node.

mxml_node_t * mxmlNewOpaquef (
    mxml_node_t *parent,
    const char *format,
    ...
);

The new opaque string node is added to the end of the specified parent's child list. The constant NULL 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.

  mxmlNewReal
Create a new real number node.

mxml_node_t * mxmlNewReal (
    mxml_node_t *parent,
    double real
);

The new real number node is added to the end of the specified parent's child list. The constant NULL can be used to specify that the new real number node has no parent.

  mxmlNewText
Create a new text fragment node.

mxml_node_t * mxmlNewText (
    mxml_node_t *parent,
    bool whitespace,
    const char *string
);

The new text node is added to the end of the specified parent's child list. The constant NULL can be used to specify that the new text node has no parent. The whitespace parameter is used to specify whether leading whitespace is present before the node. The text string must be nul-terminated and is copied into the new node.

  mxmlNewTextf
Create a new formatted text fragment node.

mxml_node_t * mxmlNewTextf (
    mxml_node_t *parent,
    bool whitespace,
    const char *format,
    ...
);

The new text node is added to the end of the specified parent's child list. The constant NULL can be used to specify that the new text node has no parent. The whitespace parameter is used to specify whether leading whitespace is present before the node. The format string must be nul-terminated and is formatted into the new node.

  mxmlNewXML
Create a new XML document tree.

mxml_node_t * mxmlNewXML (
    const char *version
);

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

  mxmlOptionsNew
Allocate load/save options.

mxml_options_t * mxmlOptionsNew (void);

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:

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

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 month > 12 || dt->day day > 31 ||
      dt->hour hour > 23 || dt->minute minute > 59 ||
      dt->second 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
);

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

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

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

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

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

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

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

  mxmlRelease
Release a node.

int  mxmlRelease (
    mxml_node_t *node
);

When the reference count reaches zero, the node (and any children) is deleted via mxmlDelete.

  mxmlRemove
Remove a node from its parent.

void mxmlRemove (
    mxml_node_t *node
);

This function does not free memory used by the node - use mxmlDelete for that. This function does nothing if the node has no parent.

  mxmlRetain
Retain a node.

int  mxmlRetain (
    mxml_node_t *node
);

  mxmlSaveAllocString
Save an XML tree to an allocated string.

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

This function saves the XML tree node to an allocated string. The string should be freed using free (or the string free callback set using mxmlSetStringCallbacks) when you are done with it.

NULL is returned if the node would produce an empty string or if the string cannot be allocated.

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,
    mxml_options_t *options,
    int fd
);

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

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,
    mxml_options_t *options,
    FILE *fp
);

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

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,
    mxml_options_t *options,
    const char *filename
);

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

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_options_t *options,
    mxml_io_cb_t io_cb,
    void *io_cbdata
);

This function saves the XML tree node using a write callback function 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 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 0 on error ... }

    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 (MXML_TYPE_TEXT); } `

  mxmlSaveString
Save an XML node tree to a string.

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

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

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.

bool  mxmlSetCDATA (
    mxml_node_t *node,
    const char *data
);

This function sets the value string for a CDATA node. The node is not changed if it (or its first child) is not a CDATA node.

  mxmlSetCDATAf
Set the data for a CDATA to a formatted string.

bool  mxmlSetCDATAf (
    mxml_node_t *node,
    const char *format,
    ...
);

This function sets the formatted string value of a CDATA node. The node is not changed if it (or its first child) is not a CDATA node.

  mxmlSetComment
Set a comment to a literal string.

bool  mxmlSetComment (
    mxml_node_t *node,
    const char *comment
);

This function sets the string value of a comment node.

  mxmlSetCommentf
Set a comment to a formatted string.

bool  mxmlSetCommentf (
    mxml_node_t *node,
    const char *format,
    ...
);

This function sets the formatted string value of a comment node.

  mxmlSetCustom
Set the data and destructor of a custom data node.

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

This function sets the data pointer data and destructor callback destroy_cb of a custom data node. The node is not changed if it (or its first child) is not a custom node.

  mxmlSetDeclaration
Set a declaration to a literal string.

bool  mxmlSetDeclaration (
    mxml_node_t *node,
    const char *declaration
);

This function sets the string value of a declaration node.

  mxmlSetDeclarationf
Set a declaration to a formatted string.

bool  mxmlSetDeclarationf (
    mxml_node_t *node,
    const char *format,
    ...
);

This function sets the formatted string value of a declaration node.

  mxmlSetDirective
Set a processing instruction to a literal string.

bool  mxmlSetDirective (
    mxml_node_t *node,
    const char *directive
);

This function sets the string value of a processing instruction node.

  mxmlSetDirectivef
Set a processing instruction to a formatted string.

bool  mxmlSetDirectivef (
    mxml_node_t *node,
    const char *format,
    ...
);

This function sets the formatted string value of a processing instruction node.

  mxmlSetElement
Set the name of an element node.

bool  mxmlSetElement (
    mxml_node_t *node,
    const char *name
);

This function sets the name of an element node. The node is not changed if it is not an element node.

  mxmlSetInteger
Set the value of an integer node.

bool  mxmlSetInteger (
    mxml_node_t *node,
    long integer
);

This function sets the value of an integer node. The node is not changed if it (or its first child) is not an integer node.

  mxmlSetOpaque
Set the value of an opaque node.

bool  mxmlSetOpaque (
    mxml_node_t *node,
    const char *opaque
);

This function sets the string value of an opaque node. The node is not changed if it (or its first child) is not an opaque node.

  mxmlSetOpaquef
Set the value of an opaque string node to a formatted string.

bool  mxmlSetOpaquef (
    mxml_node_t *node,
    const char *format,
    ...
);

This function sets the formatted string value of an opaque node. The node is not changed if it (or its first child) is not an opaque node.

  mxmlSetReal
Set the value of a real value node.

bool  mxmlSetReal (
    mxml_node_t *node,
    double real
);

This function sets the value of a real value node. The node is not changed if it (or its first child) is not a real value node.

  mxmlSetText
Set the value of a text node.

bool  mxmlSetText (
    mxml_node_t *node,
    bool whitespace,
    const char *string
);

This function sets the string and whitespace values of a text node. The node is not changed if it (or its first child) is not a text node.

  mxmlSetTextf
Set the value of a text node to a formatted string.

bool  mxmlSetTextf (
    mxml_node_t *node,
    bool whitespace,
    const char *format,
    ...
);

This function sets the formatted string and whitespace values of a text node. The node is not changed if it (or its first child) is not a text node.

  mxmlSetUserData
Set the user data pointer for a node.

bool  mxmlSetUserData (
    mxml_node_t *node,
    void *data
);

  mxmlWalkNext
Walk to the next logical node in the tree.

mxml_node_t * mxmlWalkNext (
    mxml_node_t *node,
    mxml_node_t *top,
    mxml_descend_t descend
);

This function walks to the next logical node in the tree. The descend argument controls whether the first child is considered to be the next node. The top argument constrains the walk to that node's children.

  mxmlWalkPrev
Walk to the previous logical node in the tree.

mxml_node_t * mxmlWalkPrev (
    mxml_node_t *node,
    mxml_node_t *top,
    mxml_descend_t descend
);

This function walks to the previous logical node in the tree. The descend argument controls whether the first child is considered to be the next node. The top argument constrains the walk to that node's children.

TYPES

  mxml_add_t
mxmlAdd add values

typedef enum mxml_add_e mxml_add_t;

  mxml_custfree_cb_t
Custom data destructor

typedef void(*)(void *cbdata void *custdata) mxml_custfree_cb_t;

  mxml_custload_cb_t
Custom data load callback function

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

  mxml_custsave_cb_t
Custom data save callback function

typedef char *(*)(void *cbdata mxml_node_t *node) mxml_custsave_cb_t;

  mxml_descend_t
mxmlFindElement, mxmlWalkNext, and mxmlWalkPrev descend values

typedef enum mxml_descend_e mxml_descend_t;

  mxml_entity_cb_t
Entity callback function

typedef int(*)(void *cbdata const char *name) mxml_entity_cb_t;

  mxml_error_cb_t
Error callback function

typedef void(*)(void *cbdata const char *message) mxml_error_cb_t;

  mxml_index_t
An XML node index

typedef struct _mxml_index_s mxml_index_t;

  mxml_io_cb_t
Read/write callback function

typedef size_t(*)(void *cbdata void *buffer size_t bytes) mxml_io_cb_t;

  mxml_node_t
An XML node

typedef struct _mxml_node_s mxml_node_t;

  mxml_options_t
XML options

typedef struct _mxml_options_s mxml_options_t;

  mxml_sax_cb_t
SAX callback function

typedef bool(*)(void *cbdata mxml_node_t *node mxml_sax_event_t event) mxml_sax_cb_t;

  mxml_sax_event_t
SAX event type.

typedef enum mxml_sax_event_e mxml_sax_event_t;

  mxml_strcopy_cb_t
String copy/allocation callback

typedef char *(*)(void *cbdata const char *s) mxml_strcopy_cb_t;

  mxml_strfree_cb_t
String free callback

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

  mxml_type_cb_t
Type callback function

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

  mxml_type_t
The XML node type.

typedef enum mxml_type_e mxml_type_t;

  mxml_ws_cb_t
Whitespace callback function

typedef const char *(*)(void *cbdata mxml_node_t *node mxml_ws_t when) mxml_ws_cb_t;

  mxml_ws_t
Whitespace periods

typedef enum mxml_ws_e mxml_ws_t;

SEE ALSO
Mini-XML Programmers Manual, https://www.msweet.org/mxml

COPYRIGHT
Copyright © 2003-2021 by Michael R Sweet.