Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a node in xml file with libxml2

I am trying to write a function that will find a node with a specified name in a xml file. The problem is that the function never finds the specified node.

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;
}

I can see in the debugger that function does go deep into the sub nodes but still returns NULL.

Thanks in advance.

like image 524
SneakyMummin Avatar asked Sep 06 '25 08:09

SneakyMummin


1 Answers

else if(node->children != NULL) {
    node = node->children; 
    xmlNodePtr intNode =  findNodeByName(node, nodename); 
    if (intNode != NULL) {
        return intNode;
    }
}

This should be:

else if (node->children != NULL) {
    xmlNodePtr intNode =  findNodeByName(node->children, nodename); 
    if(intNode != NULL) {
        return intNode;
    }
}

and it works fine

like image 181
AmiyaG Avatar answered Sep 08 '25 06:09

AmiyaG