Source Code File 07-05_newxml5

07-05_newxml5.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);

    xmlSaveFormatFileEnc( "new.xml", doc, "UTF-8", 1 );

    xmlFreeDoc(doc);

    return(0);
}

Output

None.

Notes

* This code generates no output. The contents of the created file, new.xml, are:

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

* As with the previous examples, this code omits error checking. Ensure that you test the Libxml2 creation functions for the NULL value.