xmlTiny::TiXmlHandle Class Reference

A TiXmlHandle is a class that wraps a node pointer with null checks; this is an incredibly useful thing. More...

#include <OBTtinyxml.h>

Collaboration diagram for xmlTiny::TiXmlHandle:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 TiXmlHandle (TiXmlNode *_node)
 Create a handle from any node (at any depth of the tree.) This can be a null pointer.
 TiXmlHandle (const TiXmlHandle &ref)
 Copy constructor.
TiXmlHandle operator= (const TiXmlHandle &ref)
TiXmlHandle FirstChild () const
 Return a handle to the first child node.
TiXmlHandle FirstChild (const char *value) const
 Return a handle to the first child node with the given name.
TiXmlHandle FirstChildElement () const
 Return a handle to the first child element.
TiXmlHandle FirstChildElement (const char *value) const
 Return a handle to the first child element with the given name.
TiXmlHandle Child (const char *value, int index) const
 Return a handle to the "index" child with the given name.
TiXmlHandle Child (int index) const
 Return a handle to the "index" child.
TiXmlHandle ChildElement (const char *value, int index) const
 Return a handle to the "index" child element with the given name.
TiXmlHandle ChildElement (int index) const
 Return a handle to the "index" child element.
TiXmlHandle FirstChild (const std::string &_value) const
TiXmlHandle FirstChildElement (const std::string &_value) const
TiXmlHandle Child (const std::string &_value, int index) const
TiXmlHandle ChildElement (const std::string &_value, int index) const
TiXmlNodeToNode () const
 Return the handle as a TiXmlNode.
TiXmlElementToElement () const
 Return the handle as a TiXmlElement.
TiXmlTextToText () const
 Return the handle as a TiXmlText.
TiXmlUnknownToUnknown () const
 Return the handle as a TiXmlUnknown.
TiXmlNodeNode () const
TiXmlElementElement () const
TiXmlTextText () const
TiXmlUnknownUnknown () const

Private Attributes

TiXmlNodenode


Detailed Description

A TiXmlHandle is a class that wraps a node pointer with null checks; this is an incredibly useful thing.

Note that TiXmlHandle is not part of the TinyXml DOM structure. It is a separate utility class.

Take an example:

	<Document>
		<Element attributeA = "valueA">
			<Child attributeB = "value1" />
			<Child attributeB = "value2" />
		</Element>
	<Document>
	

Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very easy to write a *lot* of code that looks like:

	TiXmlElement* root = document.FirstChildElement( "Document" );
	if ( root )
	{
		TiXmlElement* element = root->FirstChildElement( "Element" );
		if ( element )
		{
			TiXmlElement* child = element->FirstChildElement( "Child" );
			if ( child )
			{
				TiXmlElement* child2 = child->NextSiblingElement( "Child" );
				if ( child2 )
				{
					// Finally do something useful.
	

And that doesn't even cover "else" cases. TiXmlHandle addresses the verbosity of such code. A TiXmlHandle checks for null pointers so it is perfectly safe and correct to use:

	TiXmlHandle docHandle( &document );
	TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).ToElement();
	if ( child2 )
	{
		// do something useful
	

Which is MUCH more concise and useful.

It is also safe to copy handles - internally they are nothing more than node pointers.

	TiXmlHandle handleCopy = handle;
	

What they should not be used for is iteration:

	int i=0; 
	while ( true )
	{
		TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", i ).ToElement();
		if ( !child )
			break;
		// do something
		++i;
	}
	

It seems reasonable, but it is in fact two embedded while loops. The Child method is a linear walk to find the element, so this code would iterate much more than it needs to. Instead, prefer:

	TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild( "Child" ).ToElement();

	for( child; child; child=child->NextSiblingElement() )
	{
		// do something
	}
	

Definition at line 1639 of file tiny/tmp/OBTtinyxml.h.


Constructor & Destructor Documentation

xmlTiny::TiXmlHandle::TiXmlHandle ( TiXmlNode _node  )  [inline]

Create a handle from any node (at any depth of the tree.) This can be a null pointer.

Definition at line 1643 of file tiny/tmp/OBTtinyxml.h.

References node.

Referenced by Child(), ChildElement(), FirstChild(), and FirstChildElement().

01643 { this->node = _node; }

xmlTiny::TiXmlHandle::TiXmlHandle ( const TiXmlHandle ref  )  [inline]

Copy constructor.

Definition at line 1645 of file tiny/tmp/OBTtinyxml.h.

References node.

01645 { this->node = ref.node; }


Member Function Documentation

TiXmlHandle xmlTiny::TiXmlHandle::operator= ( const TiXmlHandle ref  )  [inline]

Definition at line 1646 of file tiny/tmp/OBTtinyxml.h.

References node.

01646 { this->node = ref.node; return *this; }

TiXmlHandle xmlTiny::TiXmlHandle::FirstChild (  )  const

Return a handle to the first child node.

Definition at line 1637 of file tiny/tmp/OBTtinyxml.cpp.

References TiXmlHandle().

Referenced by FirstChild().

01638 {
01639         if ( node )
01640         {
01641                 TiXmlNode* child = node->FirstChild();
01642                 if ( child )
01643                         return TiXmlHandle( child );
01644         }
01645         return TiXmlHandle( 0 );
01646 }

TiXmlHandle xmlTiny::TiXmlHandle::FirstChild ( const char *  value  )  const

Return a handle to the first child node with the given name.

Definition at line 1649 of file tiny/tmp/OBTtinyxml.cpp.

References TiXmlHandle().

01650 {
01651         if ( node )
01652         {
01653                 TiXmlNode* child = node->FirstChild( value );
01654                 if ( child )
01655                         return TiXmlHandle( child );
01656         }
01657         return TiXmlHandle( 0 );
01658 }

TiXmlHandle xmlTiny::TiXmlHandle::FirstChildElement (  )  const

Return a handle to the first child element.

Definition at line 1661 of file tiny/tmp/OBTtinyxml.cpp.

References TiXmlHandle().

Referenced by FirstChildElement().

01662 {
01663         if ( node )
01664         {
01665                 TiXmlElement* child = node->FirstChildElement();
01666                 if ( child )
01667                         return TiXmlHandle( child );
01668         }
01669         return TiXmlHandle( 0 );
01670 }

TiXmlHandle xmlTiny::TiXmlHandle::FirstChildElement ( const char *  value  )  const

Return a handle to the first child element with the given name.

Definition at line 1673 of file tiny/tmp/OBTtinyxml.cpp.

References TiXmlHandle().

01674 {
01675         if ( node )
01676         {
01677                 TiXmlElement* child = node->FirstChildElement( value );
01678                 if ( child )
01679                         return TiXmlHandle( child );
01680         }
01681         return TiXmlHandle( 0 );
01682 }

TiXmlHandle xmlTiny::TiXmlHandle::Child ( const char *  value,
int  index 
) const

Return a handle to the "index" child with the given name.

The first child is 0, the second 1, etc.

Definition at line 1704 of file tiny/tmp/OBTtinyxml.cpp.

References xmlTiny::TiXmlNode::NextSibling(), and TiXmlHandle().

Referenced by Child().

01705 {
01706         if ( node )
01707         {
01708                 int i;
01709                 TiXmlNode* child = node->FirstChild( value );
01710                 for (   i=0;
01711                                 child && i<count;
01712                                 child = child->NextSibling( value ), ++i )
01713                 {
01714                         // nothing
01715                 }
01716                 if ( child )
01717                         return TiXmlHandle( child );
01718         }
01719         return TiXmlHandle( 0 );
01720 }

TiXmlHandle xmlTiny::TiXmlHandle::Child ( int  index  )  const

Return a handle to the "index" child.

The first child is 0, the second 1, etc.

Definition at line 1685 of file tiny/tmp/OBTtinyxml.cpp.

References xmlTiny::TiXmlNode::NextSibling(), and TiXmlHandle().

01686 {
01687         if ( node )
01688         {
01689                 int i;
01690                 TiXmlNode* child = node->FirstChild();
01691                 for (   i=0;
01692                                 child && i<count;
01693                                 child = child->NextSibling(), ++i )
01694                 {
01695                         // nothing
01696                 }
01697                 if ( child )
01698                         return TiXmlHandle( child );
01699         }
01700         return TiXmlHandle( 0 );
01701 }

TiXmlHandle xmlTiny::TiXmlHandle::ChildElement ( const char *  value,
int  index 
) const

Return a handle to the "index" child element with the given name.

The first child element is 0, the second 1, etc. Note that only TiXmlElements are indexed: other types are not counted.

Definition at line 1742 of file tiny/tmp/OBTtinyxml.cpp.

References xmlTiny::TiXmlNode::NextSiblingElement(), and TiXmlHandle().

Referenced by ChildElement().

01743 {
01744         if ( node )
01745         {
01746                 int i;
01747                 TiXmlElement* child = node->FirstChildElement( value );
01748                 for (   i=0;
01749                                 child && i<count;
01750                                 child = child->NextSiblingElement( value ), ++i )
01751                 {
01752                         // nothing
01753                 }
01754                 if ( child )
01755                         return TiXmlHandle( child );
01756         }
01757         return TiXmlHandle( 0 );
01758 }

TiXmlHandle xmlTiny::TiXmlHandle::ChildElement ( int  index  )  const

Return a handle to the "index" child element.

The first child element is 0, the second 1, etc. Note that only TiXmlElements are indexed: other types are not counted.

Definition at line 1723 of file tiny/tmp/OBTtinyxml.cpp.

References xmlTiny::TiXmlNode::NextSiblingElement(), and TiXmlHandle().

01724 {
01725         if ( node )
01726         {
01727                 int i;
01728                 TiXmlElement* child = node->FirstChildElement();
01729                 for (   i=0;
01730                                 child && i<count;
01731                                 child = child->NextSiblingElement(), ++i )
01732                 {
01733                         // nothing
01734                 }
01735                 if ( child )
01736                         return TiXmlHandle( child );
01737         }
01738         return TiXmlHandle( 0 );
01739 }

TiXmlHandle xmlTiny::TiXmlHandle::FirstChild ( const std::string &  _value  )  const [inline]

Definition at line 1677 of file tiny/tmp/OBTtinyxml.h.

References FirstChild().

01677 { return FirstChild( _value.c_str() ); }

TiXmlHandle xmlTiny::TiXmlHandle::FirstChildElement ( const std::string &  _value  )  const [inline]

Definition at line 1678 of file tiny/tmp/OBTtinyxml.h.

References FirstChildElement().

01678 { return FirstChildElement( _value.c_str() ); }

TiXmlHandle xmlTiny::TiXmlHandle::Child ( const std::string &  _value,
int  index 
) const [inline]

Definition at line 1680 of file tiny/tmp/OBTtinyxml.h.

References Child().

01680 { return Child( _value.c_str(), index ); }

TiXmlHandle xmlTiny::TiXmlHandle::ChildElement ( const std::string &  _value,
int  index 
) const [inline]

Definition at line 1681 of file tiny/tmp/OBTtinyxml.h.

References ChildElement().

01681 { return ChildElement( _value.c_str(), index ); }

TiXmlNode* xmlTiny::TiXmlHandle::ToNode (  )  const [inline]

Return the handle as a TiXmlNode.

This may return null.

Definition at line 1686 of file tiny/tmp/OBTtinyxml.h.

References node.

Referenced by Node().

01686 { return node; } 

TiXmlElement* xmlTiny::TiXmlHandle::ToElement (  )  const [inline]

Return the handle as a TiXmlElement.

This may return null.

Definition at line 1689 of file tiny/tmp/OBTtinyxml.h.

References node, and xmlTiny::TiXmlNode::ToElement().

Referenced by Element().

01689 { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }

TiXmlText* xmlTiny::TiXmlHandle::ToText (  )  const [inline]

Return the handle as a TiXmlText.

This may return null.

Definition at line 1692 of file tiny/tmp/OBTtinyxml.h.

References node, and xmlTiny::TiXmlNode::ToText().

Referenced by Text().

01692 { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }

TiXmlUnknown* xmlTiny::TiXmlHandle::ToUnknown (  )  const [inline]

Return the handle as a TiXmlUnknown.

This may return null.

Definition at line 1695 of file tiny/tmp/OBTtinyxml.h.

References node, and xmlTiny::TiXmlNode::ToUnknown().

Referenced by Unknown().

01695 { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }

TiXmlNode* xmlTiny::TiXmlHandle::Node (  )  const [inline]

Definition at line 1700 of file tiny/tmp/OBTtinyxml.h.

References ToNode().

01700 { return ToNode(); } 

TiXmlElement* xmlTiny::TiXmlHandle::Element (  )  const [inline]

Definition at line 1704 of file tiny/tmp/OBTtinyxml.h.

References ToElement().

01704 { return ToElement(); }

TiXmlText* xmlTiny::TiXmlHandle::Text (  )  const [inline]

Definition at line 1708 of file tiny/tmp/OBTtinyxml.h.

References ToText().

01708 { return ToText(); }

TiXmlUnknown* xmlTiny::TiXmlHandle::Unknown (  )  const [inline]

Definition at line 1712 of file tiny/tmp/OBTtinyxml.h.

References ToUnknown().

01712 { return ToUnknown(); }


Member Data Documentation

TiXmlNode* xmlTiny::TiXmlHandle::node [private]

Definition at line 1715 of file tiny/tmp/OBTtinyxml.h.

Referenced by operator=(), TiXmlHandle(), ToElement(), ToNode(), ToText(), and ToUnknown().


Generated on Wed Oct 1 11:34:26 2008 for OBT by  doxygen 1.5.3