tinyxml insert block of xml (c++)

I know how to build an xml document with tinyxml such as

TiXmlElement root=TiXmlElement("root");
TiXmlElement *new_element = new TiXmlElement("element");
new_element->LinkEndChild(new TiXmlText("test"));
root.LinkEndChild(new_element);

but what I do not know how to do is:
if I already the text <element>another element here</element>
build a new document so that this text is inserted into the root

I thought I could do something like this, but it errors out
TiXmlDocument sub_element; sub_element.Parse(xml_string);
root.LinkEndChild(sub_element.ToElement());
PhilCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
If I got you right, you want to insert the element *before* "<element>another element here</element>"? In that case, try
TiXmlElement* root=TiXmlElement("root");
TiXmlElement* first = root->FirstChildElement();

TiXmlElement *new_element = new TiXmlElement("element");

root->InsertBeforeChild( first, *new_element);

Open in new window

0
PhilCAuthor Commented:
I have a text buffer that is already XML, I am trying to insert it into the root element (linking at the end is fine).  But I cannot just LinkEndChild() with a new text element or the tags are escaped < = &lt;, etc.
I am trying to insert a block of already formatted xml into another xml buffer that is getting built.
thank you
0
jkrCommented:
>>I have a text buffer that is already XML

Hmm, that's a more difficult setup, since inserting that would require you to convert that into a TinyXMLElement object tree first, you cannot just insert plain text and expect TinyXML to parse it on the fly. If they had support for std::iostreams, that'd be a piece of cakke, but since they don't, I am afraid that writing that buffer to a temp. file and opening this file as a separate TinyXMLDocument will be the only way to go...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

PhilCAuthor Commented:
so there is no way to parse it into a new document and then add the root node tree to another object tree?
thank you
0
PhilCAuthor Commented:
think I have it now, I was using the wrong function since LinkEndChild() takes possession of the pointer,
If I change my above code to the following it looks like it works as I expected
root.InsertEndChild(*sub_element.RootElement());
instead of
root.LinkEndChild(sub_element.ToElement());
0
jkrCommented:
It it is plain XML text, I'd go the way described above - that is, unless you wanted the whole block of XML plain text to be inserted as a child element, which I would not recommend (just think of quoting etc.)
0
PhilCAuthor Commented:
no, I do not want the xml as a child element, I just need to merge a few element trees into a single tree.
thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.