分类:
2010-01-20 10:00:37
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.
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" );
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