|
|
@ -46,7 +46,10 @@ |
|
|
|
All tags are by default specified as 0..n (optional and any number |
|
|
|
All tags are by default specified as 0..n (optional and any number |
|
|
|
there of). |
|
|
|
there of). |
|
|
|
|
|
|
|
|
|
|
|
<code> |
|
|
|
@code |
|
|
|
|
|
|
|
#include <xml-cxx/xml.hxx> |
|
|
|
|
|
|
|
#include <iostream> |
|
|
|
|
|
|
|
[...] |
|
|
|
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) |
|
|
@ -55,11 +58,13 @@ |
|
|
|
<<(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
|
|
|
@ -68,17 +73,30 @@ |
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
</code> */ |
|
|
|
@endcode */ |
|
|
|
namespace xml { |
|
|
|
namespace xml { |
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
//============================================================================
|
|
|
|
enum NodeType {START, END, EMPTY, SPECIAL}; |
|
|
|
//! Type of an xml node.
|
|
|
|
const bool mandatory = true; |
|
|
|
/*! Only start nodes and empty nodes may have attributes. */ |
|
|
|
const bool optional = false; |
|
|
|
enum NodeType { |
|
|
|
|
|
|
|
START, //!< start node, such as <code><node></code>
|
|
|
|
|
|
|
|
END, //!< end node, such as <code></node></code>
|
|
|
|
|
|
|
|
EMPTY, //!< empty node, such as <code><node/></code>
|
|
|
|
|
|
|
|
SPECIAL //!< special node, such as
|
|
|
|
|
|
|
|
//! a comment <code><!-- ... --></code>,
|
|
|
|
|
|
|
|
//! a xml start indication <code><?xml?></code>
|
|
|
|
|
|
|
|
//! or a document type declaration <code><!DOCTYPE ...></code>
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
//! Declares an attribute to be mandatory.
|
|
|
|
|
|
|
|
const bool mandatory(true); |
|
|
|
|
|
|
|
//! Declares an attribute to be optional.
|
|
|
|
|
|
|
|
const bool optional(false); |
|
|
|
|
|
|
|
|
|
|
|
//================================================================= EXCEPTIONS
|
|
|
|
//================================================================= EXCEPTIONS
|
|
|
|
struct Tag; |
|
|
|
struct Tag; |
|
|
@ -243,26 +261,19 @@ namespace xml { |
|
|
|
//! Attribute values ar mainly a std::pair.
|
|
|
|
//! Attribute values ar mainly a std::pair.
|
|
|
|
/*! In addition to a normal std::pair, attributes offer an
|
|
|
|
/*! In addition to a normal std::pair, attributes offer an
|
|
|
|
assignment operator to set the value, and can be constructed |
|
|
|
assignment operator to set the value, and can be constructed |
|
|
|
as empty attribute, given only a key. */ |
|
|
|
as empty attribute, given only a key. |
|
|
|
|
|
|
|
@note Simply use xml::Attr instead of xml::Attributes::Value. */ |
|
|
|
class Value: public value_type { |
|
|
|
class Value: public value_type { |
|
|
|
public: |
|
|
|
public: |
|
|
|
//! Construct an empty attribute.
|
|
|
|
Value(const value_type& o) throw(); |
|
|
|
Value(const std::string& name) throw(); |
|
|
|
Value(const std::string& name) throw(); |
|
|
|
//! Construct an attribute with name an value.
|
|
|
|
|
|
|
|
Value(const std::string& name, const std::string& namevalue) throw(); |
|
|
|
Value(const std::string& name, const std::string& namevalue) throw(); |
|
|
|
//! Assign a value.
|
|
|
|
Value& operator=(const std::string& value) throw(); |
|
|
|
value_type& operator=(const std::string& value) throw(); |
|
|
|
|
|
|
|
//! Get the attribute name.
|
|
|
|
|
|
|
|
const std::string& name() const throw(); |
|
|
|
const std::string& name() const throw(); |
|
|
|
//! Get the attribute value.
|
|
|
|
|
|
|
|
const std::string& value() const throw(); |
|
|
|
const std::string& value() const throw(); |
|
|
|
//! Get the attribute value.
|
|
|
|
|
|
|
|
std::string& value() throw(); |
|
|
|
std::string& value() throw(); |
|
|
|
//! Convert the attribute to a boolean.
|
|
|
|
|
|
|
|
operator bool() const throw(); |
|
|
|
operator bool() const throw(); |
|
|
|
//! Convert the attribute to a number.
|
|
|
|
|
|
|
|
operator unsigned long() const throw(); |
|
|
|
operator unsigned long() const throw(); |
|
|
|
//! Convert the attribute to a space separated list.
|
|
|
|
|
|
|
|
operator List() const throw(); |
|
|
|
operator List() const throw(); |
|
|
|
List toList(const std::string& separators=" \t\n\r") const throw(); |
|
|
|
List toList(const std::string& separators=" \t\n\r") const throw(); |
|
|
|
private: |
|
|
|
private: |
|
|
@ -270,16 +281,16 @@ namespace xml { |
|
|
|
}; |
|
|
|
}; |
|
|
|
Attributes() throw(); |
|
|
|
Attributes() throw(); |
|
|
|
Attributes(const std::string& empty) throw(); |
|
|
|
Attributes(const std::string& empty) throw(); |
|
|
|
Attributes& operator<<(const value_type& v) throw(); |
|
|
|
Attributes(const std::string& key, const std::string& value) throw(); |
|
|
|
|
|
|
|
Attributes& operator<<(const Value& v) throw(); |
|
|
|
Attributes& operator<<(const std::string& key) throw(); |
|
|
|
Attributes& operator<<(const std::string& key) throw(); |
|
|
|
Attributes& operator=(const std::string& value) throw(); |
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
iterator _active; |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
//! Simplification: Use xml::Attr instead of xml::Attributes::Value.
|
|
|
|
typedef Attributes::Value Attr; |
|
|
|
typedef Attributes::Value Attr; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//! @internal structure for parsing tags
|
|
|
|
struct Tag { |
|
|
|
struct Tag { |
|
|
|
std::string name; |
|
|
|
std::string name; |
|
|
|
NodeType type; |
|
|
|
NodeType type; |
|
|
@ -289,6 +300,9 @@ namespace xml { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//! An xml Node.
|
|
|
|
|
|
|
|
/*! XML Nodes may contain either text or other nodes, but not both
|
|
|
|
|
|
|
|
at the same time. */ |
|
|
|
class Node { |
|
|
|
class Node { |
|
|
|
private: |
|
|
|
private: |
|
|
|
typedef std::vector<Node*> Contents; |
|
|
|
typedef std::vector<Node*> Contents; |
|
|
|