3

好的伙计们,所以我使用 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

因为那将是下一个人节点的开始。

本质上,我认为我的问题可以归结为这一点。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
4

1 回答 1

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"); 
 } 
于 2012-11-18T20:29:12.720 回答