好吧,伙计们,所以我使用rapidXML来解析一个非常简单的XML文档。我所要做的就是将xml文档中的数据解析为我自己的自定义数据结构。作为一个起点,我希望能够输出每个节点并且数据是包含的,过去我认为我可以解决它。 rapidXML的文档似乎主要侧重于使用rapidXML创建xml文档,但是,在这里我只是想读它。首先,这是我的代码(作为一个注释,在我继续使用这个程序之前,我当然会直接从文件中加载xml,不要取笑!)
#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;
}
然而,我得到的输出是这样的:
encoding: utf-8
gender: female
fname: Morgan
lname: Saxer
就是这样。我正在寻找的是:
encoding: utf-8
gender: female
fname: Morgan
lname: Saxer
gender: male
因为那将是下一个人节点的开始。
基本上我认为我的问题可归结为此。是否有某种程度的上升水平&#34; rapidXML的功能?我意识到next_sibling(),将你带到同一级别的下一个节点,但是一旦你钻研到任何一个节点,似乎并不是一个明显的回归方式。有谁知道如何做这样的事情?
修改
使用建议的解决方案后,我的代码现在看起来像这样:
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");
}
}
现在正确输出以下内容:
gender:female
fname: Morgan
lname: Saxer
gender:male
fname: Tyler
lname: Springer
答案 0 :(得分:1)
你可以使用cur_node->parent()
来提升一个级别,但我个人建议这样做: -
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");
}