项目要用到XML 以前没接触过,这几天写了点小程序,当我遍历节点时
<no>
<yes>1</yes>
<hello>2</hello>
</no>
当我找到no这个元素时,我想一次性的吧他含有的元素内容取出来 我该用函数。
如果直接 szKey = xmlNodeGetContent(curNode);//curnode 为no元素节点
yes hello 的内容倒是可以出来 但是老是会出现空格 不知道为什么
#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;
}XML code
<?xml version="1.0"?>
<no>
<yes>1</yes>
<hello>2</hello>
</no>