libxml如何获取子atribute
libxml how to get child atribute
如何获取child atribute
的value
?例如xml:
<root>
<child>
<info>Lala</info>
</child>
</root>
如何获得Lala
?我试过这个(但不起作用:())
void parsePackage (xmlDocPtr doc, xmlNodePtr cur) {
xmlChar *key;
xmlChar *uri;
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"child"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
uri = xmlGetProp(cur, (const xmlChar *)"info");
printf("Info: %sn", uri);
xmlFree(key);
}
cur = cur->next;
}
return;
}
void parseDoc() {
xmlDocPtr doc;
xmlNodePtr cur;
doc = xmlParseFile("test.xml");
if (doc == NULL ) {
fprintf(stderr,"Document not parsed successfully. n");
return;
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,"empty documentn");
xmlFreeDoc(doc);
return;
}
if (xmlStrcmp(cur->name, (const xmlChar *) "root")) {
fprintf(stderr,"document of the wrong type, root node != package");
xmlFreeDoc(doc);
return;
}
parsePackage(doc, cur);
xmlFreeDoc(doc);
return;
}
当我开始这个代码时,我得到的不是LaLa
,而是null
。
您使用的是xmlGetProp(),它检索当前节点的属性值,但"info"是"child"节点的子节点,而不是属性。您应该在"info"节点上使用xmlNodeGetContent()。
它看起来像印刷错误。你应该使用(const xmlChar *)"child")
而不是(const xmlChar *)"cild")
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- Boost Spirit,获取迭代器内部语义动作
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 具有默认值的引用获取函数
- xmake总是报告:错误:无法获取cxx的程序,为什么
- libxml如何获取子atribute