Source Code File 06-01_nodevalue1
06-01_nodevalue1.c
#include <stdio.h> #include <stdlib.h> #include <libxml/parser.h> int main() { const char filename[] = "sample.xml"; xmlDocPtr doc; xmlNodePtr root,node; xmlChar *value; doc = xmlParseFile(filename); if( doc==NULL ) { fprintf(stderr,"Unable to open %s\n",filename); exit(1); } root = xmlDocGetRootElement(doc); node = root->children; while( node!=NULL ) { if( !xmlStrcmp(node->name,(const xmlChar *)"lastName")) { value = xmlNodeListGetString(doc,node->children,1); printf("Value of <%s> is '%s'\n",node->name,value); xmlFree(value); break; } node = node->next; } xmlFreeDoc(doc); return(0); }
Output
Value of <lastName> is 'Sinister'
Notes
* As with all XML data, this code is aware of the structure of the sample.xml file. It knows that element <lastName> is a top-level element (below the root) and that its value is a string.
Copyright © 1997-2024 by QPBC.
All rights reserved