documentation

This commit is contained in:
Marc Wäckerlin
2009-04-07 06:59:17 +00:00
parent 29997999d6
commit bca65f97a0
2 changed files with 71 additions and 36 deletions

View File

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