Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:16:40+00:00 2026-06-14T15:16:40+00:00

Alright guys, so Im using rapidXML to parse a very simple XML document. All

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:16:41+00:00Added an answer on June 14, 2026 at 3:16 pm

    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"); 
     } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright guys, this should be a very simple request. I'd like to run a
Alright, here I am again trying to write code from scratch and I can't
Alright, an easy one for you guys. We are using ActiveReport's RichTextBox to display
Alright guys i'm trying to make an Asteroids type game and I need to
Alright, so I'm trying to store data in Sqlite. So I'm trying to store
Alright so using Python with windows I am trying to run a batch command
Alright guys, I'm sure there is a simple solution to this problem. I have
Alright, I'm trying to use the basic document.getElementsByTagName function, but each time I do,
Alright guys, I really hurt my brain over this one and I'm curious if
Alright I don't see why this isnt working. It seems pretty simple. Here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.