Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8226925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:56:40+00:00 2026-06-07T15:56:40+00:00

I just started using libxml to parse the xml. I just need to know

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:56:42+00:00Added an answer on June 7, 2026 at 3:56 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started using rapidxml. I 1st create an xml file to read from.
I just started using Matlab, and I absolutely despise (or not properly understand), the
I just started using explicit resource files. I performed these steps: In the root
I just started using Rails and am not sure what I'm not doing correctly.
Just started using Sencha Touch 2 and need to integrate the Leaflet plugin https://github.com/VinylFox/Ext.ux.touch.Leaflet/tree/master/src
Just started using log4net and trying to get my head around the config and
I just started using events and delegates in C# and I'm not sure if
I just started using QT. Right now I need to transfer some code I
just started using AndroidQuery for JSON asynchronous tasks and Im not familiar with JSON
I just started using Twitter Bootstrap http://twitter.github.com/bootstrap/scaffolding.html and I'm looking for the grey/blue navbar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.