使用libxml2创建字符串(c ++)

les*_*hka 5 c++ libxml2

我的问题是我想创建xml树并获得一个简单的字符串对象(甚至是char*).我无法将xml保存到文件中.

所以在输入中我有xmlDocPtr和完整的xml树,并希望得到包含xml但不使用文件的字符串.

请注意.

Fre*_*Foo 10

使用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);
}
Run Code Online (Sandbox Code Playgroud)