get list of child nodes

This commit is contained in:
Marc Wäckerlin
2009-04-06 14:57:27 +00:00
parent 38e1b60ef1
commit 29997999d6
3 changed files with 30 additions and 8 deletions

View File

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