簡體   English   中英

用 libxml2 打印漂亮嗎?

[英]Pretty print with libxml2?

使用 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

這里被盜:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM