Class XMLElement

Inheritance Relationships

Base Type

Class Documentation

class XMLElement : public tinyxml2::XMLNode

The element is a container class. It has a value, the element name, and can contain other elements, text, comments, and unknowns. Elements also contain an arbitrary number of attributes.

Public Types

enum ElementClosingType

Values:

enumerator OPEN
enumerator CLOSED
enumerator CLOSING

Public Functions

inline const char *Name() const

Get the name of an element (which is the Value() of the node.)

inline void SetName(const char *str, bool staticMem = false)

Set the name of the element.

inline virtual XMLElement *ToElement() override

Safely cast to an Element, or null.

inline virtual const XMLElement *ToElement() const override
virtual bool Accept(XMLVisitor *visitor) const override

Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the XML tree will be conditionally visited and the host will be called back via the XMLVisitor interface.

This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this interface versus any other.)

The interface has been based on ideas from:

Which are both good references for "visiting".

An example of using Accept():

XMLPrinter printer;
tinyxmlDoc.Accept( &printer );
const char* xmlcstr = printer.CStr();

const char *Attribute(const char *name, const char *value = 0) const

Given an attribute name, Attribute() returns the value for the attribute of that name, or null if none exists. For example:

const char* value = ele->Attribute( "foo" );

The 'value' parameter is normally null. However, if specified, the attribute will only be returned if the 'name' and 'value' match. This allow you to write code:

if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();

rather than:

if ( ele->Attribute( "foo" ) ) {
    if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
}

int IntAttribute(const char *name, int defaultValue = 0) const

Given an attribute name, IntAttribute() returns the value of the attribute interpreted as an integer. The default value will be returned if the attribute isn't present, or if there is an error. (For a method with error checking, see QueryIntAttribute()).

unsigned UnsignedAttribute(const char *name, unsigned defaultValue = 0) const

See IntAttribute()

int64_t Int64Attribute(const char *name, int64_t defaultValue = 0) const

See IntAttribute()

uint64_t Unsigned64Attribute(const char *name, uint64_t defaultValue = 0) const

See IntAttribute()

bool BoolAttribute(const char *name, bool defaultValue = false) const

See IntAttribute()

double DoubleAttribute(const char *name, double defaultValue = 0) const

See IntAttribute()

float FloatAttribute(const char *name, float defaultValue = 0) const

See IntAttribute()

inline XMLError QueryIntAttribute(const char *name, int *value) const

Given an attribute name, QueryIntAttribute() returns XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion can't be performed, or XML_NO_ATTRIBUTE if the attribute doesn't exist. If successful, the result of the conversion will be written to 'value'. If not successful, nothing will be written to 'value'. This allows you to provide default value:

int value = 10;
QueryIntAttribute( "foo", &value );     // if "foo" isn't found, value will still be 10
inline XMLError QueryUnsignedAttribute(const char *name, unsigned int *value) const

See QueryIntAttribute()

inline XMLError QueryInt64Attribute(const char *name, int64_t *value) const

See QueryIntAttribute()

inline XMLError QueryUnsigned64Attribute(const char *name, uint64_t *value) const

See QueryIntAttribute()

inline XMLError QueryBoolAttribute(const char *name, bool *value) const

See QueryIntAttribute()

inline XMLError QueryDoubleAttribute(const char *name, double *value) const

See QueryIntAttribute()

inline XMLError QueryFloatAttribute(const char *name, float *value) const

See QueryIntAttribute()

inline XMLError QueryStringAttribute(const char *name, const char **value) const

See QueryIntAttribute()

inline XMLError QueryAttribute(const char *name, int *value) const

Given an attribute name, QueryAttribute() returns XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion can't be performed, or XML_NO_ATTRIBUTE if the attribute doesn't exist. It is overloaded for the primitive types, and is a generally more convenient replacement of QueryIntAttribute() and related functions.

If successful, the result of the conversion will be written to 'value'. If not successful, nothing will be written to 'value'. This allows you to provide default value:

int value = 10;
QueryAttribute( "foo", &value );        // if "foo" isn't found, value will still be 10
inline XMLError QueryAttribute(const char *name, unsigned int *value) const
inline XMLError QueryAttribute(const char *name, int64_t *value) const
inline XMLError QueryAttribute(const char *name, uint64_t *value) const
inline XMLError QueryAttribute(const char *name, bool *value) const
inline XMLError QueryAttribute(const char *name, double *value) const
inline XMLError QueryAttribute(const char *name, float *value) const
inline XMLError QueryAttribute(const char *name, const char **value) const
inline void SetAttribute(const char *name, const char *value)

Sets the named attribute to value.

inline void SetAttribute(const char *name, int value)

Sets the named attribute to value.

inline void SetAttribute(const char *name, unsigned value)

Sets the named attribute to value.

inline void SetAttribute(const char *name, int64_t value)

Sets the named attribute to value.

inline void SetAttribute(const char *name, uint64_t value)

Sets the named attribute to value.

inline void SetAttribute(const char *name, bool value)

Sets the named attribute to value.

inline void SetAttribute(const char *name, double value)

Sets the named attribute to value.

inline void SetAttribute(const char *name, float value)

Sets the named attribute to value.

void DeleteAttribute(const char *name)

Delete an attribute.

inline const XMLAttribute *FirstAttribute() const

Return the first attribute in the list.

const XMLAttribute *FindAttribute(const char *name) const

Query a specific attribute in the list.

const char *GetText() const

Convenience function for easy access to the text inside an element. Although easy and concise, GetText() is limited compared to getting the XMLText child and accessing it directly.

If the first child of 'this' is a XMLText, the GetText() returns the character string of the Text node, else null is returned.

This is a convenient method for getting the text of simple contained text:

<foo>This is text</foo>
    const char* str = fooElement->GetText();

'str' will be a pointer to "This is text".

Note that this function can be misleading. If the element foo was created from this XML:

    <foo><b>This is text</b></foo>

then the value of str would be null. The first child node isn't a text node, it is another element. From this XML:

    <foo>This is <b>text</b></foo>
GetText() will return "This is ".

void SetText(const char *inText)

Convenience function for easy access to the text inside an element. Although easy and concise, SetText() is limited compared to creating an XMLText child and mutating it directly.

If the first child of 'this' is a XMLText, SetText() sets its value to the given string, otherwise it will create a first child that is an XMLText.

This is a convenient method for setting the text of simple contained text:

<foo>This is text</foo>
    fooElement->SetText( "Hullaballoo!" );
<foo>Hullaballoo!</foo>

Note that this function can be misleading. If the element foo was created from this XML:

    <foo><b>This is text</b></foo>

then it will not change "This is text", but rather prefix it with a text element:

    <foo>Hullaballoo!<b>This is text</b></foo>

For this XML:

    <foo />
SetText() will generate
    <foo>Hullaballoo!</foo>

void SetText(int value)

Convenience method for setting text inside an element. See SetText() for important limitations.

void SetText(unsigned value)

Convenience method for setting text inside an element. See SetText() for important limitations.

void SetText(int64_t value)

Convenience method for setting text inside an element. See SetText() for important limitations.

void SetText(uint64_t value)

Convenience method for setting text inside an element. See SetText() for important limitations.

void SetText(bool value)

Convenience method for setting text inside an element. See SetText() for important limitations.

void SetText(double value)

Convenience method for setting text inside an element. See SetText() for important limitations.

void SetText(float value)

Convenience method for setting text inside an element. See SetText() for important limitations.

XMLError QueryIntText(int *ival) const

Convenience method to query the value of a child text node. This is probably best shown by example. Given you have a document is this form:

    <point>
        <x>1</x>
        <y>1.4</y>
    </point>

The QueryIntText() and similar functions provide a safe and easier way to get to the "value" of x and y.

    int x = 0;
    float y = 0;    // types of x and y are contrived for example
    const XMLElement* xElement = pointElement->FirstChildElement( "x" );
    const XMLElement* yElement = pointElement->FirstChildElement( "y" );
    xElement->QueryIntText( &x );
    yElement->QueryFloatText( &y );
Returns

XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted to the requested type, and XML_NO_TEXT_NODE if there is no child text to query.

XMLError QueryUnsignedText(unsigned *uval) const

See QueryIntText()

XMLError QueryInt64Text(int64_t *uval) const

See QueryIntText()

XMLError QueryUnsigned64Text(uint64_t *uval) const

See QueryIntText()

XMLError QueryBoolText(bool *bval) const

See QueryIntText()

XMLError QueryDoubleText(double *dval) const

See QueryIntText()

XMLError QueryFloatText(float *fval) const

See QueryIntText()

int IntText(int defaultValue = 0) const
unsigned UnsignedText(unsigned defaultValue = 0) const

See QueryIntText()

int64_t Int64Text(int64_t defaultValue = 0) const

See QueryIntText()

uint64_t Unsigned64Text(uint64_t defaultValue = 0) const

See QueryIntText()

bool BoolText(bool defaultValue = false) const

See QueryIntText()

double DoubleText(double defaultValue = 0) const

See QueryIntText()

float FloatText(float defaultValue = 0) const

See QueryIntText()

XMLElement *InsertNewChildElement(const char *name)

Convenience method to create a new XMLElement and add it as last (right) child of this node. Returns the created and inserted element.

XMLComment *InsertNewComment(const char *comment)

See InsertNewChildElement()

XMLText *InsertNewText(const char *text)

See InsertNewChildElement()

XMLDeclaration *InsertNewDeclaration(const char *text)

See InsertNewChildElement()

XMLUnknown *InsertNewUnknown(const char *text)

See InsertNewChildElement()

inline ElementClosingType ClosingType() const
virtual XMLNode *ShallowClone(XMLDocument *document) const override

Make a copy of this node, but not its children. You may pass in a Document pointer that will be the owner of the new Node. If the 'document' is null, then the node returned will be allocated from the current Document. (this->GetDocument())

Note: if called on a XMLDocument, this will return null.

virtual bool ShallowEqual(const XMLNode *compare) const override

Test if 2 nodes are the same, but don't test children. The 2 nodes do not need to be in the same Document.

Note: if called on a XMLDocument, this will return false.

Protected Functions

virtual char *ParseDeep(char *p, StrPair *parentEndTag, int *curLineNumPtr) override