您好我需要知道"如何使用Libxml2"在C ++中读取xml文件的一部分。在我的xml文件中,我有:
<svg>
<g>
<path d="11"/>
</g>
</svg>
我希望看到&#34; d&#34;在我的c ++程序中,当我到达这一点时:
xmlNode *cur_node = NULL;
for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if(xmlStrEqual(xmlCharStrdup("path"),cur_node->name)){
printf("element: %s\n", cur_node->name);
}
print_element_names(cur_node->children);
}
}
我不知道我需要做什么,请帮助我。
答案 0 :(得分:0)
我不确定我是否理解这个问题,但听起来你想在元素“path”中打印属性“d”。在上面的代码中,您需要这样的内容:
xmlChar *d = xmlGetProp(cur_node, "d");
... do something ...
xmlFree(d);
答案 1 :(得分:0)
类似的东西?
static void
print_element_names(xmlNode * a_node)
{
xmlNode *cur_node = NULL;
xmlChar *d;
for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if(xmlStrEqual(xmlCharStrdup("path"),cur_node->name)){
printf("element: %s\n", cur_node->name);
}
print_element_names(cur_node->children);
if(xmlGetProp(cur_node, "d")){
printf("wspolrzedne: %s\n", d);
}
xmlFree(d);
}
答案 2 :(得分:0)
下面的函数将读取包含节点和值的完整xml,
int readxml(xmlDocPtr pFilePointer,xmlNodePtr pNodePointer) {
while (pNodePointer != NULL) {
if ((xmlStrcmp(pNodePointer->name, (const xmlChar *)"text"))) {
printf("%s\n", pNodePointer->name);
} else {
}
if (NULL != pNodePointer->xmlChildrenNode) {
pNodePointer = pNodePointer->xmlChildrenNode;
if ((!xmlStrcmp(pNodePointer->name, (const xmlChar *)"text"))) {
string node;
xmlNodeListGetStringWrapper(pFilePointer, pNodePointer, node);
printf("%s\n", node.c_str());
continue;
}
} else if (pNodePointer->next != NULL) {
pNodePointer = pNodePointer->next;
} else {
pNodePointer = pNodePointer->parent;
pNodePointer = pNodePointer->next;
}
}
return 0;
}