C++ class for reading and writing XML structures. No need for a C++ code parser or special pre compiler. Specify a schema entirly in native C++. The schema is verified when XML is read and exceptions are thrown when the XML to be parse is invalid.
58 lines
1.4 KiB
58 lines
1.4 KiB
16 years ago
|
/*! @file
|
||
|
|
||
|
@id $Id: inherit_serialization.cxx 25 2009-04-23 15:10:21Z $
|
||
|
*/
|
||
|
// 1 2 3 4 5 6 7 8
|
||
|
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||
|
|
||
|
#include <xml-cxx/xml.hxx>
|
||
|
#include <iostream>
|
||
|
#include <sstream>
|
||
|
|
||
|
class A: public xml::Serialize {
|
||
|
public:
|
||
|
int a;
|
||
|
std::string txt;
|
||
|
protected:
|
||
|
void initXmlMembers() {
|
||
|
className("a")
|
||
|
.persist(a, "a")
|
||
|
.persist(txt, "txt");
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class B: public xml::Serialize {
|
||
|
public:
|
||
|
int i;
|
||
|
std::string txt;
|
||
|
A a;
|
||
|
protected:
|
||
|
void initXmlMembers() {
|
||
|
className("b")
|
||
|
.persist(i, "i")
|
||
|
.persist(txt, "txt")
|
||
16 years ago
|
.persist(a, "a");
|
||
16 years ago
|
}
|
||
|
};
|
||
|
|
||
|
int main(int, char**) {
|
||
|
{ // Serialization as a member
|
||
|
std::stringstream ss("<b>\n"
|
||
|
"\t<i>1234</i>\n"
|
||
|
"\t<txt>Dies ist Class B</txt>\n"
|
||
|
"\t<a>\n"
|
||
|
"\t\t<a>5678</a>\n"
|
||
|
"\t\t<txt>Dies ist Class A</txt>\n"
|
||
|
"\t</a>"
|
||
|
"</b>");
|
||
|
B b;
|
||
|
b.loadXml(ss);
|
||
|
if (b.i==1234) b.i=4321;
|
||
|
if (b.a.a==5678) b.a.a=8765;
|
||
|
std::cout<<"Text B: "<<b.txt<<std::endl;
|
||
|
std::cout<<"Text A: "<<b.a.txt<<std::endl;
|
||
|
b.saveXml(std::cout)<<std::endl;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|