title: C++ libxml xmlNode->children
body: <p>Ok I have been working on a class to iterate through all nodes in an html doc and return the data that I need. This is very simple and I have achieved this in Bash but now I am trying to port the same to C++. </p>
  
  <p>I started with the example on the libxml site but I have stepped through this function node by node and I can't understand how it is working.</p>
  
  <p>Here is the function:</p>
  
  <pre><code>    static void
  print_element_names(xmlNode * a_node)
  {
      xmlNode *cur_node = NULL;
  
      for (cur_node = a_node; cur_node; cur_node = cur_node-&gt;next) {
          if (cur_node-&gt;type == XML_ELEMENT_NODE) {
              printf("node type: Element, name: %s\n", cur_node-&gt;name);
          }
  
          print_element_names(cur_node-&gt;children);
      }
  }
  </code></pre>
  
  <p>So basically, this function takes a node assigns it to a pointer, and starts to loop through all sibling nodes, but if the current node has children, it calls the function and start over on that child node. This is all very understandable. </p>
  
  <p>So it drives down the doc structure, but how does it navigate back up the structure? </p>
  
  <p>Does xmlNode->children return the next parent node when it is found NULL? As far as I can tell, this is not true, but I just can't figure out how this is working.</p>
  
  <p>I successfully created a class to do what I want, but it is so much more complex than this and about 10 lines longer. I actually had to check if the next node was null and either navigate down if it has children or navigate back up and to the next node if it didn't. </p>
  
  <p>This example is much simpler and I would like to understand how to make my code better.</p>