#include <tXmlParser.h>
Public Member Functions | |
node (xmlNode *cur) | |
Constructor from a libxml node. | |
node () | |
Dummy constructor, only of use for stl containers. | |
bool | IsOfType (CHAR const *name) const |
Checks the type (name) of a node. | |
tString | GetName (void) const |
Gets the type (name) of the node. | |
bool | HasProp (CHAR const *prop) const |
Check if a property of the given name exists. | |
tString | GetProp (CHAR const *prop) const |
Get a property of this node as a raw string. | |
bool | GetPropBool (CHAR const *prop) const |
Get a boolean value out of a property. | |
template<typename T> | |
void | GetProp (CHAR const *prop, T &target) const |
Get a property, convert to any type. | |
bool | IsOfType (tString const &name) const |
Checks the type (name) of a node. | |
bool | HasProp (tString const &prop) const |
Check if a property of the given name exists. | |
tString | GetProp (tString const &prop) const |
Get a property of this node as a raw string. | |
bool | GetPropBool (tString const &prop) const |
Get a boolean value out of a property. | |
template<typename T> | |
void | GetProp (tString const &prop, T &target) const |
Get a property, convert to any type. | |
node & | operator++ () |
Move on to the next element on the same level. | |
node const | operator++ (int) |
Move on to the next element on the same level. | |
operator bool () const | |
Does this node actually exist? | |
node | GetFirstChild (void) const |
Get the first child node. | |
Private Types | |
typedef char | CHAR |
change to wchar for future unicode support | |
Private Attributes | |
xmlNode * | m_cur |
The current node, the whole class is a wrapper around it. |
Basic example of use (within a child of tXmlResource):
node cur = GetFileContents(); // node now points to the first child of the Resource tag, which is most likely a text node for (; cur; ++cur) { // iterate throgh the following nodes to find the one we're looking for (ignoring all comments and text nodes) if(cur.IsOfType("whateveryouwant")) { // check if this is the right node // ok, it is the right node, now we want to parse all children and get their "height" property as floats, their "show" property as booleans, and their "name" property as a plain string for(node child = cur.GetFirstChild; child; ++child) { float height; cur.GetProp("height", height); bool show = cur.GetPropBool("show"); tString name = cur.GetProp("name"); // do something with that info... } break; // break out of the loop, we found what we were looking for } }
Definition at line 55 of file tXmlParser.h.
typedef char tXmlParserNamespace::tXmlParser::node::CHAR [private] |
tXmlParserNamespace::tXmlParser::node::node | ( | xmlNode * | cur | ) |
Constructor from a libxml node.
cur | The node this object should be constructed around |
Definition at line 513 of file tXmlParser.cpp.
00513 : m_cur(cur) { 00514 //tASSERT(m_cur); 00515 }
tXmlParserNamespace::tXmlParser::node::node | ( | ) | [inline] |
bool tXmlParserNamespace::tXmlParser::node::IsOfType | ( | CHAR const * | name | ) | const |
Checks the type (name) of a node.
name | The name to be checked |
Definition at line 519 of file tXmlParser.cpp.
References m_cur, and tASSERT.
Referenced by HasProp(), and tXmlParserNamespace::tXmlResource::ValidateXml().
00519 { 00520 tASSERT(m_cur); 00521 return(!xmlStrcmp(m_cur->name, reinterpret_cast<xmlChar const *>(name))); 00522 }
tString tXmlParserNamespace::tXmlParser::node::GetName | ( | void | ) | const |
Gets the type (name) of the node.
Definition at line 525 of file tXmlParser.cpp.
References m_cur, and tASSERT.
Referenced by GetProp(), and tXmlParserNamespace::tXmlResource::ValidateXml().
00525 { 00526 tASSERT(m_cur); 00527 return tString(reinterpret_cast<const char *>(m_cur->name)); 00528 }
bool tXmlParserNamespace::tXmlParser::node::HasProp | ( | CHAR const * | prop | ) | const |
Check if a property of the given name exists.
prop | The name of the property to be checked |
Definition at line 532 of file tXmlParser.cpp.
References m_cur, and tASSERT.
Referenced by GetProp().
00532 { 00533 tASSERT(m_cur); 00534 return xmlHasProp(m_cur, 00535 reinterpret_cast<const xmlChar *> 00536 (prop) 00537 ); 00538 }
Get a property of this node as a raw string.
This function prints a warning if the attribute doesn't exist
prop | The name of the attribute to be read |
Definition at line 543 of file tXmlParser.cpp.
References GetName(), m_cur, st_Breakpoint(), tASSERT, and tERR_WARN.
Referenced by GetPropBool(), and tXmlParserNamespace::tXmlResource::ValidateXml().
00543 { 00544 tASSERT(m_cur); 00545 xmlChar *val = xmlGetProp(m_cur, 00546 reinterpret_cast<const xmlChar *> 00547 (prop) 00548 ); 00549 if(val == 0) { 00550 tERR_WARN(tString("Call for non- existent Attribute '") + tString(prop) + "' of element of type '" + GetName() + '"'); 00551 st_Breakpoint(); 00552 return tString(); 00553 } 00554 tString ret(reinterpret_cast<const char *>(val)); 00555 xmlFree(val); 00556 return(ret); 00557 }
bool tXmlParserNamespace::tXmlParser::node::GetPropBool | ( | CHAR const * | prop | ) | const |
Get a boolean value out of a property.
This function prints a warning if the attribute doesn't exist
prop | The name of the attribute to be read |
Definition at line 562 of file tXmlParser.cpp.
References GetProp().
Referenced by GetProp().
00562 { 00563 tString string(GetProp(prop)); 00564 if (string.empty()) return false; 00565 switch(string[0]) { 00566 case 't': case 'T': 00567 case 'y': case 'Y': 00568 return true; 00569 default: 00570 if(string == "on") return true; 00571 int i; 00572 return string.Convert(i) && i; 00573 } 00574 }
void tXmlParserNamespace::tXmlParser::node::GetProp | ( | CHAR const * | prop, | |
T & | target | |||
) | const [inline] |
Get a property, convert to any type.
This function will print an error message if the extraction failed
prop | The name of the attribute to be read | |
target | The place where the extacted value will be stored |
Definition at line 157 of file tXmlParser.h.
00158 { 00159 if(!(GetProp(prop).Convert(target))) { 00160 tERR_WARN( "Property '" + tString(prop) + "' of node of type '" + GetName() + "' is '" + GetProp(prop) + "' which isn't of type '" + typeid(T).name() + "' as needed."); 00161 }
bool tXmlParserNamespace::tXmlParser::node::IsOfType | ( | tString const & | name | ) | const [inline] |
bool tXmlParserNamespace::tXmlParser::node::HasProp | ( | tString const & | prop | ) | const [inline] |
Check if a property of the given name exists.
Definition at line 75 of file tXmlParser.h.
References IsOfType().
Get a property of this node as a raw string.
Definition at line 76 of file tXmlParser.h.
References HasProp().
bool tXmlParserNamespace::tXmlParser::node::GetPropBool | ( | tString const & | prop | ) | const [inline] |
Get a boolean value out of a property.
Definition at line 77 of file tXmlParser.h.
References GetProp().
void tXmlParserNamespace::tXmlParser::node::GetProp | ( | tString const & | prop, | |
T & | target | |||
) | const [inline] |
Get a property, convert to any type.
Definition at line 78 of file tXmlParser.h.
References GetPropBool().
tXmlParser::node & tXmlParserNamespace::tXmlParser::node::operator++ | ( | ) |
tXmlParser::node const tXmlParserNamespace::tXmlParser::node::operator++ | ( | int | ) |
tXmlParserNamespace::tXmlParser::node::operator bool | ( | ) | const |
tXmlParser::node tXmlParserNamespace::tXmlParser::node::GetFirstChild | ( | void | ) | const |
xmlNode* tXmlParserNamespace::tXmlParser::node::m_cur [private] |
The current node, the whole class is a wrapper around it.
Definition at line 57 of file tXmlParser.h.
Referenced by GetFirstChild(), GetName(), GetProp(), HasProp(), IsOfType(), operator bool(), and operator++().