1

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

#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 );
}

And the xml file is

<?xml version="1.0"?>
<beatles>
 <beatle link="http://www.paulmccartney.com">
  <name>
   <firstname>Paul</firstname>
   <lastname>McCartney</lastname>
  </name>
 </beatle>

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.

1 Answer 1

2

You need a small change in your while loop.

printf ( "Got the root \n" );
cur = cur -> xmlChildrenNode;
while ( cur != NULL )
{
    if (cur->type == XML_ELEMENT_NODE) {

        if ( !(xmlStrcmp ( cur->name, ( const xmlChar * ) "name" ) ) )
        {
                parseStory ( doc, cur );
        }
        cur = cur -> xmlChildrenNode;
        continue;
    }
    cur = cur -> next;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.