[英]Creating string with libxml2 (c++)
我的問題是我想創建xml樹並獲得一個簡單的字符串對象(甚至是char *)。 我無法將xml保存到文件中。
所以在輸入中我有xmlDocPtr和完整的xml樹,並希望得到包含xml但不使用文件的字符串。
請注意。
使用xmlDocDumpMemory或其任何表兄弟。 用法:
void xml_to_string(xmlDocPtr doc, std::string &out)
{
xmlChar *s;
int size;
xmlDocDumpMemory(doc, &s, &size);
if (s == NULL)
throw std::bad_alloc();
try {
out = (char *)s;
} catch (...) {
xmlFree(s);
throw;
}
xmlFree(s);
}
我剛才寫的東西。 希望能幫助到你....
xmlDocPtr pDoc = ... // your xml docuemnt
xmlCharPtr psOutput;
int iSize;
xmlDocDumpFormatMemoryEnc(pDoc, &psOutput, &iSize, "UTF-8", 1);
// psOutput should point to the string.
// Don't forget to free the memory.
xmlFree(psOutput);
如果你使用xmlDocDumpMemory()要小心。 在使用xmlDocDumpMemory()之后,在最后調用xmlCleanupParser()。 似乎xmlDocDumpMemory()在內部使用它。
我無法得到你想要的東西但是在更改節點值之后你可以使用xmlDocDump 輕松保存它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.