• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1043
  • Last Modified:

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());
0
PhilC
Asked:
PhilC
  • 4
  • 3
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now