#include <OBTtinyxml.h>
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 |
TiXmlNode * | ToNode () const |
Return the handle as a TiXmlNode. | |
TiXmlElement * | ToElement () const |
Return the handle as a TiXmlElement. | |
TiXmlText * | ToText () const |
Return the handle as a TiXmlText. | |
TiXmlUnknown * | ToUnknown () const |
Return the handle as a TiXmlUnknown. | |
TiXmlNode * | Node () const |
TiXmlElement * | Element () const |
TiXmlText * | Text () const |
TiXmlUnknown * | Unknown () const |
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 |
TiXmlNode * | ToNode () const |
Return the handle as a TiXmlNode. | |
TiXmlElement * | ToElement () const |
Return the handle as a TiXmlElement. | |
TiXmlText * | ToText () const |
Return the handle as a TiXmlText. | |
TiXmlUnknown * | ToUnknown () const |
Return the handle as a TiXmlUnknown. | |
TiXmlNode * | Node () const |
TiXmlElement * | Element () const |
TiXmlText * | Text () const |
TiXmlUnknown * | Unknown () const |
Private Attributes | |
TiXmlNode * | node |
TiXmlNode * | node |
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 1641 of file OBTtinyxml.h.
OBT::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 1645 of file OBTtinyxml.h.
Referenced by Child(), ChildElement(), FirstChild(), and FirstChildElement().
01645 { this->node = _node; }
OBT::TiXmlHandle::TiXmlHandle | ( | const TiXmlHandle & | ref | ) | [inline] |
Copy constructor.
Definition at line 1647 of file OBTtinyxml.h.
References node.
01647 { this->node = ref.node; }
OBT::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 1644 of file tiny/OBTtinyxml.h.
References node.
01644 { this->node = _node; }
OBT::TiXmlHandle::TiXmlHandle | ( | const TiXmlHandle & | ref | ) | [inline] |
Copy constructor.
Definition at line 1646 of file tiny/OBTtinyxml.h.
References node.
01646 { this->node = ref.node; }
TiXmlHandle OBT::TiXmlHandle::operator= | ( | const TiXmlHandle & | ref | ) | [inline] |
Definition at line 1648 of file OBTtinyxml.h.
References node.
01648 { this->node = ref.node; return *this; }
TiXmlHandle OBT::TiXmlHandle::FirstChild | ( | ) | const |
Return a handle to the first child node.
Definition at line 1636 of file OBTtinyxml.cpp.
References OBT::TiXmlNode::FirstChild(), node, and TiXmlHandle().
Referenced by FirstChild().
01637 { 01638 if ( node ) 01639 { 01640 TiXmlNode* child = node->FirstChild(); 01641 if ( child ) 01642 return TiXmlHandle( child ); 01643 } 01644 return TiXmlHandle( 0 ); 01645 }
TiXmlHandle OBT::TiXmlHandle::FirstChild | ( | const char * | value | ) | const |
Return a handle to the first child node with the given name.
Definition at line 1648 of file OBTtinyxml.cpp.
References OBT::TiXmlNode::FirstChild(), node, and TiXmlHandle().
01649 { 01650 if ( node ) 01651 { 01652 TiXmlNode* child = node->FirstChild( value ); 01653 if ( child ) 01654 return TiXmlHandle( child ); 01655 } 01656 return TiXmlHandle( 0 ); 01657 }
TiXmlHandle OBT::TiXmlHandle::FirstChildElement | ( | ) | const |
Return a handle to the first child element.
Definition at line 1660 of file OBTtinyxml.cpp.
References OBT::TiXmlNode::FirstChildElement(), node, and TiXmlHandle().
Referenced by FirstChildElement().
01661 { 01662 if ( node ) 01663 { 01664 TiXmlElement* child = node->FirstChildElement(); 01665 if ( child ) 01666 return TiXmlHandle( child ); 01667 } 01668 return TiXmlHandle( 0 ); 01669 }
TiXmlHandle OBT::TiXmlHandle::FirstChildElement | ( | const char * | value | ) | const |
Return a handle to the first child element with the given name.
Definition at line 1672 of file OBTtinyxml.cpp.
References OBT::TiXmlNode::FirstChildElement(), node, and TiXmlHandle().
01673 { 01674 if ( node ) 01675 { 01676 TiXmlElement* child = node->FirstChildElement( value ); 01677 if ( child ) 01678 return TiXmlHandle( child ); 01679 } 01680 return TiXmlHandle( 0 ); 01681 }
TiXmlHandle OBT::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 1703 of file OBTtinyxml.cpp.
References OBT::TiXmlNode::FirstChild(), OBT::TiXmlNode::NextSibling(), node, and TiXmlHandle().
Referenced by Child().
01704 { 01705 if ( node ) 01706 { 01707 int i; 01708 TiXmlNode* child = node->FirstChild( value ); 01709 for ( i=0; 01710 child && i<count; 01711 child = child->NextSibling( value ), ++i ) 01712 { 01713 // nothing 01714 } 01715 if ( child ) 01716 return TiXmlHandle( child ); 01717 } 01718 return TiXmlHandle( 0 ); 01719 }
TiXmlHandle OBT::TiXmlHandle::Child | ( | int | index | ) | const |
Return a handle to the "index" child.
The first child is 0, the second 1, etc.
Definition at line 1684 of file OBTtinyxml.cpp.
References OBT::TiXmlNode::FirstChild(), OBT::TiXmlNode::NextSibling(), node, and TiXmlHandle().
01685 { 01686 if ( node ) 01687 { 01688 int i; 01689 TiXmlNode* child = node->FirstChild(); 01690 for ( i=0; 01691 child && i<count; 01692 child = child->NextSibling(), ++i ) 01693 { 01694 // nothing 01695 } 01696 if ( child ) 01697 return TiXmlHandle( child ); 01698 } 01699 return TiXmlHandle( 0 ); 01700 }
TiXmlHandle OBT::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 1741 of file OBTtinyxml.cpp.
References OBT::TiXmlNode::FirstChildElement(), OBT::TiXmlNode::NextSiblingElement(), node, and TiXmlHandle().
Referenced by ChildElement().
01742 { 01743 if ( node ) 01744 { 01745 int i; 01746 TiXmlElement* child = node->FirstChildElement( value ); 01747 for ( i=0; 01748 child && i<count; 01749 child = child->NextSiblingElement( value ), ++i ) 01750 { 01751 // nothing 01752 } 01753 if ( child ) 01754 return TiXmlHandle( child ); 01755 } 01756 return TiXmlHandle( 0 ); 01757 }
TiXmlHandle OBT::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 1722 of file OBTtinyxml.cpp.
References OBT::TiXmlNode::FirstChildElement(), OBT::TiXmlNode::NextSiblingElement(), node, and TiXmlHandle().
01723 { 01724 if ( node ) 01725 { 01726 int i; 01727 TiXmlElement* child = node->FirstChildElement(); 01728 for ( i=0; 01729 child && i<count; 01730 child = child->NextSiblingElement(), ++i ) 01731 { 01732 // nothing 01733 } 01734 if ( child ) 01735 return TiXmlHandle( child ); 01736 } 01737 return TiXmlHandle( 0 ); 01738 }
TiXmlHandle OBT::TiXmlHandle::FirstChild | ( | const std::string & | _value | ) | const [inline] |
TiXmlHandle OBT::TiXmlHandle::FirstChildElement | ( | const std::string & | _value | ) | const [inline] |
TiXmlHandle OBT::TiXmlHandle::Child | ( | const std::string & | _value, | |
int | index | |||
) | const [inline] |
TiXmlHandle OBT::TiXmlHandle::ChildElement | ( | const std::string & | _value, | |
int | index | |||
) | const [inline] |
Definition at line 1683 of file OBTtinyxml.h.
01683 { return ChildElement( _value.c_str(), index ); }
TiXmlNode* OBT::TiXmlHandle::ToNode | ( | ) | const [inline] |
Return the handle as a TiXmlNode.
This may return null.
Definition at line 1688 of file OBTtinyxml.h.
Referenced by Node().
01688 { return node; }
TiXmlElement* OBT::TiXmlHandle::ToElement | ( | ) | const [inline] |
Return the handle as a TiXmlElement.
This may return null.
Definition at line 1691 of file OBTtinyxml.h.
Referenced by Element().
TiXmlText* OBT::TiXmlHandle::ToText | ( | ) | const [inline] |
TiXmlUnknown* OBT::TiXmlHandle::ToUnknown | ( | ) | const [inline] |
Return the handle as a TiXmlUnknown.
This may return null.
Definition at line 1697 of file OBTtinyxml.h.
Referenced by Unknown().
TiXmlNode* OBT::TiXmlHandle::Node | ( | ) | const [inline] |
TiXmlElement* OBT::TiXmlHandle::Element | ( | ) | const [inline] |
TiXmlText* OBT::TiXmlHandle::Text | ( | ) | const [inline] |
TiXmlUnknown* OBT::TiXmlHandle::Unknown | ( | ) | const [inline] |
TiXmlHandle OBT::TiXmlHandle::operator= | ( | const TiXmlHandle & | ref | ) | [inline] |
Definition at line 1647 of file tiny/OBTtinyxml.h.
References node.
01647 { this->node = ref.node; return *this; }
TiXmlHandle OBT::TiXmlHandle::FirstChild | ( | ) | const |
Return a handle to the first child node.
TiXmlHandle OBT::TiXmlHandle::FirstChild | ( | const char * | value | ) | const |
Return a handle to the first child node with the given name.
TiXmlHandle OBT::TiXmlHandle::FirstChildElement | ( | ) | const |
Return a handle to the first child element.
TiXmlHandle OBT::TiXmlHandle::FirstChildElement | ( | const char * | value | ) | const |
Return a handle to the first child element with the given name.
TiXmlHandle OBT::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.
TiXmlHandle OBT::TiXmlHandle::Child | ( | int | index | ) | const |
Return a handle to the "index" child.
The first child is 0, the second 1, etc.
TiXmlHandle OBT::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.
TiXmlHandle OBT::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.
TiXmlHandle OBT::TiXmlHandle::FirstChild | ( | const std::string & | _value | ) | const [inline] |
Definition at line 1678 of file tiny/OBTtinyxml.h.
References FirstChild().
01678 { return FirstChild( _value.c_str() ); }
TiXmlHandle OBT::TiXmlHandle::FirstChildElement | ( | const std::string & | _value | ) | const [inline] |
Definition at line 1679 of file tiny/OBTtinyxml.h.
References FirstChildElement().
01679 { return FirstChildElement( _value.c_str() ); }
TiXmlHandle OBT::TiXmlHandle::Child | ( | const std::string & | _value, | |
int | index | |||
) | const [inline] |
Definition at line 1681 of file tiny/OBTtinyxml.h.
References Child().
01681 { return Child( _value.c_str(), index ); }
TiXmlHandle OBT::TiXmlHandle::ChildElement | ( | const std::string & | _value, | |
int | index | |||
) | const [inline] |
Definition at line 1682 of file tiny/OBTtinyxml.h.
References ChildElement().
01682 { return ChildElement( _value.c_str(), index ); }
TiXmlNode* OBT::TiXmlHandle::ToNode | ( | ) | const [inline] |
Return the handle as a TiXmlNode.
This may return null.
Definition at line 1687 of file tiny/OBTtinyxml.h.
References node.
01687 { return node; }
TiXmlElement* OBT::TiXmlHandle::ToElement | ( | ) | const [inline] |
Return the handle as a TiXmlElement.
This may return null.
Definition at line 1690 of file tiny/OBTtinyxml.h.
References node, and OBT::TiXmlNode::ToElement().
TiXmlText* OBT::TiXmlHandle::ToText | ( | ) | const [inline] |
Return the handle as a TiXmlText.
This may return null.
Definition at line 1693 of file tiny/OBTtinyxml.h.
References node, and OBT::TiXmlNode::ToText().
TiXmlUnknown* OBT::TiXmlHandle::ToUnknown | ( | ) | const [inline] |
Return the handle as a TiXmlUnknown.
This may return null.
Definition at line 1696 of file tiny/OBTtinyxml.h.
References node, and OBT::TiXmlNode::ToUnknown().
TiXmlNode* OBT::TiXmlHandle::Node | ( | ) | const [inline] |
TiXmlElement* OBT::TiXmlHandle::Element | ( | ) | const [inline] |
Definition at line 1705 of file tiny/OBTtinyxml.h.
References ToElement().
01705 { return ToElement(); }
TiXmlText* OBT::TiXmlHandle::Text | ( | ) | const [inline] |
TiXmlUnknown* OBT::TiXmlHandle::Unknown | ( | ) | const [inline] |
Definition at line 1713 of file tiny/OBTtinyxml.h.
References ToUnknown().
01713 { return ToUnknown(); }
TiXmlNode* OBT::TiXmlHandle::node [private] |
Definition at line 1717 of file OBTtinyxml.h.
Referenced by Child(), ChildElement(), FirstChild(), FirstChildElement(), operator=(), TiXmlHandle(), ToElement(), ToNode(), ToText(), and ToUnknown().
TiXmlNode* OBT::TiXmlHandle::node [private] |
Definition at line 1716 of file tiny/OBTtinyxml.h.