Main Page   Class Hierarchy   Alphabetical List   Compound List   Compound Members  

util::xml::Node Class Reference

#include <XMLDocument.h>

Inheritance diagram for util::xml::Node:

util::Object util::xml::Attr util::xml::CharacterData util::xml::Declaration util::xml::Document util::xml::Element util::xml::ProcessingInstruction util::xml::Comment util::xml::Text util::xml::CDATASection List of all members.

Detailed Description

A base class for all XML markup elements.

The names and the fields of this class and its subclasses conform roughly to the DOM 1.0 specification.

Note that when a Node is deleted, all of its children are deleted as well.


Public Types

enum  NodeType {
  DOCUMENT_NODE, ELEMENT_NODE, ATTRIBUTE_NODE, PROCESSING_INSTRUCTION_NODE,
  DECLARATION_NODE, TEXT_NODE, COMMENT_NODE, CDATA_SECTION_NODE
}
 An enumeration of different node types. More...


Public Methods

virtual ~Node ()
 Destroy a node and all of its children.

NodeType getNodeType () const
 Get the actual type of this node.

void appendChild (Node *child)
 Add a child node to this node.

void removeChild (Node *child)
 Remove a child node from this node.

List< Node * > getChildNodes () const
 Get the children of this node in insertion order.

List< Node * > getChildNodes (NodeType type, std::string name="") const
 Get the child nodes whose type matches to the given type (and name).

const List< Node * > & childNodes () const
 Get the children of this node in insertion order.

List< Node * > & childNodes ()
 Get the children of this node in insertion order.

bool hasChildNodes () const
 Check whether this node has children.

const Node * getParentNode () const
 Get the parent node.

Node * getParentNode ()
 Get the parent node.

void setParentNode (Node *parent)
 Set the parent node.

Node & operator= (const Node &other)
 Copy a node.

const Node * getChildNode (std::string name) const
 Get a pointer to a node rooted at this node.

virtual std::string getName () const
 Get the name of this node.

std::string getTypeName () const
 Get the type name of this node.

bool isInternalName (std::string name) const
 Check if a name is an internal name, i.e.

virtual void printOut (std::ostream &out) const=0 throw (util::io::IOException&)
 Print the contents of a node to a stream.


Protected Methods

 Node (NodeType type)
 Node cannot be directly instantiated.

 Node (const Node &other)
 Copy a node.


Protected Attributes

Node * _pParent
 A pointer to the parent node.

List< Node * > _plstChildren
 A list of child nodes.


Member Enumeration Documentation

enum util::xml::Node::NodeType
 

An enumeration of different node types.

Each node type has a corresponding class:


Constructor & Destructor Documentation

util::xml::Node::Node NodeType    type [inline, protected]
 

Node cannot be directly instantiated.

Instead, subclasses must provide it a type when instantiating.

util::xml::Node::Node const Node &    other [inline, protected]
 

Copy a node.

Only the type of the node is copied.


Member Function Documentation

const Node * util::xml::Node::getChildNode std::string    name const
 

Get a pointer to a node rooted at this node.

This method has no DOM correspondent. Let us suppose you have a Document node with a root node labeled 'root'. You can get a reference to the root node by using "document.element[root]" or just "document.root" as a node name. The attributes of 'root' would be accessible through "document.root.attribute[name]". (See explanation below.) An internal name is associated with each node type:

  • DOCUMENT_NODE - document
  • ELEMENT_NODE - element
  • ATTRIBUTE_NODE - attribute
  • PROCESSING_INSTRUCTION_NODE - instruction
  • DECLARATION_NODE - declaration
  • TEXT_NODE - text
  • COMMENT_NODE - comment
  • CDATA_SECTION_NODE - cdata
When a node has child nodes, they can be referenced using a bracket notation or directly by name. In cases where internal node names clash with element names in markup, elements can be referenced using '.element[name]' notation. The same applies to attributes. Examples:

  1. document.root.text - the first text element within 'root'
  2. document.root.element[text][1] - the second subelement of 'root' with the name 'text'
  3. document.root.element[text][1].attribute[atr] - an attribute named 'atr' in the second subelement of 'root' with the name 'text'
  4. document.root.attribute[type] - attribute 'type' in 'root' tag.
Consider the following XML document:
 <?xml version="1.0">
 <root type="foo">
 This is the first text section.
 <text>Here we have some more text.</text>
 <text attr="foo">And here, too.</text>
 </root>
 

For this document, the previous examples would give the following results:

  1. A Text node with the contents "\nThis is the first text section.
    " (
    's represent newlines)
  2. An Element node with two children: an attribute named 'attr' and a text node with the contents "And here, too."
  3. NULL, as the attribute name is misspelled.
  4. An Attr node with the contents "foo".
If the context of the search (i.e. 'element' or 'attribute') is not given, the first child whose name matches is returned. For example:

 <?xml version="1.0">
 <root foo="X">
 <foo/>
 </root>
 

Now, using 'document.root.foo' as a node path returns the attribute 'foo', but 'document.root.element[foo]' returns the empty child element.

Parameters:
name  a path to the node
Returns:
a pointer to the required node, or NULL if the node cannot be found

List< Node * > util::xml::Node::getChildNodes NodeType    type,
std::string    name = ""
const
 

Get the child nodes whose type matches to the given type (and name).

For example, getChildNodes(Node::ATTRIBUTE_NODE) returns all attributes that are direct childs of this node. In a similar manner, getChildNodes(Node::ELEMENT_NODE,"child") returns all direct child elements whose name is "child". This method has no DOM correspondent.

Parameters:
type  the type of the nodes to return

virtual std::string util::xml::Node::getName   const [inline, virtual]
 

Get the name of this node.

Attributes, elements, processing instructions and declarations return the name found in the markup. Other node types return an internal name like 'document', 'text' etc.

See also:
getChildNode(string)

Reimplemented in util::xml::Attr, util::xml::ProcessingInstruction, util::xml::Declaration, and util::xml::Element.

NodeType util::xml::Node::getNodeType   const [inline]
 

Get the actual type of this node.

If this returns, say, DOCUMENT_NODE, a Node* can be safely casted to Document*.

string util::xml::Node::getTypeName   const
 

Get the type name of this node.

See also:
getChildNode(string)

bool util::xml::Node::isInternalName std::string    name const
 

Check if a name is an internal name, i.e.

'document', 'text' etc.

See also:
getChildNode(string)

Node & util::xml::Node::operator= const Node &    other
 

Copy a node.

Only the type of the node is copied.

virtual void util::xml::Node::printOut std::ostream &    out const throw (util::io::IOException&) [pure virtual]
 

Print the contents of a node to a stream.

The output of this method is well-formed XML. Calling the printOut method of a Document object produces a well-formed XML document. All other nodes produce a well-formed XML document fraction.

Exceptions:
IOException  & if something goes wrong while writing

Implemented in util::xml::Text, util::xml::Comment, util::xml::CDATASection, util::xml::Attr, util::xml::ProcessingInstruction, util::xml::Declaration, util::xml::Element, and util::xml::Document.


The documentation for this class was generated from the following files:
Documentation generated on 11.09.2003 with Doxygen.
The documentation is copyrighted material.
Copyright © Topi Mäenpää 2003. All rights reserved.