mxml(3)
π 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 nodeMXML_SAX_COMMENTβ Comment nodeMXML_SAX_DATAβ Data nodeMXML_SAX_DIRECTIVEβ Processing directive nodeMXML_SAX_ELEMENT_CLOSEβ Element closedMXML_SAX_ELEMENT_OPENβ Element opened
π¦ mxml_type_e
The XML node type. Your little type check moment.
MXML_CUSTOMβ Custom dataMXML_ELEMENTβ XML element with attributesMXML_IGNOREβ Ignore/throw away nodeMXML_INTEGERβ Integer valueMXML_OPAQUEβ Opaque stringMXML_REALβ Real valueMXML_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 withMXML_TEXTnodes. If you want the entire string between elements in the XML file, you MUST read the XML file withMXML_OPAQUEnodes and get the resulting strings using themxmlGetOpaquefunction 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 theMXML_TEXT_CALLBACK, which returns inline text as a series of whitespace-delimited words, instead of using theMXML_OPAQUE_CALLBACKwhich 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 theMXML_TEXT_CALLBACK, which returns inline text as a series of whitespace-delimited words, instead of using theMXML_OPAQUE_CALLBACKwhich 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 theMXML_TEXT_CALLBACK, which returns inline text as a series of whitespace-delimited words, instead of using theMXML_OPAQUE_CALLBACKwhich 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 Β©οΈ
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
- alpine 3.24: mxml-doc 3.3.1-r0 Β· Apache-2.0
- arch rolling: mxml 3.3.1-2 Β· LGPL
- epel 10: mxml-devel 3.3.1-8.el10_1 Β· Apache-2.0 WITH mxml-exception
- epel 9: mxml-devel 3.3.1-2.el9 Β· ASL 2.0 with exception
- fedora 43: mxml-devel 3.3.1-9.fc43 Β· Apache-2.0 WITH mxml-exception
- fedora 43: mxml-devel 3.3.1-9.fc43 Β· Apache-2.0 WITH mxml-exception
- fedora 44: mxml-devel 3.3.1-10.fc44 Β· Apache-2.0 WITH mxml-exception
- fedora 44: mxml-devel 3.3.1-10.fc44 Β· Apache-2.0 WITH mxml-exception
- nixos 26.05: mxml 3.3.1
- nixpkgs unstable: mxml 3.3.1
- opensuse-leap 16.0: mxml-devel 3.3.1-bp160.1.13 Β· Apache-2.0
- opensuse-leap 16.0: mxml-devel 3.3.1-bp160.1.13 Β· Apache-2.0
- opensuse-leap 16.0: mxml-devel 3.3.1-bp160.1.13 Β· Apache-2.0
- opensuse-leap 16.0: mxml-devel 3.3.1-bp160.1.13 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms