Source Code File 07-03_newxml3
07-03_newxml3.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( "-", doc, 0 );
    xmlFreeDoc(doc);
    return(0);
}
	Output
<?xml version="1.0"?>
    <root><data>Yes, I'm data!</data></root>
Notes
* To output the data in a pretty manner, change the third argument of the xmlSaveFormatFile() function to 1 from 0.
* This code dispenses with error checking, as described in the book. In your code, always check for the NULL pointer returned from functions xmlNewDoc(), xmlNewDocNode(), and xmlNewTextChild().
Copyright © 1997-2025 by QPBC.
All rights reserved
