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.
template<typename T>
 Node (const std::string &name, const T &c)
Ref ref () const
 Return a Dv::Xml::Node::Ref object referring to this Node.
Ref ref ()
 Return a Dv::Xml::Node::Ref object referring to this Node.
 operator Node::Ref ()
 Return a Dv::Xml::Node::Ref object referring to this Node.
 operator Node::Ref () const
 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 (Exception)
 Retrieve attribute value.
const Ref end () const
 Return Ref that points past the last child of this node.
std::string name () const
 
Returns:
name of underlying xml::node

Nodeadd (const Node::Ref &ref)
 Add another node as a child to this node.
Nodeadd (const std::string &name)
 Add a new node as a child to this node.
Nodeadd (const std::string &name, const std::string &contents)
 Add a new text node as a child to this node.
template<typename T>
Nodeadd (const std::string &name, const T &contents)
 Add a new text node as a child to this node.

Classes

class  AttributeReference
 Auxiliary class for attribute access. More...
class  Ref
 A reference to an xml::node object. More...


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 448 of file node.h.

Referenced by add().

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 453 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 460 of file node.h.

template<typename T>
Dv::Xml::Node::Node ( const std::string &  name,
const T &  c 
) [inline, explicit]

Definition at line 465 of file node.h.

References tostring().


Member Function Documentation

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

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

Definition at line 469 of file node.h.

Referenced by end(), operator Node::Ref(), operator()(), Dv::Xml::operator/(), Dv::Xml::operator>>(), and operator[]().

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

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

Definition at line 474 of file node.h.

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

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

Definition at line 479 of file node.h.

References ref().

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

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

Definition at line 482 of file node.h.

References ref().

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 496 of file node.h.

References ref().

const std::string Dv::Xml::Node::operator() ( const std::string &  name  )  const throw (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 503 of file node.h.

References name(), and ref().

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 510 of file node.h.

References Dv::Xml::Node::Ref::end(), and ref().

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

Returns:
name of underlying xml::node

Definition at line 513 of file node.h.

Referenced by add(), operator()(), Dv::Xml::operator/(), and Dv::Xml::operator>>().

Node& Dv::Xml::Node::add ( const Node::Ref ref  ) 

Add another node as a child to this node.

Parameters:
ref reference to node to add
Returns:
*this

Referenced by Dv::Sql::xml_add(), and Dv::Sql::xml_add_member().

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

Add a new node as a child to this node.

Parameters:
name of new node to add as a child
Returns:
*this

Definition at line 535 of file node.h.

References name(), and Node().

Node& Dv::Xml::Node::add ( const std::string &  name,
const std::string &  contents 
) [inline]

Add a new text node as a child to this node.

Parameters:
name of the new node to add as a child
contents of the new node to add as a child
Returns:
*this

Definition at line 545 of file node.h.

References name(), and Node().

template<typename T>
Node& Dv::Xml::Node::add ( const std::string &  name,
const T &  contents 
) [inline]

Add a new text node as a child to this node.

Parameters:
name of the new node to add as a child
contents of the new node to add as a child
Returns:
*this

Definition at line 556 of file node.h.

References name(), Node(), and Dv::Util::tostring().


The documentation for this class was generated from the following file:
dvxml-0.1.7 [ 7 January, 2008]