Source Code File 04-02_xmlmem2

04-02_xmlmem2.c

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

int main()
{
    const char data[] = "<rootNode>\
                         <child>Baby1</child>\
                         </rootNode>";
    xmlDocPtr doc;
    xmlNodePtr root;

    doc = xmlParseMemory( data, sizeof(data) );

    root = xmlDocGetRootElement(doc);
    if( root==NULL)
    {
        fprintf(stderr,"Can't read data\n");
        xmlFreeDoc(doc);
        exit(1);
    }
    printf("Root node is '%s'\n",root->name);

    xmlFreeDoc(doc);

    return(0);
}

Output Screenshot

Root node is 'rootNode'

Notes

* The value returned from the xmlParseMemory() function should be compared against the NULL constant to ensure its success.

* The name member of the xmlNodePtr structure contains the element's name. For the root element, this string is output at Line 22.