Source Code File 07-04_newxml4

07-04_newxml4.c

#include <stdio.h>
#include <stdlib.h>
#include <libxml/parser.h>

int main()
{
    const xmlChar *r = (const xmlChar *)"root";
    const xmlChar *element = (const xmlChar *)"data";
    const xmlChar *value = (const xmlChar *)"Yes, I'm data!";
    xmlDocPtr doc;
    xmlNodePtr root,node;

    doc = xmlNewDoc( (const xmlChar *)"1.0");
    root = xmlNewDocNode( doc, NULL, r, NULL);
    xmlDocSetRootElement( doc, root );
    node = xmlNewTextChild( root, NULL, element, value);

    xmlSaveFormatFile( "new.xml", doc, 1 );

    xmlFreeDoc(doc);

    return(0);
}    

Output

None.

Notes

* The code has no output, though the file new.xml is created. Here are its contents:

<?xml version="1.0"?>
<root>
  <data>Yes, I'm data!</data>
</root>

* As with the previous example, no error checking is done in this document. Ensure that in your code you test the Libxml2 creation functions to determine whether they return NULL or a valid value.