5

In TinyXml 1 it was possible to convert a child element to a string using the << operator, e.g.

TiXmlElement * pxmlChild = pxmlParent->FirstChildElement( "child" );
std::stringstream ss;
ss << (*pxmlChild);

This doesn't appear possible in TinyXml2. How do you convert an element to an xml string in TinyXml2?

Edit: Specifically I'm after the xml, e.g. if the xml was:

<parent>
    <child>
        <value>abc</value>
    </child>
<parent>

I want the xml for the child element, e.g.

<child>
    <value>abc</value>
</child>
manlio
  • 18,345
  • 14
  • 76
  • 126
snowdude
  • 3,854
  • 1
  • 18
  • 27

3 Answers3

9

Seems like Print isn't there any more but Accept works just as well:

XMLPrinter printer;
pxmlChild->Accept( &printer );
ss << printer.CStr();
Adam Thomson
  • 91
  • 1
  • 2
3

From the TinyXml2 community:

Printing (of a sub-node) is in a utility function:

XMLPrinter printer;
pxmlChild->Print( &printer );
ss << printer.CStr();
snowdude
  • 3,854
  • 1
  • 18
  • 27
  • 2
    It appears that this solution does not exist anymore- no idea why. See my question: http://stackoverflow.com/questions/27726049/extracting-sub-tree-xml-string-with-tinyxml2 – Jim Clay Jan 02 '15 at 18:04
0
    TiXmlElement *assertion; // you can add some elements when you test
    TiXmlPrinter printer;
    assertion->Accept( &printer );
    std::string stringBuffer = printer.CStr();
    cout<<stringBuffer.c_str()<<endl;