|
|
@ -47,10 +47,6 @@ |
|
|
|
there of). |
|
|
|
there of). |
|
|
|
|
|
|
|
|
|
|
|
<code> |
|
|
|
<code> |
|
|
|
#include <xml-cxx/xml.hxx> |
|
|
|
|
|
|
|
#include <iostream> |
|
|
|
|
|
|
|
#include <<stream> |
|
|
|
|
|
|
|
[...] |
|
|
|
|
|
|
|
xml::Factory test(xml::Node("persons") // root node
|
|
|
|
xml::Factory test(xml::Node("persons") // root node
|
|
|
|
<<(xml::Node("person") // child of persons
|
|
|
|
<<(xml::Node("person") // child of persons
|
|
|
|
.attr("id", xml::mandatory) |
|
|
|
.attr("id", xml::mandatory) |
|
|
@ -59,13 +55,11 @@ |
|
|
|
<<(xml::Node("friends") // friends of person
|
|
|
|
<<(xml::Node("friends") // friends of person
|
|
|
|
<<(xml::Node("friend") // a friend
|
|
|
|
<<(xml::Node("friend") // a friend
|
|
|
|
.attr("id", xml::mandatory))))); |
|
|
|
.attr("id", xml::mandatory))))); |
|
|
|
[...] |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
std::auto_ptr<xml::Node> persons(test.read(std::ifstream("file.xml))); |
|
|
|
std::auto_ptr<xml::Node> persons(test.read(std::ifstream("file.xml))); |
|
|
|
// Here we can be sure, that our structure is valid,
|
|
|
|
// Here we can be sure, that our structure is valid,
|
|
|
|
// but we must check optional elements before access, otherwise
|
|
|
|
// but we must check optional elements before access, otherwise
|
|
|
|
// we get an exception.
|
|
|
|
// we get an exception.
|
|
|
|
[...] |
|
|
|
|
|
|
|
for (xml::Node::size_type i(0); i<persons.children(); ++i) { |
|
|
|
for (xml::Node::size_type i(0); i<persons.children(); ++i) { |
|
|
|
std::cout<<"Person: "<<*persons[i]["name"]; // exception if no "name"
|
|
|
|
std::cout<<"Person: "<<*persons[i]["name"]; // exception if no "name"
|
|
|
|
if (persons[i]("friends")) // check if "friends" is set
|
|
|
|
if (persons[i]("friends")) // check if "friends" is set
|
|
|
@ -74,7 +68,6 @@ |
|
|
|
std::cout<<" has no friend list"; |
|
|
|
std::cout<<" has no friend list"; |
|
|
|
std::cout<<std::endl; |
|
|
|
std::cout<<std::endl; |
|
|
|
} |
|
|
|
} |
|
|
|
[...] |
|
|
|
|
|
|
|
} catch (const std::exception& x) { |
|
|
|
} catch (const std::exception& x) { |
|
|
|
std::cerr<<"**** Error in file \"file.xml\":"<<std::endl |
|
|
|
std::cerr<<"**** Error in file \"file.xml\":"<<std::endl |
|
|
|
<<x.what()<<std::endl; |
|
|
|
<<x.what()<<std::endl; |
|
|
@ -301,6 +294,7 @@ namespace xml { |
|
|
|
typedef std::vector<Node*> Contents; |
|
|
|
typedef std::vector<Node*> Contents; |
|
|
|
public: |
|
|
|
public: |
|
|
|
typedef Contents::size_type size_type; |
|
|
|
typedef Contents::size_type size_type; |
|
|
|
|
|
|
|
typedef std::vector<Node*> List; |
|
|
|
Node(std::string name) throw(); |
|
|
|
Node(std::string name) throw(); |
|
|
|
Node(const Node& o) throw(); |
|
|
|
Node(const Node& o) throw(); |
|
|
|
Node& operator=(const Node& o) throw(); |
|
|
|
Node& operator=(const Node& o) throw(); |
|
|
@ -321,6 +315,7 @@ namespace xml { |
|
|
|
Node& attr(const std::string& name, bool mandatory) throw(); |
|
|
|
Node& attr(const std::string& name, bool mandatory) throw(); |
|
|
|
std::string attr(const std::string& name) const throw(); |
|
|
|
std::string attr(const std::string& name) const throw(); |
|
|
|
std::string& attr(const std::string& name) throw(); |
|
|
|
std::string& attr(const std::string& name) throw(); |
|
|
|
|
|
|
|
List list(const std::string& name) const throw(); |
|
|
|
bool operator()(const std::string& child) const throw(); |
|
|
|
bool operator()(const std::string& child) const throw(); |
|
|
|
Node& operator<<(const Node& o) throw(cannot_have_children); |
|
|
|
Node& operator<<(const Node& o) throw(cannot_have_children); |
|
|
|
Node& operator<<(const Attributes& o) throw(); |
|
|
|
Node& operator<<(const Attributes& o) throw(); |
|
|
|