Note that there are some explanatory texts on larger screens.

plurals
  1. POTraversing a simple DOM Xml document using RapidXML
    text
    copied!<p>Alright guys, so Im using rapidXML to parse a very simple XML document. All I am trying to do is parse the data from the xml document into my own custom data structure. As a starting point, I'd like to be able to output each node and the data is contains, past that I think I can figure it out. The documentation for rapidXML seems to be predominantly focused on the creation of xml docs using rapidXML, however, here I am just trying to read it. As a start here is my code (as a note before I continue down the line with this program I will of course load the xml directly from a file, dont make fun!)</p> <pre><code>#include &lt;cstdlib&gt; #include &lt;iostream&gt; #include "Page.h" #include "HashTable.h" #include "TimeStamp.h" #include "AvlTree.h" #include "./rapidxml/rapidxml.hpp" #include "./rapidxml/rapidxml_print.hpp" #include &lt;fstream&gt; #include &lt;sstream&gt; #include &lt;vector&gt; using namespace std; using namespace rapidxml; char s[] = "&lt;?xml version='1.0' encoding='utf-8'?&gt;" "&lt;people&gt;" "&lt;person gender='female'&gt;" "&lt;fname&gt;Morgan&lt;/fname&gt;" "&lt;lname&gt;Saxer&lt;/lname&gt;" "&lt;/person&gt;" "&lt;person gender='male'&gt;" "&lt;fname&gt;Tyler&lt;/fname&gt;" "&lt;lname&gt;Springer&lt;/lname&gt;" "&lt;/person&gt;" "&lt;/people&gt;"; void traverse_xml(const std::string&amp; input_xml) { vector&lt;char&gt; xml_copy(input_xml.begin(), input_xml.end()); xml_copy.push_back('\0'); xml_document&lt;&gt; doc; doc.parse&lt;parse_declaration_node | parse_no_data_nodes&gt;(&amp;xml_copy[0]); string encoding = doc.first_node()-&gt;first_attribute("encoding")-&gt;value(); cout &lt;&lt; "encoding: " &lt;&lt; encoding &lt;&lt; endl; xml_node&lt;&gt;* cur_node = doc.first_node("people"); string person_type = cur_node-&gt;first_node("person")-&gt;first_attribute("gender")-&gt;value(); cout &lt;&lt; "Gender: " &lt;&lt; person_type &lt;&lt; endl; cur_node = cur_node-&gt;first_node("person")-&gt;first_node("fname"); string attr2 = cur_node-&gt;value(); cout &lt;&lt; "fname: " &lt;&lt; attr2 &lt;&lt; endl; cur_node = cur_node-&gt;next_sibling("lname"); attr2 = cur_node-&gt;value(); cout &lt;&lt; "lname: " &lt;&lt; attr2 &lt;&lt; endl; //next person node begins here, this is where I am having the problem cur_node = cur_node-&gt;next_sibling("person"); //this doesn't work, and I don't know what command to use instead attr2 = cur_node-&gt;first_attribute("gender")-&gt;value(); cout &lt;&lt; "Gender: " &lt;&lt; attr2 &lt;&lt; endl; } int main(int argc, char** argv) { traverse_xml(s); return 0; } </code></pre> <p>Yet, the output Im getting is this:</p> <pre><code>encoding: utf-8 gender: female fname: Morgan lname: Saxer </code></pre> <p>and that's it. What im looking for is this:</p> <pre><code>encoding: utf-8 gender: female fname: Morgan lname: Saxer gender: male </code></pre> <p>as that will be the beginning of the next person node.</p> <p>Essentially I think my question can be boiled down to this. Is there some sort of "up one level" function for rapidXML? I realize next_sibling(), takes you to the next node on the same level, but once you delve into any one node, there doesn't seem to be an obvious way to come back out. Does anyone have idea how to do such a thing?</p> <p><strong>EDIT:</strong><br /> After using the suggested solution my code now looks like this:</p> <pre><code> void traverse_xml(const std::string&amp; input_xml) { vector&lt;char&gt; xml_copy(input_xml.begin(), input_xml.end()); xml_copy.push_back('\0'); xml_document&lt;&gt; doc; doc.parse&lt;parse_declaration_node | parse_no_data_nodes&gt;(&amp;xml_copy[0]); xml_node&lt;&gt;* people = doc.first_node("people"); xml_node&lt;&gt;* person = people-&gt;first_node("person"); while (person != NULL) { cout &lt;&lt; "Gender:" &lt;&lt; person-&gt;first_attribute("gender")-&gt;value() &lt;&lt; endl; cout &lt;&lt; "fname: " &lt;&lt; person-&gt;first_node("fname")-&gt;value() &lt;&lt; endl; cout &lt;&lt; "lname: " &lt;&lt; person-&gt;first_node("lname")-&gt;value() &lt;&lt; endl; person = person-&gt;next_sibling("person"); } } </code></pre> <p>and now <em>correctly</em> outputs the following:</p> <pre><code>gender:female fname: Morgan lname: Saxer gender:male fname: Tyler lname: Springer </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload