Contents Previous Next

Nodes

Every piece of information in an XML file (elements, text, numbers) is stored in memory in "nodes". 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.

Table 2-1: Mini-XML Node Value Members
ValueTypeNode member
Customvoid * node->value.custom.data
Elementchar * node->value.element.name
Integerintnode->value.integer
Opaque (string)char * node->value.opaque
Realdoublenode->value.real
Textchar *node->value.text.string

Each node also has a user_data member which allows you to associate application-specific data with each node as needed.

New nodes are created using the mxmlNewElement, mxmlNewInteger, mxmlNewOpaque, mxmlNewReal, mxmlNewText mxmlNewTextf mxmlNewXML functions. Only elements can have child nodes, and the top node must be an element, usually the <?xml version="1.0"?> node created by mxmlNewXML().

Nodes have pointers to the node above (parent), below ( child), left (prev), and right (next) of the current node. If you have an XML file like the following:

    <?xml version="1.0"?>
    <data>
        <node>val1</node>
        <node>val2</node>
        <node>val3</node>
        <group>
            <node>val4</node>
            <node>val5</node>
            <node>val6</node>
        </group>
        <node>val7</node>
        <node>val8</node>
    </data>

the node tree for the file would look like the following in memory:

    ?xml
      |
    data
      |
    node - node - node - group - node - node
      |      |      |      |       |      |
    val1   val2   val3     |     val7   val8
                           |
                         node - node - node
                           |      |      |
                         val4   val5   val6

where "-" is a pointer to the next node and "|" is a pointer to the first child node.

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

Contents Previous Next