使用libxml2。我可以使用xmlSaveFormatFileEnc()
将XML漂亮地打印到文件中。但是有没有办法在文本字符串或流中做同样的事情?
我想避免将XML写到文件中并读回它,只是为了获得XML的漂亮印刷版。
为便于记录,我现在正在做的事情如下:
xmlInitParser();
xmlKeepBlanksDefault(0);
xmlLineNumbersDefault(1);
xmlThrDefIndentTreeOutput(1);
xmlThrDefTreeIndentString(" ");
std::string content = "....."; // do something here to get the XML
xmlDoc * doc = xmlParseDoc((xmlChar*)content.c_str());
xmlSaveFormatFileEnc("output.xml", doc, "utf-8", 1); // pretty print
答案 0 :(得分:0)
从here被盗:
xmlBufferPtr buf;
/* Create a new XML buffer, to which the XML document will be
* written */
buf = xmlBufferCreate();
if (buf == NULL) {
std::cerr << "testXmlwriterMemory: Error creating the xml buffer" << std::endl;
return;
}
/* Create a new XmlWriter for memory, with no compression.
* Remark: there is no compression for this kind of xmlTextWriter */
writer = xmlNewTextWriterMemory(buf, 0);
if (writer == NULL) {
std::cerr << "testXmlwriterMemory: Error creating the xml writer" << std::endl;
return;
}
然后,在写入缓冲区后:
std::cout << buf->content << std::endl