update to new build system
This commit is contained in:
60
examples/address.cxx
Normal file
60
examples/address.cxx
Normal file
@@ -0,0 +1,60 @@
|
||||
/*! @file
|
||||
|
||||
@id $Id$
|
||||
*/
|
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#include <xml-cxx/xml.hxx>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
int main(int, char**) try {
|
||||
// Example template in factory instantiation
|
||||
xml::Factory addrTpl(xml::Node("address")
|
||||
<<(xml::Node("name").limits(1, 1)
|
||||
<<xml::String("first").limits(1, 1)
|
||||
<<xml::String("middle") // 0..n -> .limit(0, 0)
|
||||
<<xml::String("last").limits(1, 1))
|
||||
<<(xml::Node("location").max(1)
|
||||
<<xml::String("line").min(1))
|
||||
<<xml::String("email")
|
||||
<<xml::String("url")
|
||||
<<xml::String("country").max(1));
|
||||
// Example XML file to read
|
||||
std::stringstream ss("\
|
||||
<address>\
|
||||
<name>\
|
||||
<first>Marc</first>\
|
||||
<middle>Roman</middle>\
|
||||
<last>Wäckerlin</last>\
|
||||
</name>\
|
||||
<location>\
|
||||
<line>SwissSign AG</line>\
|
||||
<line>Pfingstweidstrasse 60b</line>\
|
||||
<line>8005 Zürich</line>\
|
||||
</location>\
|
||||
<country>Schweiz</country>\
|
||||
<email>marc@waeckerlin.org</email>\
|
||||
<email>marc.waeckerlin@swisssign.com</email>\
|
||||
<url>http://dev.swisssign.com/trac/libxml-cxx</url>\
|
||||
<url>http://marc.wäckerlin.ch</url>\
|
||||
<url>http://marc.waeckerlin.org</url>\
|
||||
<url>http://dev.swisssign.com</url>\
|
||||
<url>http://swissign.com</url>\
|
||||
<url>http://swissign.ch</url>\
|
||||
</address>");
|
||||
std::auto_ptr<xml::Node> author(addrTpl.read(ss));
|
||||
// write to stdout
|
||||
std::cout<<"Successfully read:"<<std::endl
|
||||
<<"------------------------------"<<std::endl
|
||||
<<*author<<std::endl
|
||||
<<"------------------------------"<<std::endl;
|
||||
// store in project's AUTHORS file
|
||||
std::ofstream ofs("../AUTHORS");
|
||||
ofs<<*author<<std::endl;
|
||||
return 0;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr<<"**** Error: "<<e.what()<<std::endl;
|
||||
}
|
57
examples/contain_serialization.cxx
Normal file
57
examples/contain_serialization.cxx
Normal file
@@ -0,0 +1,57 @@
|
||||
/*! @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")
|
||||
.persist(a, "a");
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
47
examples/inherit_serialization.cxx
Normal file
47
examples/inherit_serialization.cxx
Normal file
@@ -0,0 +1,47 @@
|
||||
/*! @file
|
||||
|
||||
@id $Id$
|
||||
*/
|
||||
// 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 A { // A inherits xml::Serialize, B inherits A
|
||||
public:
|
||||
int b;
|
||||
protected:
|
||||
void initXmlMembers() {
|
||||
A::initXmlMembers(); // <- Here is the important difference
|
||||
className("B");
|
||||
persist(b, "b");
|
||||
}
|
||||
};
|
||||
|
||||
int main(int, char**) {
|
||||
std::stringstream ss("<B>\n"
|
||||
"\t<b>1234</b>\n"
|
||||
"\t<a>5678</a>\n"
|
||||
"\t<txt>Dies ist ein Serialisierungs-Test</txt>\n"
|
||||
"</B>");
|
||||
B b;
|
||||
b.loadXml(ss);
|
||||
if (b.b==1234) b.b=4321;
|
||||
if (b.a==5678) b.a=8765;
|
||||
b.saveXml(std::cout)<<std::endl;
|
||||
return 0;
|
||||
}
|
91
examples/list_serialization.cxx
Normal file
91
examples/list_serialization.cxx
Normal file
@@ -0,0 +1,91 @@
|
||||
/*! @file
|
||||
|
||||
@id $Id: inherit_serialization.cxx 26 2009-04-24 07:13:10Z $
|
||||
*/
|
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#include <xml-cxx/xml.hxx>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <list>
|
||||
|
||||
class A: public xml::Serialize {
|
||||
public:
|
||||
xml::List<std::string> list;
|
||||
protected:
|
||||
void initXmlMembers() {
|
||||
className("A");
|
||||
persist(list, "list");
|
||||
}
|
||||
};
|
||||
|
||||
class B: public xml::Serialize {
|
||||
public:
|
||||
int b;
|
||||
A a;
|
||||
xml::List<A> as;
|
||||
protected:
|
||||
void initXmlMembers() {
|
||||
className("B");
|
||||
persist(b, "b");
|
||||
persist(a, "a");
|
||||
persist(as, "As");
|
||||
}
|
||||
};
|
||||
|
||||
int main(int, char**) {
|
||||
std::stringstream ss("<B>"
|
||||
" <b>1234</b>"
|
||||
" <a>"
|
||||
" <list>"
|
||||
" <item>guguseli</item>"
|
||||
" </list>"
|
||||
" </a>"
|
||||
" <As>"
|
||||
" <A>"
|
||||
" <list>"
|
||||
" <item>Hello</item>"
|
||||
" <item>World</item>"
|
||||
" <item>how</item>"
|
||||
" <item>are</item>"
|
||||
" <item>you</item>"
|
||||
" </list>"
|
||||
" </A>"
|
||||
" <A>"
|
||||
" <list>"
|
||||
" <item>a</item>"
|
||||
" <item>b</item>"
|
||||
" <item>c</item>"
|
||||
" <item>d</item>"
|
||||
" <item>e</item>"
|
||||
" </list>"
|
||||
" </A>"
|
||||
" <A>"
|
||||
" <list>"
|
||||
" <item>f</item>"
|
||||
" <item>g</item>"
|
||||
" <item>h</item>"
|
||||
" <item>i</item>"
|
||||
" <item>j</item>"
|
||||
" </list>"
|
||||
" </A>"
|
||||
" <A>"
|
||||
" <list>"
|
||||
" <item>k</item>"
|
||||
" <item>l</item>"
|
||||
" <item>m</item>"
|
||||
" <item>n</item>"
|
||||
" <item>o</item>"
|
||||
" </list>"
|
||||
" </A>"
|
||||
" </As>"
|
||||
"</B>");
|
||||
B b;
|
||||
std::cout<<"SCHEMA:"<<std::endl<<b.schema()<<std::endl;
|
||||
b.loadXml(ss);
|
||||
if (b.as.front().list.front()=="Hello") b.as.front().list.front()="Good Bye";
|
||||
if (b.as.front().list.back()=="you") b.as.front().list.back()="we";
|
||||
b.saveXml(std::cout)<<std::endl;
|
||||
return 0;
|
||||
}
|
25
examples/makefile.am
Normal file
25
examples/makefile.am
Normal file
@@ -0,0 +1,25 @@
|
||||
## @id $Id$
|
||||
#
|
||||
# This file has been added by bootstrap.sh on Mon, 13 July 2015 12:42:59 +0200
|
||||
# Feel free to change it or even remove and rebuild it, up to your needs
|
||||
#
|
||||
## 1 2 3 4 5 6 7 8
|
||||
## 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
AM_CPPFLAGS = -I${top_srcdir}/src -I${top_srcdir}/src
|
||||
AM_LDFLAGS = -L${abs_top_builddir}/src/.libs
|
||||
LDADD = -lxml-cxx
|
||||
|
||||
noinst_PROGRAMS = address node_macros serialization \
|
||||
contain_serialization inherit_serialization \
|
||||
list_serialization optional_serialization
|
||||
|
||||
address_SOURCES = address.cxx
|
||||
node_macros_SOURCES = node_macros.cxx
|
||||
serialization_SOURCES = serialization.cxx
|
||||
contain_serialization_SOURCES = contain_serialization.cxx
|
||||
inherit_serialization_SOURCES = inherit_serialization.cxx
|
||||
list_serialization_SOURCES = list_serialization.cxx
|
||||
optional_serialization_SOURCES = optional_serialization.cxx
|
||||
|
||||
MAINTAINERCLEANFILES = makefile.in
|
30
examples/node_macros.cxx
Normal file
30
examples/node_macros.cxx
Normal file
@@ -0,0 +1,30 @@
|
||||
/*! @file
|
||||
|
||||
@id $Id$
|
||||
*/
|
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#include <xml-cxx/xml.hxx>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
XML_NODE(base);
|
||||
XML_NODE(child);
|
||||
XML_STRING(element);
|
||||
|
||||
int main(int, char**) {
|
||||
xml::Factory test(*xml::node::base.clone()
|
||||
<<(*xml::node::child.clone()
|
||||
<<*xml::string::element.clone()));
|
||||
std::stringstream ss("<base>\n"
|
||||
" <child>\n"
|
||||
" <element>Hello</element>\n"
|
||||
" </child>\n"
|
||||
"</base>");
|
||||
std::auto_ptr<xml::Node> file(test.read(ss));
|
||||
std::cout<<"The text in element is: "
|
||||
<<(*file)[xml::name::child][xml::name::element].text()
|
||||
<<std::endl;
|
||||
return 0;
|
||||
}
|
46
examples/optional_serialization.cxx
Normal file
46
examples/optional_serialization.cxx
Normal file
@@ -0,0 +1,46 @@
|
||||
/*! @file
|
||||
|
||||
@id $Id$
|
||||
*/
|
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#include <xml-cxx/xml.hxx>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
class A: public xml::Serialize {
|
||||
public:
|
||||
xml::Optional<int> a;
|
||||
xml::Optional<std::string> txt;
|
||||
protected:
|
||||
void initXmlMembers() {
|
||||
className("A");
|
||||
persist(a, "a");
|
||||
persist(txt, "txt");
|
||||
}
|
||||
};
|
||||
|
||||
class B: public xml::Serialize {
|
||||
public:
|
||||
xml::Optional<A> a;
|
||||
xml::Optional<int> i;
|
||||
xml::Optional<std::string> txt;
|
||||
protected:
|
||||
void initXmlMembers() {
|
||||
className("B");
|
||||
persist(a, "a");
|
||||
persist(i, "i");
|
||||
persist(txt, "txt");
|
||||
}
|
||||
};
|
||||
|
||||
int main(int, char**) {
|
||||
{ // Serialization as a member
|
||||
std::stringstream ss("<B><a><txt>Only this text is given</txt></a></B>");
|
||||
B b;
|
||||
b.loadXml(ss);
|
||||
b.saveXml(std::cout)<<std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
91
examples/serialization.cxx
Normal file
91
examples/serialization.cxx
Normal file
@@ -0,0 +1,91 @@
|
||||
/*! @file
|
||||
|
||||
@id $Id$
|
||||
*/
|
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#include <xml-cxx/xml.hxx>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
//! Class with built in xml::Serialize as member, no inheritance
|
||||
class A {
|
||||
public:
|
||||
A(): _ser("a") {
|
||||
_ser.persist(a, "a");
|
||||
_ser.persist(txt, "txt");
|
||||
}
|
||||
int a;
|
||||
std::string txt;
|
||||
xml::Serialize _ser;
|
||||
};
|
||||
|
||||
//! Class that inherits xml::Serialize
|
||||
class B: public xml::Serialize {
|
||||
public:
|
||||
int a;
|
||||
std::string txt;
|
||||
protected:
|
||||
void initXmlMembers() {
|
||||
className("b");
|
||||
persist(a, "a");
|
||||
persist(txt, "txt");
|
||||
}
|
||||
};
|
||||
|
||||
//! Class with external xml::Serialize
|
||||
class C {
|
||||
public:
|
||||
int a;
|
||||
std::string txt;
|
||||
};
|
||||
|
||||
int main(int, char**) {
|
||||
{ // Serialization as a member
|
||||
std::stringstream ss("<a>\n"
|
||||
"\t<a>1234</a>\n"
|
||||
"\t<txt>Dies ist ein Serialisierungs-Test</txt>\n"
|
||||
"</a>");
|
||||
A a;
|
||||
a._ser.loadXml(ss);
|
||||
if (a.a==1234) a.a=4321;
|
||||
a._ser.saveXml(std::cout)<<std::endl;
|
||||
} { // Inherited Serialization
|
||||
std::stringstream ss("<b>\n"
|
||||
"\t<a>1234</a>\n"
|
||||
"\t<txt>Dies ist ein Serialisierungs-Test</txt>\n"
|
||||
"</b>");
|
||||
B b;
|
||||
b.loadXml(ss);
|
||||
if (b.a==1234) b.a=4321;
|
||||
b.saveXml(std::cout)<<std::endl;
|
||||
} { // External xml::Serialize: "ser" must live in no longer than "c"!
|
||||
std::stringstream ss("<c>\n"
|
||||
"\t<a>1234</a>\n"
|
||||
"\t<txt>Dies ist ein Serialisierungs-Test</txt>\n"
|
||||
"</c>");
|
||||
C c;
|
||||
xml::Serialize ser(xml::Serialize("c")
|
||||
.persist(c.a, "a")
|
||||
.persist(c.txt, "txt"));
|
||||
ser.loadXml(ss);
|
||||
if (c.a==1234) c.a=4321;
|
||||
ser.saveXml(std::cout)<<std::endl;
|
||||
} { // Use xml::Serialize to store anything, e.g. local variables
|
||||
// Important: "ser" must live in no longer than the variables!
|
||||
std::stringstream ss("<d>\n"
|
||||
"\t<a>1234</a>\n"
|
||||
"\t<txt>Dies ist ein Serialisierungs-Test</txt>\n"
|
||||
"</d>");
|
||||
int a;
|
||||
std::string txt;
|
||||
xml::Serialize ser(xml::Serialize("d")
|
||||
.persist(a, "a")
|
||||
.persist(txt, "txt"));
|
||||
ser.loadXml(ss);
|
||||
if (a==1234) a=4321;
|
||||
ser.saveXml(std::cout)<<std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user