Pugixml C++解析XML

Pugixml C++ parsing XML

我是 pugixml 的新手。假设我有 XML 给出 here。我想获得每个学生的 NameRoll 的值。下面的代码只找到标签而不是值。

#include <iostream>
#include "pugixml.hpp"

int main()
{
    std::string xml_mesg = "<data> \
    <student>\
       <Name>student 1</Name>\
       <Roll>111</Roll>\
    </student>\
    <student>\
        <Name>student 2</Name>\
        <Roll>222</Roll>\
    </student>\
    <student>\
        <Name>student 3</Name>\
        <Roll>333</Roll>\
    </student>\
</data>";
    pugi::xml_document doc;
    doc.load_string(xml_mesg.c_str());
    pugi::xml_node data = doc.child("data");
    for(pugi::xml_node_iterator it=data.begin(); it!=data.end(); ++it)
    {
        for(pugi::xml_node_iterator itt=it->begin(); itt!=it->end(); ++itt)
            std::cout << itt->name() << " " << std::endl;
    }
    return 0;
}

我想要每个学生的 Name 和 Roll 的输出。我怎样才能修改上面的代码?另外,如果可以参考here(press Test),我可以直接写pugixml支持的xpath。如果是这样,我如何在 Pugixml 中使用 Xpath 获取我寻求的值。

以下是仅使用 Xpath 即可实现的方法:

pugi::xpath_query student_query("/data/student");

pugi::xpath_query name_query("Name/text()");
pugi::xpath_query roll_query("Roll/text()");

pugi::xpath_node_set xpath_students = doc.select_nodes(student_query);
for (pugi::xpath_node xpath_student : xpath_students)
{
    // Since Xpath results can be nodes or attributes, you must explicitly get
    // the node out with .node()
    pugi::xml_node student = xpath_student.node();

    pugi::xml_node name = student.select_node(name_query).node();
    pugi::xml_node roll = student.select_node(roll_query).node();

    std::cout << "Student name: " << name.value() << std::endl;
    std::cout << "        roll: " << roll.value() << std::endl;
}

我认为您得到 "tags/nodes" 而不是它们的值的原因是因为您使用的是 name() 函数而不是 value()。尝试用 itt->value() 代替您的 itt->name() 。 我找到了一些关于访问文档数据的好文档 here

谢谢@Cornstalks for the insight of using xpath in pugixml. I used child_value given here。因此,我的代码是:

    for(pugi::xml_node_iterator it=data.begin(); it!=data.end(); ++it)
    {
        for(pugi::xml_node_iterator itt=it->begin(); itt!=it->end(); ++itt)
            std::cout << itt->name() << " " << itt->child_value() << " " << std::endl;
    }

我也可以按照 @Cornstalks 的建议使用 xpath,从而使我的代码为:

pugi::xml_document doc;
doc.load_string(xml_mesg.c_str());
pugi::xpath_query student_query("/data/student");

pugi::xpath_query name_query("Name/text()");
pugi::xpath_query roll_query("Roll/text()");

pugi::xpath_node_set xpath_students = doc.select_nodes(student_query);
for (pugi::xpath_node xpath_student : xpath_students)
{
    // Since Xpath results can be nodes or attributes, you must explicitly get
    // the node out with .node()
    pugi::xml_node student = xpath_student.node();

    pugi::xml_node name = student.select_node(name_query).node();
    pugi::xml_node roll = student.select_node(roll_query).node();

    std::cout << "Student name: " << name.value() << std::endl;
    std::cout << "        roll: " << roll.value() << std::endl;
}

在您的内部循环中,更改以下行以获取如下值: student1 和 111 等等...

std::cout << itt.text().get() << " " << std::endl;