Search code examples
c++xml-parsingpugixml

How to extract child data with pugixml?


I am experimenting with pugixml to extract data from a large XML file. I only am interested in the values of the values in the nodes BAR and Nm:

    <Document xmlns="xxxxxx" xmlns:xsi="vvvvvvv">
      <Outer>
        <HDR>
          <MsgId>FOOBAR222222</MsgId>
          <ID>
            <AAAAA>FOOBAR222222</AAAAA>
          </ID>
        </HDR>
        <ENTRY>
          <Status>existing</Status>
          <ELEM>
            <TM>2012-11-19T13:00:00</TM>
          </ELEM>
          <FOO>
            <BAR>xxxxx</BAR>
            <NM>
              <Nm>yyyyyyy</Nm>
            </NM>
          </FOO>
        </ENTRY>

From what I saw, it's possible to walk the root document, however, I am getting a bit lost on accessing parent and child nodes:

        void walk(xml_node parent)
        {
            for(xml_node child = parent.first_child(); child; child = child.next_sibling())
            {
              // ... Would like to output:  "FOO: xxxx  / NM:  yyyyyyyy"
            }
        }

Solution

  • You can give parameters to the first_child() and other member functions:

    auto Outer = document.first_child("OUTER");
    auto Entry = Outer.first_child("ENTRY");
    auto Foo = Entry.first_child("FOO");
    

    etc.

    When you finally get to the destination, use .value() to get to the node value.

    PugiXML also features XPath support, but that would probably be overkill in that case.