1

我是 pugixml 的新手。考虑我在这里给出了 XML 。我想获得Name每个Roll学生的价值。下面的代码只找到标签而不是值。

#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;
}

我想要每个学生的姓名和名册的输出。如何修改上面的代码?另外,如果可以参考这里(按测试),我可以直接编写 pugixml 支持的 xpath。如果是这样,我怎样才能获得我在 Pugixml 中使用 Xpath 寻求的值。

4

4 回答 4

1

以下是仅使用 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;
}
于 2015-01-08T16:05:10.397 回答
1

我认为您获得“标签/节点”而不是它们的值的原因是因为您使用的是 name() 函数而不是 value()。尝试用 itt->value() 替换您的 itt->name() 。我在这里找到了一些关于访问文档数据的好文档

于 2015-01-08T17:07:19.820 回答
1

感谢@Cornstalks对在 pugixml 中使用 xpath 的见解。我用这里child_value给出的。我的代码是这样的:

    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;
}
于 2015-01-09T05:00:55.867 回答
0

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

std::cout << itt.text().get() << " " << std::endl;
于 2016-09-26T08:24:21.643 回答