Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

cppdom::XMLNode Class Reference

xml node A node has the following properties name - The element name of the node type - The type of the node. More...

#include <cppdom.h>

Inheritance diagram for cppdom::XMLNode:

Inheritance graph
[legend]
Collaboration diagram for cppdom::XMLNode:

Collaboration graph
[legend]
List of all members.

Public Methods

 XMLNode (XMLContextPtr pctx)
 constructor, takes xml context pointer. More...

 XMLNode (const XMLNode &node)
 ~XMLNode ()
XMLNode & operator= (const XMLNode &node)
 assign operator. More...

XMLContextPtr getContext ()
access to node info
XMLNodeType getType () const
 returns type of node. More...

std::string getName ()
 Returns the local name of the node (the element name). More...

XMLAttributesgetAttrMap ()
 returns attribute map of the node. More...

XMLAttribute getAttribute (const std::string &name) const
 Get the named attribute. More...

bool hasAttribute (const std::string &name) const
 Check if the node has a given attribute. More...

const std::string & getCdata ()
 returns cdata string. More...

node data manipulation
void setType (XMLNodeType type)
 sets new nodetype. More...

void setName (const std::string &name)
 set the node name. More...

void setCdata (const std::string &cdata)
 sets new cdata. More...

void setAttribute (const std::string &attr, const XMLAttribute &value)
 sets new attribute value. More...

void addChild (XMLNodePtr &node)
bool removeChild (XMLNodePtr &node)
bool removeChild (std::string &childName)
bool removeChildren (std::string &childName)
navigation through the nodes
XMLNodeListgetChildren ()
 returns a list of the nodes children. More...

XMLNodePtr getChild (const std::string &name)
 Returns the first child of the given local name. More...

XMLNodeList getChildren (const std::string &name)
 Returns a list of all children (one level deep) with local name of childName. More...

XMLNodeList getChildren (const char *name)
 Returns list of all children. More...

template<class Predicate> XMLNodeList getChildrenPred (Predicate pred)
 Return a list of children that pass the given STL predicate. More...

XMLNode * getParent () const
 Returns our parent. More...

load/save functions
void load (std::istream &in, XMLContextPtr &context)
 loads xml node from input stream. More...

void save (std::ostream &out, int indent=0)
 saves node to xml output stream. More...


Protected Methods

 XMLNode ()
 Default Constructor. More...


Protected Attributes

XMLTagNameHandle mNodeNameHandle
 handle to the real tag name. More...

XMLContextPtr mContext
 smart pointer to the context class. More...

XMLNodeType mNodeType
 The type of the node. More...

XMLAttributes mAttributes
 Attributes of the element. More...

std::string mCdata
 Character data (if there is any). More...

XMLNodeList mNodeList
 stl list with subnodes. More...

XMLNode * mParent
 Our parent. More...


Friends

class XMLParser

Detailed Description

xml node A node has the following properties name - The element name of the node type - The type of the node.

see XMLNodeType children - Child elements of the node cdata - the cdata content if of type cdata

Definition at line 362 of file cppdom.h.


Constructor & Destructor Documentation

cppdom::XMLNode::XMLNode   [protected]
 

Default Constructor.

Definition at line 298 of file cppdom.cpp.

References cppdom::xml_nt_node.

00299       : mNodeType(xml_nt_node), mParent(0)
00300    {}

cppdom::XMLNode::XMLNode XMLContextPtr    pctx [explicit]
 

constructor, takes xml context pointer.

Definition at line 302 of file cppdom.cpp.

References cppdom::xml_nt_node.

00303       : mContext(ctx), mNodeType(xml_nt_node), mParent(0)
00304    {}

cppdom::XMLNode::XMLNode const XMLNode &    node
 

Definition at line 306 of file cppdom.cpp.

00307       : mNodeNameHandle(node.mNodeNameHandle)
00308       , mContext(node.mContext)
00309       , mNodeType(node.mNodeType)
00310       , mAttributes(node.mAttributes)
00311       , mCdata(node.mCdata)
00312       , mNodeList(node.mNodeList)
00313       , mParent(node.mParent)
00314    {}

cppdom::XMLNode::~XMLNode  
 

Definition at line 316 of file cppdom.cpp.

References mNodeList.

00317    {
00318       for (XMLNodeList::iterator i = mNodeList.begin(); i != mNodeList.end(); ++i)
00319       {
00320          (*i)->mParent = NULL;
00321       }
00322    }


Member Function Documentation

void cppdom::XMLNode::addChild XMLNodePtr   node
 

Definition at line 386 of file cppdom.cpp.

References mNodeList.

Referenced by cppdom::XMLParser::parseDocument(), and cppdom::XMLParser::parseNode().

00387    {
00388       node->mParent = this;      // Tell the child who their daddy is
00389       mNodeList.push_back(node);
00390    }

XMLAttribute cppdom::XMLNode::getAttribute const std::string &    name const
 

Get the named attribute.

Returns:
empty string ("") if not found, else the value
Postcondition:
Object does not change.

Definition at line 351 of file cppdom.cpp.

References cppdom::XMLAttributes::get(), and mAttributes.

00352    {
00353       return mAttributes.get(name);
00354    }

XMLAttributes & cppdom::XMLNode::getAttrMap  
 

returns attribute map of the node.

Definition at line 346 of file cppdom.cpp.

References mAttributes.

Referenced by cppdom::XMLParser::parseHeader(), and cppdom::XMLParser::parseNode().

00347    {
00348       return mAttributes;
00349    }

const std::string & cppdom::XMLNode::getCdata  
 

returns cdata string.

@note: This only returns data for nodes that are leaf nodes of type "cdata". Calling this on a node that has cdata as a child does nothing

Definition at line 361 of file cppdom.cpp.

References mCdata.

00362    {
00363       return mCdata;
00364    }

XMLNodePtr cppdom::XMLNode::getChild const std::string &    name
 

Returns the first child of the given local name.

Note:
currently no path-like childname can be passed, like in e.g. msxml

Definition at line 416 of file cppdom.cpp.

References mNodeList, and cppdom::XMLNodePtr.

00417    {
00418       // possible speedup: first search if a handle to the childname is existing
00419       XMLNodeList::const_iterator iter;
00420 
00421       // search for first occurance of node
00422       for(iter = mNodeList.begin(); iter != mNodeList.end(); ++iter)
00423       {
00424          XMLNodePtr np = (*iter);
00425          if (np->getName() == name)
00426          {
00427             return np;
00428          }
00429       }
00430 
00431       // no valid child found
00432       return XMLNodePtr();
00433    }

XMLNodeList cppdom::XMLNode::getChildren const char *    name
 

Returns list of all children.

See also:
getChildren
Note:
Needed because of predicate template matching char*

Definition at line 452 of file cppdom.cpp.

References getChildren(), and cppdom::XMLNodeList.

00453    {
00454       return getChildren(std::string(name));
00455    }

XMLNodeList cppdom::XMLNode::getChildren const std::string &    name
 

Returns a list of all children (one level deep) with local name of childName.

Note:
currently no path-like childname can be passed, like in e.g. msxml If has standard compose() functions, could impl this by calling getChildren(pred)

Definition at line 435 of file cppdom.cpp.

References mNodeList, and cppdom::XMLNodeList.

00436    {
00437       XMLNodeList result(0);
00438       XMLNodeList::const_iterator iter;
00439 
00440       // search for all occurances of nodename and insert them into the new list
00441       for(iter = mNodeList.begin(); iter != mNodeList.end(); ++iter)
00442       {
00443          if ((*iter)->getName() == name)
00444          {
00445             result.push_back(*iter);
00446          }
00447       }
00448 
00449       return result;
00450    }

XMLNodeList & cppdom::XMLNode::getChildren  
 

returns a list of the nodes children.

Definition at line 410 of file cppdom.cpp.

References mNodeList, and cppdom::XMLNodeList.

Referenced by getChildren().

00411    {
00412       return mNodeList;
00413    }

template<class Predicate>
XMLNodeList cppdom::XMLNode::getChildrenPred Predicate    pred [inline]
 

Return a list of children that pass the given STL predicate.

Definition at line 456 of file cppdom.h.

References cppdom::XMLNodeList.

00457       {
00458          XMLNodeList result(0);
00459          XMLNodeList::const_iterator iter;
00460 
00461          // search for all occurances of nodename and insert them into the new list
00462          for(iter = mNodeList.begin(); iter != mNodeList.end(); ++iter)
00463          {
00464             if (pred(*iter))
00465             {
00466                result.push_back(*iter);
00467             }
00468          }
00469          return result;
00470       }

XMLContextPtr cppdom::XMLNode::getContext  
 

Definition at line 537 of file cppdom.cpp.

References mContext.

00538    {
00539       return mContext;
00540    }

std::string cppdom::XMLNode::getName  
 

Returns the local name of the node (the element name).

Definition at line 341 of file cppdom.cpp.

References mContext, and mNodeNameHandle.

00342    {
00343       return mContext->getTagname(mNodeNameHandle);
00344    }

XMLNode * cppdom::XMLNode::getParent   const
 

Returns our parent.

The returned value could be NULL.

Returns:
Our parent, which may be NULL if we have no parent.

Definition at line 457 of file cppdom.cpp.

References mParent.

00458    {
00459       return mParent;
00460    }

XMLNodeType cppdom::XMLNode::getType   const
 

returns type of node.

Definition at line 336 of file cppdom.cpp.

References mNodeType, and cppdom::XMLNodeType.

00337    {
00338       return mNodeType;
00339    }

bool cppdom::XMLNode::hasAttribute const std::string &    name const
 

Check if the node has a given attribute.

Definition at line 356 of file cppdom.cpp.

References cppdom::XMLAttributes::has(), and mAttributes.

00357    {
00358       return mAttributes.has(name);
00359    }

void cppdom::XMLNode::load std::istream &    in,
XMLContextPtr   context
 

loads xml node from input stream.

Exceptions:
throws  cppdom::XMLError when a streaming or parsing error occur

Reimplemented in cppdom::XMLDocument.

Definition at line 463 of file cppdom.cpp.

References cppdom::XMLParser::parseNode().

00464    {
00465       XMLParser parser(in, context->getLocation());
00466       parser.parseNode(*this, context);
00467    }

XMLNode & cppdom::XMLNode::operator= const XMLNode &    node
 

assign operator.

Definition at line 324 of file cppdom.cpp.

References mAttributes, mCdata, mContext, mNodeList, mNodeNameHandle, mNodeType, and mParent.

00325    {
00326       mNodeNameHandle = node.mNodeNameHandle;
00327       mContext = node.mContext;
00328       mNodeType = node.mNodeType;
00329       mAttributes = node.mAttributes;
00330       mCdata = node.mCdata;
00331       mNodeList = node.mNodeList;
00332       mParent = node.mParent;
00333       return *this;
00334    }

bool cppdom::XMLNode::removeChild std::string &    childName
 

Definition at line 398 of file cppdom.cpp.

00399    {
00400       std::cout << "cppdom::XMLNode::removeChild:  not implemented\n";
00401       return false;
00402    }

bool cppdom::XMLNode::removeChild XMLNodePtr   node
 

Definition at line 392 of file cppdom.cpp.

00393    {
00394       std::cout << "cppdom::XMLNode::removeChild:  not implemented\n";
00395       return false;
00396    }

bool cppdom::XMLNode::removeChildren std::string &    childName
 

Definition at line 404 of file cppdom.cpp.

00405    {
00406       std::cout << "cppdom::XMLNode::removeChildren:  not implemented\n";
00407       return false;
00408    }

void cppdom::XMLNode::save std::ostream &    out,
int    indent = 0
 

saves node to xml output stream.

Exceptions:
throws  cppdom::XMLError when a streaming or parsing error occur

Definition at line 470 of file cppdom.cpp.

References mAttributes, mCdata, mContext, mNodeList, mNodeNameHandle, mNodeType, cppdom::xml_nt_cdata, cppdom::xml_nt_leaf, cppdom::xml_nt_node, and cppdom::xml_save_invalid_nodetype.

00471    {
00472       // output indendation spaces
00473       for(int i=0; i<indent; ++i)
00474       {
00475          out << ' ';
00476       }
00477 
00478       // output cdata
00479       if (mNodeType == xml_nt_cdata)
00480       {
00481          out << mCdata.c_str() << std::endl;
00482          return;
00483       }
00484 
00485       // output tag name
00486       out << '<' << mContext->getTagname(mNodeNameHandle);
00487 
00488       // output all attributes
00489       XMLAttributes::const_iterator iter, stop;
00490       iter = mAttributes.begin();
00491       stop = mAttributes.end();
00492 
00493       for(; iter!=stop; ++iter)
00494       {
00495          XMLAttributes::value_type attr = *iter;
00496          out << ' ' << attr.first << '=' << '\"' << attr.second << '\"';
00497       }
00498 
00499       // depending on the nodetype, do output
00500       switch(mNodeType)
00501       {
00502       case xml_nt_node:
00503          {
00504             out << '>' << std::endl;
00505 
00506             // output all subnodes
00507             XMLNodeList::const_iterator iter,stop;
00508             iter = mNodeList.begin();
00509             stop = mNodeList.end();
00510 
00511             for(; iter!=stop; ++iter)
00512             {
00513                (*iter)->save(out, indent+1);
00514             }
00515 
00516             // output indendation spaces
00517             for(int i=0;i<indent;i++)
00518             {
00519                out << ' ';
00520             }
00521 
00522             // output closing tag
00523             out << '<' << '/'
00524                << mContext->getTagname(mNodeNameHandle) << '>' << std::endl;
00525          }
00526          break;
00527       case xml_nt_leaf:
00528          // a leaf has no subnodes
00529          out << '/' << '>' << std::endl;
00530          break;
00531       default:
00532          // unknown nodetype
00533          throw XMLError(xml_save_invalid_nodetype);
00534       }
00535    }

void cppdom::XMLNode::setAttribute const std::string &    attr,
const XMLAttribute   value
 

sets new attribute value.

Postcondition:
Element.attr is set to value. If it didn't exist before, now it does.

Definition at line 381 of file cppdom.cpp.

References cppdom::XMLAttribute::getString(), mAttributes, and cppdom::XMLAttributes::set().

00382    {
00383       mAttributes.set(attr, value.getString());
00384    }

void cppdom::XMLNode::setCdata const std::string &    cdata
 

sets new cdata.

Definition at line 376 of file cppdom.cpp.

References mCdata.

00377    {
00378       mCdata = cdata;
00379    }

void cppdom::XMLNode::setName const std::string &    name
 

set the node name.

Definition at line 371 of file cppdom.cpp.

References mContext, and mNodeNameHandle.

00372    {
00373       mNodeNameHandle = mContext->insertTagname(name);
00374    }

void cppdom::XMLNode::setType XMLNodeType    type
 

sets new nodetype.

Definition at line 366 of file cppdom.cpp.

References mNodeType, and cppdom::XMLNodeType.

00367    {
00368       mNodeType = type;
00369    }


Friends And Related Function Documentation

friend class XMLParser [friend]
 

Reimplemented in cppdom::XMLDocument.

Definition at line 364 of file cppdom.h.


Member Data Documentation

XMLAttributes cppdom::XMLNode::mAttributes [protected]
 

Attributes of the element.

Definition at line 495 of file cppdom.h.

Referenced by getAttribute(), getAttrMap(), hasAttribute(), operator=(), cppdom::XMLDocument::save(), save(), and setAttribute().

std::string cppdom::XMLNode::mCdata [protected]
 

Character data (if there is any).

Definition at line 496 of file cppdom.h.

Referenced by getCdata(), operator=(), cppdom::XMLParser::parseNode(), save(), and setCdata().

XMLContextPtr cppdom::XMLNode::mContext [protected]
 

smart pointer to the context class.

Definition at line 493 of file cppdom.h.

Referenced by getContext(), getName(), operator=(), cppdom::XMLParser::parseDocument(), cppdom::XMLParser::parseNode(), save(), and setName().

XMLNodeList cppdom::XMLNode::mNodeList [protected]
 

stl list with subnodes.

Definition at line 497 of file cppdom.h.

Referenced by addChild(), getChild(), getChildren(), operator=(), cppdom::XMLDocument::save(), save(), and ~XMLNode().

XMLTagNameHandle cppdom::XMLNode::mNodeNameHandle [protected]
 

handle to the real tag name.

Definition at line 492 of file cppdom.h.

Referenced by getName(), operator=(), cppdom::XMLParser::parseDocument(), cppdom::XMLParser::parseHeader(), cppdom::XMLParser::parseNode(), save(), and setName().

XMLNodeType cppdom::XMLNode::mNodeType [protected]
 

The type of the node.

Definition at line 494 of file cppdom.h.

Referenced by getType(), operator=(), cppdom::XMLParser::parseNode(), save(), setType(), and cppdom::XMLDocument::XMLDocument().

XMLNode* cppdom::XMLNode::mParent [protected]
 

Our parent.

Definition at line 498 of file cppdom.h.

Referenced by getParent(), and operator=().


The documentation for this class was generated from the following files:
Generated on Thu Jan 2 21:29:21 2003 for cppdom by doxygen1.2.15