You can create and update XML documents in memory using the various mxmlNew functions. The following code will create the XML document described in the previous section:
mxml_node_t *xml; /* <?xml ... ?> */ mxml_node_t *data; /* <data> */ mxml_node_t *node; /* <node> */ mxml_node_t *group; /* <group> */ xml = mxmlNewXML("1.0"); data = mxmlNewElement(xml, "data"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val1"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val2"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val3"); group = mxmlNewElement(data, "group"); node = mxmlNewElement(group, "node"); mxmlNewText(node, 0, "val4"); node = mxmlNewElement(group, "node"); mxmlNewText(node, 0, "val5"); node = mxmlNewElement(group, "node"); mxmlNewText(node, 0, "val6"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val7"); node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val8");
We start by creating the <?xml version="1.0"?> node common to all XML files using the mxmlNewXML function:
xml = mxmlNewXML("1.0");
We then create the <data> node used for this document using the mxmlNewElement function. The first argument specifies the parent node (xml ) while the second specifies the element name (data):
data = mxmlNewElement(xml, "data");
Each <node>...</node> in the file is created using the mxmlNewElement and mxmlNewText functions. The first argument of mxmlNewText specifies the parent node (node). The second argument specifies whether whitespace appears before the text - 0 or false in this case. The last argument specifies the actual text to add:
node = mxmlNewElement(data, "node"); mxmlNewText(node, 0, "val1");
The resulting in-memory XML document can then be saved or processed just like one loaded from disk or a string.