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.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.1 KiB

16 years ago
/*! @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;
}