How to convert an XMLElement to string in TinyXML2

喜夏-厌秋 提交于 2019-12-01 18:31:41

问题


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>

回答1:


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

XMLPrinter printer;
pxmlChild->Accept( &printer );
ss << printer.CStr();



回答2:


From the TinyXml2 community:

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

XMLPrinter printer;
pxmlChild->Print( &printer );
ss << printer.CStr();



回答3:


    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;


来源:https://stackoverflow.com/questions/11935689/how-to-convert-an-xmlelement-to-string-in-tinyxml2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!