我正在尝试编写一个函数,用于在 xml 文件中查找具有指定名称的节点。 问题是该函数永远找不到指定的节点。
xmlNodePtr findNodeByName(xmlNodePtr rootnode, const xmlChar * nodename)
{
xmlNodePtr node = rootnode;
if(node == NULL){
log_err("Document is empty!");
return NULL;
}
while(node != NULL){
if(!xmlStrcmp(node->name, nodename)){
return node;
}
else if(node->children != NULL){
node = node->children;
xmlNodePtr intNode = findNodeByName(node, nodename);
if(intNode != NULL){
return intNode;
}
}
node = node->next;
}
return NULL;
}
我可以在调试器中看到函数确实深入到子节点但仍然返回 NULL。
提前致谢。
最佳答案
else if(node->children != NULL) {
node = node->children;
xmlNodePtr intNode = findNodeByName(node, nodename);
if (intNode != NULL) {
return intNode;
}
}
这应该是:
else if (node->children != NULL) {
xmlNodePtr intNode = findNodeByName(node->children, nodename);
if(intNode != NULL) {
return intNode;
}
}
效果不错
关于c - 使用 libxml2 在 xml 文件中查找节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13021785/