Function GetText


#include <tinyxml2.h>

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 ".

Mentioned in


Lines 1638-1644 in tinyxml2.cpp. Line 1547 in tinyxml2.h.

const char* XMLElement::GetText() const
    if ( FirstChild() && FirstChild()->ToText() ) {
        return FirstChild()->Value();
    return 0;

Add Discussion as Guest

Log in