同じ子ノード名を持つノードのすべての子に対して同じノード値を取得しています。たとえば、ここで私のコードでは、ノード名のノード データ値をすべての場合に 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