File: synopsis-xml.cpp

package info (click to toggle)
libzeep 5.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,596 kB
  • sloc: cpp: 27,393; xml: 7,798; javascript: 180; sh: 37; makefile: 8
file content (38 lines) | stat: -rw-r--r-- 1,064 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <zeep/xml/document.hpp>

//[ synopsis_xml_main
int main()
{
    using namespace zeep::xml::literals; 

    /*<< Construct an XML document in memory using a string literal >>*/
    auto doc = 
        R"(<persons>
            <person id="1">
                <firstname>John</firstname>
                <lastname>Doe</lastname>
            </person>
            <person id="2">
                <firstname>Jane</firstname>
                <lastname>Jones</lastname>
            </person>
        </persons>)"_xml;

    /*<< Iterate over an XPath result set >>*/
    for (auto& person: doc.find("//person")) 
    {
        std::string firstname, lastname;

        /*<< Iterate over the __element__ nodes inside the person __element__ >>*/
        for (auto& name: *person)
        {
            if (name.name() == "firstname")	firstname = name.str();
            if (name.name() == "lastname")	lastname = name.str();
        }

        std::cout << person->get_attribute("id") << ": " << lastname << ", " << firstname << std::endl;
    }

    return 0;
}
//]