XML Parser using libxml

iarekenta

New Member
I just started using libxml to parse the xml. I just need to know root node "beatles" is present or not, then get the subchild "lastname". My code is below\[code\]#include <stdio.h>#include <string.h>#include <stdlib.h>#include <libxml/xmlmemory.h>#include <libxml/parser.h>void parseStory ( xmlDocPtr doc, xmlNodePtr cur ){ xmlChar *key; cur = cur -> xmlChildrenNode; printf ( "Here\n" ); while ( cur != NULL ) { if ( ( !xmlStrcmp ( cur -> name, ( const xmlChar * ) "lastname" ) ) ) { key = xmlNodeListGetString ( doc, cur -> xmlChildrenNode,1); printf ( "keyword: %s\n", key ); xmlFree ( key ); } cur = cur -> next; } return ;}static void parseDoc ( char *docname ){ xmlDocPtr doc; xmlNodePtr cur; doc = xmlParseFile ( docname ); if ( doc == NULL ) { fprintf ( stderr, "Document not parsed successfully. \n" ); return; } printf ( "Parsing Successful\n" ); cur = xmlDocGetRootElement ( doc ); if ( cur == NULL ) { fprintf ( stderr, "empty document \n" ); xmlFreeDoc ( doc ); return; } printf ( "Got the root Node\n" ); if ( xmlStrcmp ( cur->name, ( const xmlChar * ) "beatles" ) ) { fprintf ( stderr, "Document of the wrong type root node != "); xmlFreeDoc(doc); return; } printf ( "Got the root \n" ); cur = cur -> xmlChildrenNode; while ( cur != NULL ) { if ( !(xmlStrcmp ( cur->name, ( const xmlChar * ) "name" ) ) ) { parseStory ( doc, cur ); } cur = cur -> next; } xmlFreeDoc ( doc ); return;}int main ( int argc, char **argv ){ char *docname; if ( argc <= 1 ) { printf ( "Usage: %s docname\n", argv[0] ); return ( 0 ); } docname = argv [1]; parseDoc ( docname ); return ( 1 );}\[/code\]And the xml file is\[code\]<?xml version="1.0"?><beatles> <beatle link="http://www.paulmccartney.com"> <name> <firstname>Paul</firstname> <lastname>McCartney</lastname> </name> </beatle>\[/code\]I am able to get root node "beatles", but i am unable to find the "name" and "lastname".Please help me with this. If there are any other libxml functions to use, please tell me.Thanks.
 
Back
Top