TinyXML:将文档保存为char*或string

foo*_*bar 14 c++ tinyxml

我正在尝试使用TinyXML从内存中读取和保存,而不是仅将文件读取并保存到磁盘.

似乎documnent的解析函数可以加载char*.但是当我完成它时,我需要将文档保存到char*.有谁知道这个?

编辑:打印和流媒体功能不是我想要的.它们以可视格式输出,我需要实际的xml内容.

编辑:打印很酷.

Ale*_*x B 22

这是我正在使用的一些示例代码,改编自TiXMLPrinter文档:

TiXmlDocument doc;
// populate document here ...

TiXmlPrinter printer;
printer.SetIndent( "    " );

doc.Accept( &printer );
std::string xmltext = printer.CStr();
Run Code Online (Sandbox Code Playgroud)


小智 13

TinyXml中一个简单而优雅的解决方案,用于将TiXmlDocument打印到std :: string.

我做了这个小例子

// Create a TiXmlDocument    
TiXmlDocument *pDoc =new TiXmlDocument("my_doc_name");

// Add some content to the document, you might fill in something else ;-)    
TiXmlComment*   comment = new TiXmlComment("hello world" );    
pDoc->LinkEndChild( comment );

// Declare a printer    
TiXmlPrinter printer;

// attach it to the document you want to convert in to a std::string 
pDoc->Accept(&printer);

// Create a std::string and copy your document data in to the string    
std::string str = printer.CStr();
Run Code Online (Sandbox Code Playgroud)


gno*_*bal 10

我不熟悉TinyXML,但从文档中可以看出,通过使用operator <<到C++流(因此可以使用C++字符串流)或TiXMLPrinter类,您可以在不使用文件的情况下获取STL字符串.请参阅TinyXML文档(查找"打印"部分)