File: serialize-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 (36 lines) | stat: -rw-r--r-- 878 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
//[ synopsis_xml_serialize
#include <fstream>
#include <zeep/xml/document.hpp>

struct Person
{
    std::string firstname;
    std::string lastname;

    /*<< A struct we want to serialize needs a `serialize` method >>*/
    template<class Archive>
    void serialize(Archive& ar, const unsigned int version)
    {
        ar & zeep::make_nvp("firstname", firstname)
           & zeep::make_nvp("lastname", lastname);
    }
};

int main()
{
    /*<< Read in a text document containing XML and parse it into a document object >>*/
    std::ifstream file("test.xml");
    zeep::xml::document doc(file);
    
    std::vector<Person> persons;
    /*<< Deserialize all persons into an array >>*/
    doc.deserialize("persons", persons);

    doc.clear();

    /*<< Serialize all persons back into an XML document again >>*/
    doc.serialize("persons", persons);

    return 0;
}
//]