I've got a string that looks like:
"<xml /> <more_xml />".
I want to insert that inside a TiXmlElement. How can I do that? I can't figure out a way to correctly parse it, as there's two (or more) root elements in the string.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Aha, I can create a TiXmlText object and insert that into the element. However, quoting then becomes an issue, because all the '<' and '&' and '>' characters in the string are converted to '<', etc.
Any ideas?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
TinyXML errs towards permissiveness - including allowing more than one root element. If taking advantage of a loophole in TinyXML bothers you (and maybe it should in case we tighten it down in the future) you could wrap the elements you want to parse in a root elment by prepending a <root> and postpending a </root>.
lee
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Oh.... I see now. I didn't know that a TiXlDocument could have multiple children. I thought it was merely tolerant (i.e. ignores) any additional root elements in the document.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I've got a string that looks like:
"<xml /> <more_xml />".
I want to insert that inside a TiXmlElement. How can I do that? I can't figure out a way to correctly parse it, as there's two (or more) root elements in the string.
So, if I had an existing document that looked like this:
<xml>
<element>
</element>
</xml>
I'd want to insert the string so the document looks like:
<xml>
<element>
<xml /> <more_xml />
</element>
</xml>
Aha, I can create a TiXmlText object and insert that into the element. However, quoting then becomes an issue, because all the '<' and '&' and '>' characters in the string are converted to '<', etc.
Any ideas?
Assuming you have a TiXmlElement* to <element>, you would want to do something like:
element->LinkEndChild( new TiXmlElement( "xml" ));
element->LinkEndChild( new TiXmlElement( "more_xml" ));
...because you want to insert new *elements*, not new text. If you are generating those elements from text, you can always:
TiXmlElement* fooElement = new TiXmlElement( "foo" );
foo->Parse( "<more_xml/>";
element->LinkEndChild( fooElement );
Noting that "xml" is a reserved name, and you shouldn't use it.
hope that helps!
lee
Thanks for the response.
The problem is that I have a string that contains multiple xml elements. They're not broken up into separate strings, so I can't do:
std::string the_input_string = "<an_element /> <another_element />";
foo->Parse(the_input_string);
right?
I know my life would be a lot easier if I had separate strings for each element.
What I'd really like is some function that would return a vector of TiXmlElements given a string that contains multiple root elements.
Then I could insert each one of those elements into the root element.
Actually, if you used:
TiXmlDocument xml;
xml.Parse( "<an_element /> <another_element />" );
You could then add the child elements of 'xml' to another XmlDocument, which I think gets you the behavior you want.
A vector if TiXmlElements isn't going to happen - it's a little too specialized. :)
lee
I thought a document could only have one root element (and there's two in that string).
TinyXML errs towards permissiveness - including allowing more than one root element. If taking advantage of a loophole in TinyXML bothers you (and maybe it should in case we tighten it down in the future) you could wrap the elements you want to parse in a root elment by prepending a <root> and postpending a </root>.
lee
Oh.... I see now. I didn't know that a TiXlDocument could have multiple children. I thought it was merely tolerant (i.e. ignores) any additional root elements in the document.