mxml(3)

πŸ“¦
version 3.3.1 Β· 2 versions
, in alpine 3.24, arch rolling, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0

πŸ’– mxml(3) β€” Mini-XML API πŸ’–

🌸 NAME 🌸

mxml β€” Mini-XML API, bestie. This is the whole little XML-parsing era.


πŸ“₯ 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. No giant dependency drama. 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.
🫢 Heads up, bestie: 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. It's giving "parser, not validator," and that's the vibe.

πŸ› οΈ 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;

int intvalue = mxmlGetInteger(node);

const char *opaquevalue = mxmlGetOpaque(node);

double realvalue = mxmlGetReal(node);

int 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_sax_event_e

SAX event type. Periodt.

  • MXML_SAX_CDATA β€” CDATA node
  • MXML_SAX_COMMENT β€” Comment node
  • MXML_SAX_DATA β€” Data node
  • MXML_SAX_DIRECTIVE β€” Processing directive node
  • MXML_SAX_ELEMENT_CLOSE β€” Element closed
  • MXML_SAX_ELEMENT_OPEN β€” Element opened

πŸ¦‹ mxml_type_e

The XML node type. Your little type check moment.

  • MXML_CUSTOM β€” Custom data
  • MXML_ELEMENT β€” XML element with attributes
  • MXML_IGNORE β€” Ignore/throw away node
  • MXML_INTEGER β€” Integer value
  • MXML_OPAQUE β€” Opaque string
  • MXML_REAL β€” Real value
  • MXML_TEXT β€” Text fragment

πŸ”₯ FUNCTIONS πŸ”₯

🌟 mxmlAdd

Add a node to a tree.

void mxmlAdd (
    mxml_node_t *parent,
    int where,
    mxml_node_t *child,
    mxml_node_t *node
);

Adds the specified node to the parent. If the child argument is not NULL, puts the new node before or after the specified child depending on the value of the where argument. If the child argument is NULL, puts the new node at the beginning of the child list (MXML_ADD_BEFORE) or at the end of the child list (MXML_ADD_AFTER). The constant MXML_ADD_TO_PARENT can be used to specify a NULL child pointer. Slay those tree placements.

🌟 mxmlDelete

Delete a node and all of its children.

void mxmlDelete (
    mxml_node_t *node
);

If the specified node has a parent, this function first removes the node from its parent using the mxmlRemove function. Cleanup era.

🌟 mxmlElementDeleteAttr

Delete an attribute.

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

🌟 mxmlElementGetAttr

Get an attribute.

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

This function returns NULL if the node is not an element or the named attribute does not exist.

🌟 mxmlElementGetAttrByIndex

Get an element attribute by index.

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

The index (idx) is 0-based. NULL is returned if the specified index is out of range.

🌟 mxmlElementGetAttrCount

Get the number of element attributes.

int  mxmlElementGetAttrCount (
    mxml_node_t *node
);

🌟 mxmlElementSetAttr

Set an attribute.

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

If the named attribute already exists, the value of the attribute is replaced by the new string value. The string value is copied into the element node. This function does nothing if the node is not an element.

🌟 mxmlElementSetAttrf

Set an attribute with a formatted value.

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

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.

🌟 mxmlEntityAddCallback

Add a callback to convert entities to Unicode.

int  mxmlEntityAddCallback (
    mxml_entity_cb_t cb
);

🌟 mxmlEntityGetName

Get the name that corresponds to the character value.

const char * mxmlEntityGetName (
    int val
);

If val does not need to be represented by a named entity, NULL is returned.

🌟 mxmlEntityGetValue

Get the character corresponding to a named entity.

int  mxmlEntityGetValue (
    const char *name
);

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

🌟 mxmlEntityRemoveCallback

Remove a callback.

void mxmlEntityRemoveCallback (
    mxml_entity_cb_t cb
);

🌟 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,
    int descend
);

The search is constrained by the name, attribute name, and value; any NULL names or values are treated as wildcards, so different kinds of searches can be implemented by looking for all elements of a given name or all elements with a specific attribute. The descend argument determines whether the search descends into child nodes; normally you will use MXML_DESCEND_FIRST for the initial search and MXML_NO_DESCEND to find additional direct descendents of the node. The top node argument constrains the search to a particular node's children.

🌟 mxmlFindPath

Find a node with the given path.

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

The path is a slash-separated list of element names. The name "" is considered a wildcard for one or more levels of elements. For example, "foo/one/two", "bar/two/one", "/one", and so forth.

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

🌟 mxmlGetCDATA

Get the value for a CDATA node.

const char * mxmlGetCDATA (
    mxml_node_t *node
);

NULL is returned if the node is not a CDATA element.

🌟 mxmlGetCustom

Get the value for a custom node.

const void * mxmlGetCustom (
    mxml_node_t *node
);

NULL is returned if the node (or its first child) is not a custom value node.

🌟 mxmlGetElement

Get the name for an element node.

const char * mxmlGetElement (
    mxml_node_t *node
);

NULL is returned if the node is not an element node.

🌟 mxmlGetFirstChild

Get the first child of an element node.

mxml_node_t * mxmlGetFirstChild (
    mxml_node_t *node
);

Heads up bestie: you get NULL back if node isn't an element node, or if it's an element with zero children. Empty house, nobody home 🏚️

mxmlGetInteger

Grabs the integer value from the node you point at β€” or from its first child if the node itself isn't the integer. Sneaky but iconic ✨

int mxmlGetInteger (
    mxml_node_t *node
);

0 comes back if the node (or its first child) is not an integer value node. Zero. Nothing. Nada 😭

mxmlGetLastChild

Gets you the last child of an element node β€” the baby of the family, main character of the tail end πŸ’…

mxml_node_t * mxmlGetLastChild (
    mxml_node_t *node
);

NULL if the node isn't an element node, or if it has no children at all. No kids, no last kid 🫢

mxmlGetNextSibling

mxml_node_t * mxmlGetNextSibling (
    mxml_node_t *node
);

mxmlGetOpaque

Gets you an opaque string value for a node or its first child. All the raw vibes, preserved exactly 🌸

const char * mxmlGetOpaque (
    mxml_node_t *node
);

NULL is returned if the node (or its first child) is not an opaque value node. So if you're getting NULL back, check your types, not your luck πŸ¦‹

mxmlGetParent

Gets the parent node. The one who raised it, literally πŸ’œ

mxml_node_t * mxmlGetParent (
    mxml_node_t *node
);

NULL comes back for a root node β€” roots have no parent, they're self-made πŸ‘‘

mxmlGetPrevSibling

Gets the previous node for the current parent. The one walking behind you in the group chat lineup πŸŽ€

mxml_node_t * mxmlGetPrevSibling (
    mxml_node_t *node
);

NULL is returned if this is the first child for the current parent. Somebody has to be first bestie 🌈

mxmlGetReal

Get the real (as in double, as in float, as in decimals) value for a node or its first child πŸ”₯

double mxmlGetReal (
    mxml_node_t *node
);

0.0 is returned if the node (or its first child) is not a real value node. Zero point zero, no cap πŸ’€

mxmlGetRefCount

Get the current reference (use) count for a node. The node's little popularity tally 🫢

int 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 β€” that's the whole lifecycle arc bestie ⭐

mxmlGetText

Get the text value for a node or its first child πŸ“

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

NULL is returned if the node (or its first child) is not a text node. The whitespace argument can be NULL β€” so you're allowed to not care ✨

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

That's the classic trap, fr fr β€” read carefully and save yourself a whole debugging era 😭

mxmlGetType

Get the node type. What is this node, actually? Identity crisis resolved πŸ’…

mxml_type_t mxmlGetType (
    mxml_node_t *node
);

MXML_IGNORE is returned if node is NULL. Yes, even nothingness gets a type πŸ’€

mxmlGetUserData

Get the user data pointer for a node. Your own little stashed trinket, untouched and returned as-is πŸŽ€

void * mxmlGetUserData (
    mxml_node_t *node
);

mxmlIndexDelete

Delete an index. It's gone, it's over, we don't talk about it πŸ’…

void mxmlIndexDelete (
    mxml_index_t *ind
);

mxmlIndexEnum

Return the next node in the index β€” one at a time, like a very organized little queue πŸ¦‹

mxml_node_t * mxmlIndexEnum (
    mxml_index_t *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 β€” so yes, it's alphabetical-coded ✨

mxmlIndexFind

Find the next matching node. The search function that actually knows where things are πŸ”

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

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 β€” same energy, different entrance 🌈

mxmlIndexGetCount

Get the number of nodes in an index. A headcount, basically πŸ’–

int mxmlIndexGetCount (
    mxml_index_t *ind
);

mxmlIndexNew

Create a new index. Fresh index, fresh start, new era ✨

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

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. Sorted and serving πŸ‘‘

mxmlIndexReset

Reset the enumeration/find pointer in the index and return the first node in the index. Back to the beginning, bestie πŸ”

mxml_node_t * mxmlIndexReset (
    mxml_index_t *ind
);

This function should be called prior to using mxmlIndexEnum or mxmlIndexFind for the first time. Don't skip it, it's giving necessary πŸ’€


mxmlLoadFd

Load a file descriptor into an XML node tree. Feed it a raw fd and let it cook πŸ“

mxml_node_t * mxmlLoadFd (
    mxml_node_t *top,
    int fd,
    mxml_load_cb_t cb
);

The nodes in the specified file are added to the specified top node. If no top node is provided, the XML file MUST be well-formed with a single parent node for the entire file. The callback function returns the value type that should be used for child nodes. The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK, MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading child (data) nodes of the specified type. Pick your callback like you pick an outfit β€” with intention πŸ’…

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

This is the rookie mistake, no cap. Say it with me: MXML_OPAQUE_CALLBACK for whole strings 😀

mxmlLoadFile

Load a file into an XML node tree. Classic FILE * era, we love a throwback πŸŽ€

mxml_node_t * mxmlLoadFile (
    mxml_node_t *top,
    FILE *fp,
    mxml_load_cb_t cb
);

The nodes in the specified file are added to the specified top node. If no top node is provided, the XML file MUST be well-formed with a single parent node for the entire file. The callback function returns the value type that should be used for child nodes. The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK, MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading child (data) nodes of the specified type 🌸

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

mxmlLoadString

Load a string into an XML node tree. Just a const char *, no filesystem drama πŸ¦‹

mxml_node_t * mxmlLoadString (
    mxml_node_t *top,
    const char *s,
    mxml_load_cb_t cb
);

The nodes in the specified string are added to the specified top node. If no top node is provided, the XML string MUST be well-formed with a single parent node for the entire string. The callback function returns the value type that should be used for child nodes. The constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK, MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading child (data) nodes of the specified type πŸ’œ

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

Yes, it's the third time. Yes, it's still true. It's giving "the library is telling you something" 😭


mxmlNewCDATA

Create a new CDATA node. Raw, unescaped, unfiltered content β€” the rebel of the node types πŸ”₯

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 MXML_NO_PARENT can be used to specify that the new CDATA node has no parent. The data string must be nul-terminated and is copied into the new node. CDATA nodes currently use the MXML_ELEMENT type β€” yes, currently, don't @ me πŸ’€

mxmlNewCustom

Create a new custom data node. Bring your own data, bring your own destruction ✨

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

The new custom node is added to the end of the specified parent's child list. The constant MXML_NO_PARENT can be used to specify that the new element node has no parent. NULL can be passed when the data in the node is not dynamically allocated or is separately managed β€” i.e. when you got this handled yourself πŸ’…

mxmlNewElement

Create a new element node. The building block, the moment the tree starts existing 🌱

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 MXML_NO_PARENT can be used to specify that the new element node has no parent. Free-range element, feral and thriving πŸ¦‹

mxmlNewInteger

Create a new integer node. Whole numbers only, no decimals allowed in this household πŸ”’

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

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

mxmlNewOpaque

Create a new opaque string. The string stays exactly as you wrote it β€” whitespace and all, bestie 🌸

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 MXML_NO_PARENT can be used to specify that the new opaque string node has no parent. The opaque string must be nul-terminated and is copied into the new node β€” copied, so your original is safe πŸ’œ

mxmlNewOpaquef

Create a new formatted opaque string node. printf energy, but for opaque strings ✨

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 MXML_NO_PARENT can be used to specify that the new opaque string node has no parent. The format string must be nul-terminated and is formatted into the new node. Format away, just keep it nul-terminated πŸ™ˆ

mxmlNewReal

Create a new real number node. Decimals get a node too, equality πŸ’…

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 MXML_NO_PARENT can be used to specify that the new real number node has no parent 🫢

mxmlNewText

Create a new text fragment node. A little slice of text, with a whitespace flag riding along ⭐

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

The new text node is added to the end of the specified parent's child list. The constant MXML_NO_PARENT can be used to specify that the new text node has no parent. The whitespace parameter is used to specify whether leading whitespace is present before the node. The text string must be nul-terminated and is copied into the new node β€” copied, again, we love a safe copy 🌈

mxmlNewTextf

Create a new formatted text fragment node. Same text fragment energy, but with ... args doing the work πŸŽ€

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

The new text node gets tacked onto the end of the parent's child list. Need it parentless? Pass the constant MXML_NO_PARENT and it'll be a free agent πŸ’…. The whitespace parameter is there to tell it whether there's leading whitespace hanging out before the node. And the format string? Gotta be nul-terminated, bestie, and it gets formatted into the new node. No exceptions ✨

πŸ†• mxmlNewXML

Guess what β€” you're making a whole new XML document tree. Main character energy 🌈

mxml_node_t * mxmlNewXML (
    const char *version
);

The "version" argument is literally the version number that goes into the ?xml element node. Pass NULL and it just assumes version "1.0" β€” lowkey convenient πŸŽ€

πŸ“‰ mxmlRelease

Release a node. Let it go, bestie πŸ¦‹

int  mxmlRelease (
    mxml_node_t *node
);

And the moment the reference count hits zero, the node (plus all its children, rip 😭) gets deleted via mxmlDelete.

βœ‚οΈ mxmlRemove

Unhook a node from its parent. Toxic relationship? Ended πŸ’…

void mxmlRemove (
    mxml_node_t *node
);
⚠️ Heads up: this does not free the memory the node is using β€” if you want it gone-gone, use mxmlDelete for that. Also, if the node has no parent, this function simply does nothing. No drama. 🫢

πŸ“Œ mxmlRetain

Retain a node β€” the mirror-move to mxmlRelease ✨

int  mxmlRetain (
    mxml_node_t *node
);

🌊 mxmlSAXLoadFd

Load a file descriptor into an XML node tree using a SAX callback. Stream it, don't hoard it 🧚

mxml_node_t * mxmlSAXLoadFd (
    mxml_node_t *top,
    int fd,
    mxml_load_cb_t cb,
    mxml_sax_cb_t sax_cb,
    void *sax_data
);

The nodes in the specified file get added to the specified top node. If you don't provide a top node, then the XML file MUST be well-formed with a single parent node like that for the entire file πŸ‘‘

The callback function returns the value type that should be used for child nodes. And the constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK, MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading child nodes of the specified type β€” pick your fighter πŸ’–

🚨 Critical, no cap: the SAX callback must call mxmlRetain for any nodes that need to be kept for later use. Otherwise? Nodes get deleted when the parent node is closed, or after each data, comment, CDATA, or directive node. Poof. Gone. πŸ’€

πŸ“‚ mxmlSAXLoadFile

Same vibe, but with a FILE * instead of a file descriptor πŸ“

mxml_node_t * mxmlSAXLoadFile (
    mxml_node_t *top,
    FILE *fp,
    mxml_load_cb_t cb,
    mxml_sax_cb_t sax_cb,
    void *sax_data
);

The nodes in the specified file are added to the specified top node. No top node given? Then the XML file MUST be well-formed with a single parent node like that for the entire file πŸ‘‘

The callback function returns the value type that should be used for child nodes, and MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK, MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading child nodes of the specified type 🌸

🚨 Again, for the people in the back: the SAX callback must call mxmlRetain 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. 😭

🧡 mxmlSAXLoadString

Now with a const char *s β€” string girlies rise ✨

mxml_node_t * mxmlSAXLoadString (
    mxml_node_t *top,
    const char *s,
    mxml_load_cb_t cb,
    mxml_sax_cb_t sax_cb,
    void *sax_data
);

The nodes in the specified string are added to the specified top node. If no top node is provided, the XML string MUST be well-formed with a single parent node like that for the entire string πŸ‘‘

The callback function returns the value type to use for child nodes. Constants MXML_INTEGER_CALLBACK, MXML_OPAQUE_CALLBACK, MXML_REAL_CALLBACK, and MXML_TEXT_CALLBACK are defined for loading child nodes of the specified type πŸ’œ

🚨 You already know: the SAX callback must call mxmlRetain 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. Periodt. πŸ’…

🎁 mxmlSaveAllocString

Save an XML tree to an allocated string. It literally hands you a string β€” iconic πŸŽ€

char * mxmlSaveAllocString (
    mxml_node_t *node,
    mxml_save_cb_t cb
);

This function returns a pointer to a string containing the textual representation of the XML node tree. When you're done with it, free it with free() β€” clean up after yourself bestie 🧹

NULL is returned if the node would produce an empty string, or if the string cannot be allocated. F in the chat for a failed malloc πŸ˜”

The callback argument specifies a function that returns a whitespace string or NULL before and after each element. And if you pass MXML_NO_CALLBACK, then whitespace will only be added before MXML_TEXT nodes with leading whitespace, and before attribute names inside opening element tags. That's the whole deal πŸ¦‹

πŸ–¨οΈ mxmlSaveFd

Save an XML tree to a file descriptor πŸ”₯

int  mxmlSaveFd (
    mxml_node_t *node,
    int fd,
    mxml_save_cb_t cb
);

The callback argument specifies a function that returns a whitespace string or NULL before and after each element. If MXML_NO_CALLBACK is specified, whitespace will only be added before MXML_TEXT nodes with leading whitespace and before attribute names inside opening element tags ✨

πŸ’Ύ mxmlSaveFile

Save an XML tree to a file β€” the classic, the standard, the moment 🌟

int  mxmlSaveFile (
    mxml_node_t *node,
    FILE *fp,
    mxml_save_cb_t cb
);

The callback argument specifies a function that returns a whitespace string or NULL before and after each element. With MXML_NO_CALLBACK, whitespace will only be added before MXML_TEXT nodes with leading whitespace and before attribute names inside opening element tags 🫢

πŸ“ mxmlSaveString

Save an XML node tree to a string. Buffer girlies, this one's for you πŸ’–

int  mxmlSaveString (
    mxml_node_t *node,
    char *buffer,
    int bufsize,
    mxml_save_cb_t cb
);

Heads up on the return value: this function returns the total number of bytes that would be required for the string β€” but it only copies (bufsize - 1) characters into the specified buffer. So yes, you can call it to measure first. Clever, honestly πŸ‘‘

The callback argument specifies a function that returns a whitespace string or NULL before and after each element. If MXML_NO_CALLBACK is specified, whitespace will only be added before MXML_TEXT nodes with leading whitespace and before attribute names inside opening element tags 🌈

🧼 mxmlSetCDATA

Set the element name of a CDATA node πŸŽ€

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

And if the node (or its first child) is not a CDATA element node? Nothing happens. The node is not changed. No notes πŸ’…

πŸ› οΈ mxmlSetCustom

Set the data and destructor of a custom data node πŸ¦‹

int  mxmlSetCustom (
    mxml_node_t *node,
    void *data,
    mxml_custom_destroy_cb_t destroy
);

If the node (or its first child) is not a custom node, the node is not changed. Simple as ✨

πŸŽ›οΈ mxmlSetCustomHandlers

Set the handling functions for custom data β€” you're customising the whole experience, love that for you πŸ’œ

void mxmlSetCustomHandlers (
    mxml_custom_load_cb_t load,
    mxml_custom_save_cb_t save
);

Here's the contract: the load function accepts a node pointer and a data string, and must return 0 on success and non-zero on error 🚨

The save function accepts a node pointer and must return a malloc'd string on success and NULL on error. Don't forget the malloc, bestie 😭

🏷️ mxmlSetElement

Set the name of an element node πŸ‘‘

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

Not an element node? Then the node is not changed. Sorry to that node πŸ’€

🚨 mxmlSetErrorCallback

Set the error message callback 🫢

void mxmlSetErrorCallback (
    mxml_error_cb_t cb
);

πŸ”’ mxmlSetInteger

Set the value of an integer node πŸ’–

int  mxmlSetInteger (
    mxml_node_t *node,
    int integer
);

If the node (or its first child) is not an integer node, the node is not changed. Vibe check failed, moving on πŸ™ˆ

πŸ«₯ mxmlSetOpaque

Set the value of an opaque node πŸŽ€

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

Not an opaque node (or its first child isn't one)? Node stays exactly as it was ✨

πŸͺ„ mxmlSetOpaquef

Set the value of an opaque string node to a formatted string. Fancy πŸ’…

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

And again: if the node (or its first child) is not an opaque node, the node is not changed πŸ¦‹

🎯 mxmlSetReal

Set the value of a real number node 🌸

int  mxmlSetReal (
    mxml_node_t *node,
    double real
);

Not a real number node (node or first child)? Not changed. Periodt πŸ’œ

✍️ mxmlSetText

Set the value of a text node πŸŽ€

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

If the node (or its first child) is not a text node, the node is not changed. No cap 🫢

πŸ–‹οΈ mxmlSetTextf

Set the value of a text node to a formatted string β€” text with range πŸ”₯

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

If the node (or its first child) is not a text node, the node is not changed ✨

πŸ—‚οΈ mxmlSetUserData

Set the user data pointer for a node. Your data, your rules πŸ’–

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

πŸ“ mxmlSetWrapMargin

Set the wrap margin when saving XML data πŸ“

void mxmlSetWrapMargin (
    int column
);

And here's the key detail: wrapping is disabled when "column" is 0. Zero equals no wrapping, bestie 🌈

πŸšΆβ€β™€οΈ mxmlWalkNext

Walk to the next logical node in the tree. Stroll time πŸ¦‹

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

The descend argument controls whether the first child is considered to be the next node. And the top node argument constrains the walk to the node's children β€” keeps you from wandering off into the woods πŸ‘‘

πŸšΆβ€β™€οΈ mxmlWalkPrev

Walk to the previous logical node in the tree. Backwards stroll, same energy πŸ’…

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

The descend argument controls whether the previous node's last child is considered to be the previous node. And the top node argument constrains the walk to the node's children 🌸

✨ TYPES ✨

Okay bestie, these are the typedefs β€” the little name tags Mini-XML uses for its structs, enums, and callback signatures. Every single one is here, no skipping. 🫢

πŸŽ€ mxml_custom_destroy_cb_t β€” custom data destructor

typedef void(*)(void *) mxml_custom_destroy_cb_t;

This is the custom data destructor type. It takes a void * and returns nothing.

πŸ¦‹ mxml_custom_load_cb_t β€” custom data load callback function

typedef int(*)(mxml_node_t *, const char *) mxml_custom_load_cb_t;

This is the custom data load callback type. It takes an mxml_node_t * and a const char *, and it returns an int.

🌸 mxml_custom_save_cb_t β€” custom data save callback function

typedef char *(*)(mxml_node_t *) mxml_custom_save_cb_t;

This is the custom data save callback type. It takes an mxml_node_t * and returns a char *.

🧚 mxml_entity_cb_t β€” entity callback function

typedef int(*)(const char *) mxml_entity_cb_t;

This is the entity callback type. It takes a const char * and returns an int.

πŸ’– mxml_error_cb_t β€” error callback function

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

This is the error callback type. It takes a const char * and returns nothing.

πŸ‘‘ mxml_index_t β€” an XML node index

typedef struct _mxml_index_s mxml_index_t;

This is the XML node index type.

⭐ mxml_load_cb_t β€” load callback function

typedef mxml_type_t(*)(mxml_node_t *) mxml_load_cb_t;

This is the load callback type. It takes an mxml_node_t * and returns an mxml_type_t.

🌈 mxml_node_t β€” an XML node

typedef struct _mxml_node_s mxml_node_t;

This is the XML node type.

πŸ”₯ mxml_save_cb_t β€” save callback function

typedef const char *(*)(mxml_node_t *, int) mxml_save_cb_t;

This is the save callback type. It takes an mxml_node_t * and an int, and it returns a const char *.

πŸ’… mxml_sax_cb_t β€” SAX callback function

typedef void(*)(mxml_node_t *, mxml_sax_event_t, void *) mxml_sax_cb_t;

This is the SAX callback type. It takes an mxml_node_t *, an mxml_sax_event_t, and a void *, and it returns nothing.

πŸ“ mxml_sax_event_t β€” SAX event type

typedef enum mxml_sax_event_e mxml_sax_event_t;

This is the SAX event type.

πŸ’œ mxml_type_t β€” the XML node type

typedef enum mxml_type_e mxml_type_t;

This is the XML node type enum alias.


πŸ” SEE ALSO πŸ”

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

Copyright Β© 2003-2021 by Michael S Sweet.


Footer vibe check, bestie: this page is dated 2022-07-25, the source/version tag is Mini-XML API, and the page is mxml(3) β€” periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from