c++xmlpugixml

can't get attribute's name and value while parsing xml


I have got a non empty pugi::xml_node my_node;.

If I print it with my_node.print(std::cout); I can clearly see that the contents of this node are okay. Let's say it's:

<my_class id="0" name="my class" type="my type">
   <child_1> 45.0 </child_1>
   <child_2> 0.01 </child_2>
   <child_3> 100.0 </child_3>
   <child_4> some_string </child_4>
</my_class>

But when I try to iterate it's attributes, I get weird output (which looks like some kind of addresses?).

for (auto attr : my_node.attributes()){
    std::cout << attr.name() << " : " << attr.value() << std::endl;
}

And here's the output:

0x55727e049cd4 : 0x55727e049ce4
0x55727e049d1c : 0x55727e049d34
0x55727e049d8c : 0x55727e049da4

I took this example from the documentation by the way and I've got no idea why this is happening.

What am I doing wrong here?


Solution

  • name() and value() methods return const char_t*.

    char_t is Pugi-specific typedef, controlled by PUGIXML_WCHAR_MODE macro.

    What I think happened, you have defined PUGIXML_WCHAR_MODE, char_t becomes wchar_t, however std::cout is not Unicode. If that’s the case, try using Unicode output instead, like this:

    std::wcout << attr.name() << L" : " << attr.value() << std::endl;