Reputation: 1558
In my current project I have to modify several XML documents using tinyXML.
I didn't find a function SetText(const char*). I know that instead you have to create the TiXmlText and link it to the Element:
TiXmlElement* pParent = ...;
TiXmlText* pText = new TiXmlText(szText);
pParent->LinkEndChild(pText);
However, if the node already has a Text child, according to my understanding I have to modify its value instead.
I also did not find something like FirstChildText() or GetTextNode() etc. I guess using this line
TiXmlText* pText = pParent->FirstChild()->ToText();
will cause problems if pParent already has other children than text (in my case an attribute, comment - I can ignore elements/mixed mode), so I ended up in iterating over all children checking their Type() to be TINYXML_TEXT.
Is there a better way to do so or maybe an existing set of helper functions (including setText) I didn't find yet?
Upvotes: 0
Views: 168
Reputation: 580
SetText() is supported in TinyXML-2, but not TinyXML-1. It has no "intelligence" and assumes the FirstChild is a text node. If you know that there is no child node, or only a child text node, the almost-equivalent to SetText() is:
if (pParent->FirstChild()) {
pParent->RemoveChild(pParent->FirstChild());
}
pParent->LinkEndChild( new TiXmlText( "foo" ));
If you are selectively replacing text, for instance skipping the comment you mention, then you do have to iterate. There is no built in functionality for that. But you can change the text when you find it. You don't need to create a new text node to change the text:
pText->SetValue("this is my new text");
Upvotes: 0