日期:2014-05-16  浏览次数:20853 次

懂linux 下 XML 文件解析的大神进来帮我看看
项目要用到XML 以前没接触过,这几天写了点小程序,当我遍历节点时
<no>
  <yes>1</yes>
  <hello>2</hello>
</no>
 
当我找到no这个元素时,我想一次性的吧他含有的元素内容取出来 我该用函数。
如果直接 szKey = xmlNodeGetContent(curNode);//curnode 为no元素节点
yes hello 的内容倒是可以出来 但是老是会出现空格 不知道为什么 帮帮小弟吧弄一下午了

------解决方案--------------------
C/C++ code

#include <stdio.h>

#include <libxml/parser.h>
#include <libxml/tree.h>

void xmlNodeWalk(xmlNodePtr node)
{
        xmlChar *data;

        while (NULL != node) {
                if (NULL == node->xmlChildrenNode
                        || XML_TEXT_NODE == node->xmlChildrenNode->type) {
                        data = xmlNodeGetContent(node);
                        printf("[%s][%s]\n", node->name, data);
                        xmlFree(data);
                } else {
                         xmlNodeWalk(node->xmlChildrenNode);
                }
                node = node->next;
        }
}

int main(int argc, char *argv[])
{

        xmlDocPtr doc;
        xmlNodePtr root;

        xmlKeepBlanksDefault(0);

        doc = xmlParseFile("demo.xml");
        root = xmlDocGetRootElement(doc);
        xmlNodeWalk(root);
        xmlFreeDoc(doc);
        return 0;
}