poseid
poseid

Reputation: 7156

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"
            }
        }

Upvotes: 1

Views: 3541

Answers (2)

jav
jav

Reputation: 654

Pugi has also a simple path facility 5.10. Miscellaneous functions

You could do the above with just one line:

document.first_element_by_path("/Document/Outer/ENTRY/FOO/BAR").first_child().value();

Upvotes: 0

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39370

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.

Upvotes: 2

Related Questions