Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members

Dv::Xml::Node Class Reference

Convenience interface to xml::node. More...

#include <node.h>

List of all members.

Public Member Functions

 Node ()
 Default constructor.

 Node (const std::string &name)
 Create an xml::node with a given name.

 Node (const std::string &name, const std::string &content)
 Create an xml::node with a given name and content.

Ref ref () const
 Return a Dv::Xml::Node::Ref object referring to this Node.

 operator Ref ()
 Return a Dv::Xml::Node::Ref object referring to this Node.

AttributeReference operator[] (const std::string &name)
 Set attribute value.

const std::string operator() (const std::string &name) const throw (Dv::Xml::Exception)
 Retrieve attribute value.

const Ref end () const
 Return Ref that points past the last child of this node.

std::string name () const


Detailed Description

Convenience interface to xml::node.

There are two classes:

The intention is that Dv::Xml::Node is only used for "roots" in XML tree structures, as in

 Dv::Xml::Node node("rootlabel");

For all other operations, Dv::Xml::Node::Ref should be used. A strange feature is that Dv::Xml::Node::Ref::operator* returns a reference to itself. The reason is that returning a reference to a Dv::Xml::Node would involve copying the underlying xml::node. On the other hand, returning a reference to the underlying xml::node would imply that the convenient member functions of Dv::Xml::Node::Ref would not be available on the result of Dv::Xml::Node::Ref::operator* .

For convenience, some Dv::Xml::Node::Ref operations are also available on Dv::Xml::Node, either by direct delegation (e.g. Dv::Xml::Node::operator[], Dv::Xml::Node::operator()) or via a user-defined convertor that converst a Dv::Xml::Node to a Dv::Xml::Node::Ref referring to this node.

The main difference with the underlying xmlwrapp interface is that many operations are available through expressions. E.g. (n/"login"/"name")("first_name"), with n a Dv::Xml::Node of Dv::Xml::Node::Ref object, * refers to the first_name attribute of the node obtained from n by taking the first "login" child and then the first "name" child.

Intuitively, for a Dv::Xml::Node::Ref n, n/"label" returns a Dv::Xml::Node::Ref referring to the first child node with "label" as name. The increment operator returns the next child with the same name. E.g.

 Dv::Xml::Node::Ref n;
 ..
 Dv::Xml::Node::Ref c(n/"label");
 ++c // refer to 2nd child of n with name "label"

The > operator can be used to create new child nodes. E.g. n>"label" returns a new child of n with name "label". The same operator also works with a Dv::Xml::Node argument.

 Dv::Xml::Node::Ref n;
 Dv::Xml::Node::Ref c;
 (n >> c) // attach a copy of c as a child to n

Attributes can be set and accessed using operator[] and operator() respectively.

 Dv::Xml::Node::Ref n;
 n["first_name"] = "blabla"; // set attribute
 if (n("first_name") != "blabla") // obtain attribute value
   throw std::logic_error("BUG");

The text content of a Dv::Xml::Node::Ref can be obtained by a conversion (to std::string) operator. Assigning a std::string to a Dv::Xml::Node::Ref has the same effect as xml::node::set_content().

Definition at line 89 of file node.h.


Constructor & Destructor Documentation

Dv::Xml::Node::Node  )  [inline]
 

Default constructor.

Definition at line 446 of file node.h.

Dv::Xml::Node::Node const std::string &  name  )  [inline, explicit]
 

Create an xml::node with a given name.

Parameters:
name of new xml::node.

Definition at line 451 of file node.h.

Dv::Xml::Node::Node const std::string &  name,
const std::string &  content
[inline]
 

Create an xml::node with a given name and content.

Parameters:
name of new xml::node.
content text content of new xml::node.

Definition at line 457 of file node.h.


Member Function Documentation

Ref Dv::Xml::Node::ref  )  const [inline]
 

Return a Dv::Xml::Node::Ref object referring to this Node.

Definition at line 462 of file node.h.

Dv::Xml::Node::operator Ref  )  [inline]
 

Return a Dv::Xml::Node::Ref object referring to this Node.

Definition at line 467 of file node.h.

AttributeReference Dv::Xml::Node::operator[] const std::string &  name  )  [inline]
 

Set attribute value.

Parameters:
name key of attribute
Returns:
Dv::Xml::Node::AttributeReference that can be used to either retrieve the current value of assign a new one. If there
See also:
Dv::Xml::Node::AttributeReference
 Dv::Xml::Node::Ref n;
 ..
 n["name"] = "fred";
 std::cout << n["name"] << std::endl;

Definition at line 481 of file node.h.

const std::string Dv::Xml::Node::operator() const std::string &  name  )  const throw (Dv::Xml::Exception) [inline]
 

Retrieve attribute value.

Parameters:
name key of attribute
Returns:
value of attribute
Exceptions:
Dv::Xml::Exception if this node has no attribute for key "name".

Definition at line 488 of file node.h.

const Ref Dv::Xml::Node::end  )  const [inline]
 

Return Ref that points past the last child of this node.

Returns:
nil Dv::Xml::Node::Ref.

Definition at line 495 of file node.h.

References Dv::Xml::Node::Ref::end().

std::string Dv::Xml::Node::name  )  const [inline]
 

Returns:
name of underlying xml::node

Definition at line 498 of file node.h.


The documentation for this class was generated from the following file:
dvxml-0.1.4 [19 September, 2003]