get list of child nodes

master
Marc Wäckerlin 16 years ago
parent 38e1b60ef1
commit 29997999d6
  1. 20
      install-64-and-32-bit-linux.sh
  2. 9
      src/xml-cxx/xml.hxx
  3. 9
      src/xml.cxx

@ -0,0 +1,20 @@
./bootstrap.sh && \
./configure && \
make && \
sudo make install && \
make clean && \
LDFLAGS="-L/usr/lib32 -m32" CXXFLAGS="-m32" ./configure \
--libdir=/usr/local/lib32 \
--build=x86_64 \
--host=i386 && \
make && \
sudo make install && \
make clean && \
./configure \
--prefix=/opt/local/i586-mingw32msvc \
--build=x86_64 \
--host=i586-mingw32msvc && \
make && \
sudo make install && \
make clean

@ -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();

@ -283,9 +283,16 @@ namespace xml {
if (it!=_attributes.end()) return it->second;
return std::string();
}
std::string& Node::attr(const std::string&name) throw() {
std::string& Node::attr(const std::string& name) throw() {
return _attributes[name];
}
Node::List Node::list(const std::string& name) const throw() {
List res;
for (Contents::const_iterator it(_contents.begin());
it!=_contents.end(); ++it)
if ((*it)->_name==name) res.push_back(*it);
return res;
}
bool Node::operator()(const std::string& child) const throw() {
return find(child);
}

Loading…
Cancel
Save