Contents Previous Next

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

The first argument specifies an existing XML parent node, if any. Normally you will pass NULL for this argument unless you are combining multiple XML sources. The XML file must contain a complete XML document including the ?xml element if the parent node is NULL.

The second argument specifies the stdio file to read from, as opened by fopen() or popen(). You can also use stdin if you are implementing an XML filter program.

The third argument specifies a callback function which returns the value type of the immediate children for a new element node: MXML_INTEGER, MXML_OPAQUE, MXML_REAL, or MXML_TEXT. Load callbacks are described in detail in Chapter 3. The example code uses the MXML_NO_CALLBACK constant which specifies that all data nodes in the document contain whitespace-separated text values.

The mxmlLoadString() function loads XML node trees from a string:

    char buffer[8192];
    mxml_node_t *tree;

    ...
    tree = mxmlLoadString(NULL, buffer, MXML_NO_CALLBACK);

The first and third arguments are the same as used for mxmlLoadFile(). The second argument specifies the string or character buffer to load and must be a complete XML document including the ?xml element if the parent node is NULL.


Contents Previous Next