Contents Previous Next

Saving XML

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 first argument is the XML node tree to save. It should normally be a pointer to the top-level ?xml node in your XML document.

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

The third argument is the whitespace callback to use when saving the file. Whitespace callbacks are covered in detail in Chapter 3. The example code above uses the MXML_NO_CALLBACK constant to specify that no special whitespace handling is required.

The mxmlSaveAllocString(), and mxmlSaveString() functions save XML node trees to strings:

    char buffer[8192];
    char *ptr;
    mxml_node_t *tree;

    ...
    mxmlSaveString(tree, buffer, sizeof(buffer), MXML_NO_CALLBACK);

    ...
    ptr = mxmlSaveAllocString(tree, MXML_NO_CALLBACK);

The first and last arguments are the same as used for mxmlSaveFile(). The mxmlSaveString() function takes pointer and size arguments for saving the XML document to a fixed-size buffer, while mxmlSaveAllocString() returns a string buffer that was allocated using malloc().


Contents Previous Next