c++ - 如何使用pugixml解析获取具有相同节点名称的节点的节点数据?

标签 c++ pugixml

我为具有相同子节点名称的节点的所有子节点获取相同的节点值。例如,在我的代码中,我在所有情况下都将节点名称的节点数据值作为 ACHRA。我想要获取正确的节点值。请指导。

这是我的代码:

XML代码:

<?xml version='1.0' encoding = 'UTF-8' ?>

<student>
  <person>
    <name name="AttractMode0" >Achra</name>
    <name name="abc" >Elivia</name>
    <name name="def" >Christina</name>
    <gender name="AttractMode1" >female</gender>
    <country name="AttractMode2" >India</country>
  </person> 

  <person>
    <name name="AttractMode3" >georg</name>
    <gender name="AttractMode4" >male</gender>
    <country name="AttractMode5" >Austria</country>  
  </person>
</student>

C++代码

#include "pugixml-1.4/src/pugixml.cpp" 
#include <iostream> 
#include <sstream> 
int main()
{
    pugi::xml_document doc;
    std::string namePerson;

    if (!doc.load_file("student.xml")) return -1;

    pugi::xml_node persons = doc.child("student");
    std::cout << persons.name() << std::endl;

for (pugi::xml_node person = persons.first_child(); person; person = person.next_sibling())
    {

        for (pugi::xml_attribute attr = person.first_attribute(); attr; attr = attr.next_attribute())
        {
            std::cout << " " << attr.name() << "=" << attr.value() << std::endl;
        }

        for (pugi::xml_node child = person.first_child(); child; child = child.next_sibling())
        {
            std::cout << child.name() <<"="<< person.child_value(child.name())<<std::endl;     // get element name
            // iterate through all attributes
            for (pugi::xml_attribute attr = child.first_attribute(); attr; attr = attr.next_attribute())
            {
                std::cout << " " << attr.name() << "=" << attr.value() << std::endl;
            }
            std::cout << std::endl;
        }
    }
    std::cout << std::endl;
}

我的输出是:

student

Person: 
name=Achra
 name=AttractMode0

name=Achra
 name=abc

name=Achra
 name=def

gender=female
 name=AttractMode1

country=India
 name=AttractMode2


Person: 
name=georg
 name=AttractMode3

gender=male
 name=AttractMode4

country=Austria
 name=AttractMode5

最佳答案

引用自您的 pugixml 库文档:

Plain character data nodes (node_pcdata) represent plain text in XML. PCDATA nodes have a value, but do not have name or children/attributes. Note that plain character data is not a part of the element node but instead has its own node; for example, an element node can have several child PCDATA nodes.

只需替换这一行:

std::cout << child.name() << "=" << person.child_value(child.name()) << std::endl;

像这样:

pugi::xml_node pcdata = child.first_child();
std::cout << child.name() << " = " << pcdata.value() << std::endl;

现在的输出是:

name = Achra
 name = AttractMode0
name = Elivia
 name = abc
name = Christina
 name = def
gender = female
 name = AttractMode1
country = India
 name = AttractMode2
name = georg
 name = AttractMode3
gender = male
 name = AttractMode4
country = Austria
 name = AttractMode5

您可能还想看看这个 walker - 看起来更简单。

关于c++ - 如何使用pugixml解析获取具有相同节点名称的节点的节点数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25186553/

相关文章:

c++ - 使用pugixml使用C++解析数据但无法获取所有节点属性

c++ - Pugixml - 解析具有前缀映射和不带前缀映射的命名空间

c++ - 使用自定义内核或 CUBLAS 对 vector 张量积进行 CUDA 优化

c++ - 如何解决未处理的异常? (OpenGL, GLFW3)

c++ - 从 vector 列表调用成员函数指针

c++ - Qt 5.2.1 无法在 mac OSX 10.8.5 上构建

c++ - 从 Sprite 的位置和旋转获取位置偏移

c++ - 使用pugixml将xml namespace 添加到xml_document

c++ - pugixml为同一级别的所有子级获取相同的地址

c++ - 我所在的对象的哪个迭代