Using libxml2. I can pretty-print the XML into a file by using xmlSaveFormatFileEnc()
. But is there a way to do the same thing into a text string or stream?
I'd like to avoid writing the XML out to a file and reading it back in just to get the pretty-print version of the XML.
For the record, what I'm doing now is the following:
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
Stolen from 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;
}
And then, after you have written to the buffer:
std::cout << buf->content << std::endl
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.