You can use the following function to get the text child. It returns an empty C-string or the text if it exists. All possible checks are being made before.
The reason that tinyxml doesn't have a "getText" method on TiXmlElement is this:
<element>This is <b>bold</b> and this is not.</element>
There is no simple text call - that I can think of - that can correctly return the text of "element", because it has 2 or 3 text strings in it, depending on your point of view. What would element.getText() return? "This is"? "This is and this is not?" And how do you get to the "bold"?
Actually, a good solution may be to enhance "TiXmlHandle" so that the Text() method returns a char* instead of a Text Element. Then you would have super convenient text access, without the strangeness of the case above.
lee
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
how do get the string "john: from the folowing xml?
<person>
<name>john</name>
</person>
the following code prints the tagname and not the contents.
TiXmlDocument doc;
doc.Parse("<person><name>john</name></person>");
TiXmlElement *root = doc.RootElement();
cout << root->Value() << endl;
TiXmlNode *name = root->FirstChild();
cout << name->Value() << endl;
doing name->ToText() instead returns null
any help would be appreciated.
Did you read the documentation? The bit about "How TinyXml works" should show you the node type you're looking for.
i guess i didn't read it well enough. I didn't realize the text was a child. Why not have a function to get the (text)content of an element?
You can use the following function to get the text child. It returns an empty C-string or the text if it exists. All possible checks are being made before.
const char * GetTextChild (TiXmlElement * mother)
{
if (! mother || ! (mother -> FirstChild ()) || ! (mother -> FirstChild () -> ToText ()) || ! (mother -> FirstChild () -> ToText () -> Value ()))
return "";
return mother -> FirstChild () -> ToText () -> Value ();
}
The reason that tinyxml doesn't have a "getText" method on TiXmlElement is this:
<element>This is <b>bold</b> and this is not.</element>
There is no simple text call - that I can think of - that can correctly return the text of "element", because it has 2 or 3 text strings in it, depending on your point of view. What would element.getText() return? "This is"? "This is and this is not?" And how do you get to the "bold"?
Actually, a good solution may be to enhance "TiXmlHandle" so that the Text() method returns a char* instead of a Text Element. Then you would have super convenient text access, without the strangeness of the case above.
lee