The XML node type.
Name | Description |
---|---|
MXML_ELEMENT | XML element with attributes |
MXML_INTEGER | Integer value |
MXML_OPAQUE | Opaque string |
MXML_REAL | Real value |
MXML_TEXT | Text fragment |
Add a node to a tree. 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.
void mxmlAdd( mxml_node_t * parent, int where, mxml_node_t * child, mxml_node_t * node);
Name | Description |
---|---|
parent | Parent node |
where | Where to add, MXML_ADD_BEFORE or MXML_ADD_AFTER |
child | Child node for where or MXML_ADD_TO_PARENT |
node | Node to add |
Nothing.
Delete a 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.
void mxmlDelete( mxml_node_t * node);
Name | Description |
---|---|
node | Node to delete |
Nothing.
Get an attribute. This function returns NULL if the node is not an element or the named attribute does not exist.
const char * mxmlElementGetAttr( mxml_node_t * node, const char * name);
Name | Description |
---|---|
node | Element node |
name | Name of attribute |
Attribute value or NULL
Set an attribute. 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.
void mxmlElementSetAttr( mxml_node_t * node, const char * name, const char * value);
Name | Description |
---|---|
node | Element node |
name | Name of attribute |
value | Attribute value |
Nothing.
Find the named element. 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.
mxml_node_t * mxmlFindElement( mxml_node_t * node, mxml_node_t * top, const char * name, const char * attr, const char * value, int descend);
Name | Description |
---|---|
node | Current node |
top | Top node |
name | Element name or NULL for any |
attr | Attribute name, or NULL for none |
value | Attribute value, or NULL for any |
descend | Descend into tree - MXML_DESCEND, MXML_NO_DESCEND, or MXML_DESCEND_FIRST |
Element node or NULL
Load a file into an XML node tree. 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 like <?xml> for the entire file. The callback function returns the value type that should be used for child nodes. If MXML_NO_CALLBACK is specified then all child nodes will be either MXML_ELEMENT or MXML_TEXT nodes. 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.
mxml_node_t * mxmlLoadFile( mxml_node_t * top, FILE * fp, mxml_type_t (*cb)(mxml_node_t *node));
Name | Description |
---|---|
top | Top node |
fp | File to read from |
(*cb)(mxml_node_t *node) | Callback function or MXML_NO_CALLBACK |
First node or NULL if the file could not be read.
Load a string into an XML node tree. 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 <?xml> for the entire string. The callback function returns the value type that should be used for child nodes. If MXML_NO_CALLBACK is specified then all child nodes will be either MXML_ELEMENT or MXML_TEXT nodes. 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.
mxml_node_t * mxmlLoadString( mxml_node_t * top, const char * s, mxml_type_t (*cb)(mxml_node_t *node));
Name | Description |
---|---|
top | Top node |
s | String to load |
(*cb)(mxml_node_t *node) | Callback function or MXML_NO_CALLBACK |
First node or NULL if the string has errors.
Create a new element node. 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.
mxml_node_t * mxmlNewElement( mxml_node_t * parent, const char * name);
Name | Description |
---|---|
parent | Parent node or MXML_NO_PARENT |
name | Name of element |
New node
Create a new integer node. 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.
mxml_node_t * mxmlNewInteger( mxml_node_t * parent, int integer);
Name | Description |
---|---|
parent | Parent node or MXML_NO_PARENT |
integer | Integer value |
New node
Create a new opaque string. The new opaque 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 node has no parent. The opaque string must be nul-terminated and is copied into the new node.
mxml_node_t * mxmlNewOpaque( mxml_node_t * parent, const char * opaque);
Name | Description |
---|---|
parent | Parent node or MXML_NO_PARENT |
opaque | Opaque string |
New node
Create a new real number node. 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.
mxml_node_t * mxmlNewReal( mxml_node_t * parent, double real);
Name | Description |
---|---|
parent | Parent node or MXML_NO_PARENT |
real | Real number value |
New node
Create a new text fragment node. 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.
mxml_node_t * mxmlNewText( mxml_node_t * parent, int whitespace, const char * string);
Name | Description |
---|---|
parent | Parent node or MXML_NO_PARENT |
whitespace | 1 = leading whitespace, 0 = no whitespace |
string | String |
New node
Create a new formatted text fragment node. 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 format string must be nul-terminated and is formatted into the new node.
mxml_node_t * mxmlNewTextf( mxml_node_t * parent, int whitespace, const char * format, ...);
Name | Description |
---|---|
parent | Parent node or MXML_NO_PARENT |
whitespace | 1 = leading whitespace, 0 = no whitespace |
format | Printf-style frmat string |
... | Additional args as needed |
New node
Remove a node from its parent. Does not free memory used by the node - use mxmlDelete() for that. This function does nothing if the node has no parent.
void mxmlRemove( mxml_node_t * node);
Name | Description |
---|---|
node | Node to remove |
Nothing.
Save an XML node tree to an allocated string. This function returns a pointer to a string containing the textual representation of the XML node tree. The string should be freed using the free() function when you are done with it. NULL is returned if the node would produce an empty string or if the string cannot be allocated.
char * mxmlSaveAllocString( mxml_node_t * node, int (*cb)(mxml_node_t *node, int ws));
Name | Description |
---|---|
node | Node to write |
(*cb)(mxml_node_t *node, int ws) | Whitespace callback or MXML_NO_CALLBACK |
Allocated string or NULL
Save an XML tree to a file. The callback argument specifies a function that returns a whitespace character or nul (0) 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.
int mxmlSaveFile( mxml_node_t * node, FILE * fp, int (*cb)(mxml_node_t *node, int ws));
Name | Description |
---|---|
node | Node to write |
fp | File to write to |
(*cb)(mxml_node_t *node, int ws) | Whitespace callback or MXML_NO_CALLBACK |
0 on success, -1 on error.
Save an XML node tree to a string. This function returns the total number of bytes that would be required for the string but only copies (bufsize - 1) characters into the specified buffer.
int mxmlSaveString( mxml_node_t * node, char * buffer, int bufsize, int (*cb)(mxml_node_t *node, int ws));
Name | Description |
---|---|
node | Node to write |
buffer | String buffer |
bufsize | Size of string buffer |
(*cb)(mxml_node_t *node, int ws) | Whitespace callback or MXML_NO_CALLBACK |
Size of string
Set the name of an element node. The node is not changed if it is not an element node.
int mxmlSetElement( mxml_node_t * node, const char * name);
Name | Description |
---|---|
node | Node to set |
name | New name string |
0 on success, -1 on failure
Set the error message callback.
void mxmlSetErrorCallback( void (*cb)(const char *));
Name | Description |
---|---|
(*cb)(const char *) | Error callback function |
Nothing.
Set the value of an integer node. The node is not changed if it is not an integer node.
int mxmlSetInteger( mxml_node_t * node, int integer);
Name | Description |
---|---|
node | Node to set |
integer | Integer value |
0 on success, -1 on failure
Set the value of an opaque node. The node is not changed if it is not an opaque node.
int mxmlSetOpaque( mxml_node_t * node, const char * opaque);
Name | Description |
---|---|
node | Node to set |
opaque | Opaque string |
0 on success, -1 on failure
Set the value of a real number node. The node is not changed if it is not a real number node.
int mxmlSetReal( mxml_node_t * node, double real);
Name | Description |
---|---|
node | Node to set |
real | Real number value |
0 on success, -1 on failure
Set the value of a text node. The node is not changed if it is not a text node.
int mxmlSetText( mxml_node_t * node, int whitespace, const char * string);
Name | Description |
---|---|
node | Node to set |
whitespace | 1 = leading whitespace, 0 = no whitespace |
string | String |
0 on success, -1 on failure
Set the value of a text node to a formatted string. The node is not changed if it is not a text node.
int mxmlSetTextf( mxml_node_t * node, int whitespace, const char * format, ...);
Name | Description |
---|---|
node | Node to set |
whitespace | 1 = leading whitespace, 0 = no whitespace |
format | Printf-style format string |
... | Additional arguments as needed |
0 on success, -1 on failure
Walk 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 node argument constrains the walk to the node's children.
mxml_node_t * mxmlWalkNext( mxml_node_t * node, mxml_node_t * top, int descend);
Name | Description |
---|---|
node | Current node |
top | Top node |
descend | Descend into tree - MXML_DESCEND, MXML_NO_DESCEND, or MXML_DESCEND_FIRST |
Next node or NULL
Walk to the previous logical node in the tree. The descend argument controls whether the previous node's last child is considered to be the previous node. The top node argument constrains the walk to the node's children.
mxml_node_t * mxmlWalkPrev( mxml_node_t * node, mxml_node_t * top, int descend);
Name | Description |
---|---|
node | Current node |
top | Top node |
descend | Descend into tree - MXML_DESCEND, MXML_NO_DESCEND, or MXML_DESCEND_FIRST |
Previous node or NULL
An XML element attribute value.
struct mxml_attr_s { char * name; char * value; };
Name | Description |
---|---|
name | Attribute name |
value | Attribute value |
An XML node.
struct mxml_node_s { struct mxml_node_s * child; struct mxml_node_s * last_child; struct mxml_node_s * next; struct mxml_node_s * parent; struct mxml_node_s * prev; mxml_type_t type; mxml_value_t value; };
Name | Description |
---|---|
child | First child node |
last_child | Last child node |
next | Next node under same parent |
parent | Parent node |
prev | Previous node under same parent |
type | Node type |
value | Node value |
An XML text value.
struct mxml_text_s { char * string; int whitespace; };
Name | Description |
---|---|
string | Fragment string |
whitespace | Leading whitespace? |
An XML element value.
struct mxml_value_s { mxml_attr_t * attrs; char * name; int num_attrs; };
Name | Description |
---|---|
attrs | Attributes |
name | Name of element |
num_attrs | Number of attributes |
An XML element attribute value.
typedef struct mxml_attr_s mxml_attr_t;
An XML element value.
typedef struct mxml_value_s mxml_element_t;
An XML node.
typedef struct mxml_node_s mxml_node_t;
An XML text value.
typedef struct mxml_text_s mxml_text_t;
The XML node type.
typedef enum mxml_type_e mxml_type_t;
An XML node value.
typedef union mxml_value_u mxml_value_t;
An XML node value.
union mxml_value_u { mxml_element_t element; int integer; char * opaque; double real; mxml_text_t text; };
Name | Description |
---|---|
element | Element |
integer | Integer number |
opaque | Opaque string |
real | Real number |
text | Text fragment |