0

I am confused as to why I can find the node by name through next_sibling. But when I try to use first_node I am getting a segfault.

The parent node containing all the nodes I need to find.

rapidxml::xml_node<> *xmlnode_chd = xml_doc->first_node(); 

The two lines to access the node

xmlnode_chd->first_node()->next_sibling("name_to_find")->name(); //Works
xmlnode_chd->first_node("name_to_find")->name();                 //Fails

To my understanding a sibling node has the same parent. I thought that the logic between these two statements was the same. I am probably overlooking something simple.

French
  • 1
  • 1
  • It fails because you don't check return value of `first_node` if it's null. – kiner_shah Feb 13 '23 at 05:41
  • Wouldnt find_sibling return null as well though? If the name didnt exist? Maybe I am not understanding how first_node works, I thought by passing it a name parameter it would find the first node that matches the name. – French Feb 13 '23 at 05:45
  • As per docs, it returns 0 if first_node isn't found. https://rapidxml.sourceforge.net/manual.html#classrapidxml_1_1xml__node_7823e36687669e59c2afdf66334ef35a_17823e36687669e59c2afdf66334ef35a – kiner_shah Feb 13 '23 at 05:47
  • 1
    Ok thanks for response. I guess that my node is not a child of the root node, but exists as a sibling to a node that is a child of the root node. I just need to do some reading into the docs. I figured that all the siblings were children of the root node. – French Feb 13 '23 at 06:00
  • Always check for return values of functions, will save you lots of time in case some error occurs. – kiner_shah Feb 13 '23 at 06:02
  • Yes I guessed I kinda phrased it wrong, I understood that it was returning 0. I just didn't understand why because I thought that the logic between those lines was the same. – French Feb 13 '23 at 06:16
  • You phrased it right, I was just giving you an advice to avoid chaining function calls like `A->b()->c()` and be cautious. – kiner_shah Feb 13 '23 at 06:18

0 Answers0