本文介绍了XML解析器使用的libxml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用的libxml解析XML。我只需要知道根节点甲壳虫是present与否,然后获取subchild姓氏。我的code是低于

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

和XML文件

<?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.

如果有任何其他的libxml功能使用,请告诉我。
谢谢你。

If there are any other libxml functions to use, please tell me.Thanks.

推荐答案

您需要在您的while循环的微小变化。

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

这篇关于XML解析器使用的libxml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 06:13