Question

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!)

#include <cstdlib>
#include <iostream>
#include "Page.h" 
#include "HashTable.h"
#include "TimeStamp.h"
#include "AvlTree.h"
#include "./rapidxml/rapidxml.hpp"
#include "./rapidxml/rapidxml_print.hpp"
#include <fstream>
#include <sstream>
#include <vector>


using namespace std;
using namespace rapidxml;

char s[] = "<?xml version='1.0' encoding='utf-8'?>"
    "<people>"
        "<person gender='female'>"
            "<fname>Morgan</fname>"
            "<lname>Saxer</lname>"
        "</person>"
        "<person gender='male'>"
            "<fname>Tyler</fname>"
            "<lname>Springer</lname>"
        "</person>"
    "</people>";

void traverse_xml(const std::string& input_xml)
{
    vector<char> xml_copy(input_xml.begin(), input_xml.end());
    xml_copy.push_back('\0');

    xml_document<> doc;
    doc.parse<parse_declaration_node | parse_no_data_nodes>(&xml_copy[0]);

    string encoding = doc.first_node()->first_attribute("encoding")->value();
    cout << "encoding: " << encoding << endl;

    xml_node<>* cur_node = doc.first_node("people");

    string person_type = cur_node->first_node("person")->first_attribute("gender")->value();
    cout << "Gender: " << person_type << endl;

    cur_node = cur_node->first_node("person")->first_node("fname");
    string attr2 = cur_node->value();
    cout << "fname: " << attr2 << endl;

    cur_node = cur_node->next_sibling("lname");
    attr2 = cur_node->value();
    cout << "lname: " << attr2 << endl;

    //next person node begins here, this is where I am having the problem

    cur_node = cur_node->next_sibling("person"); //this doesn't work, and I don't know what command to use instead
    attr2 = cur_node->first_attribute("gender")->value();
    cout << "Gender: " << attr2 << endl;
}

int main(int argc, char** argv) {

   traverse_xml(s);
       return 0;
}

Yet, the output Im getting is this:

encoding: utf-8 
gender: female
fname: Morgan
lname: Saxer

and that's it. What im looking for is this:

encoding: utf-8
gender: female
fname: Morgan
lname: Saxer
gender: male

as that will be the beginning of the next person node.

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?

EDIT:
After using the suggested solution my code now looks like this:

 void traverse_xml(const std::string& input_xml)
 {
    vector<char> xml_copy(input_xml.begin(), input_xml.end());
    xml_copy.push_back('\0');

    xml_document<> doc;
    doc.parse<parse_declaration_node | parse_no_data_nodes>(&xml_copy[0]);

    xml_node<>* people = doc.first_node("people");
    xml_node<>* person = people->first_node("person");

    while (person != NULL)
    {
      cout << "Gender:" << person->first_attribute("gender")->value() << endl;
      cout << "fname: " << person->first_node("fname")->value() << endl;
      cout << "lname: " << person->first_node("lname")->value() << endl;
      person = person->next_sibling("person");
    }
 }

and now correctly outputs the following:

gender:female
fname: Morgan
lname: Saxer
gender:male
fname: Tyler
lname: Springer
Was it helpful?

Solution

You can use cur_node->parent() to go 'up' a level, but personally I'd recommend something like this instead:-

 xml_node<>* people = doc.first_node("people");
 xml_node<>* person = people->first_node("person");

 while (person != NULL)
 {
  ... output the person
  person = person->next_sibling("person"); 
 } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top