gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:08:53 24 4
gpt4 key购买 nike

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

所以在输入中我有带有完整 xml 树的 xmlDocPtr 并且想要获取包含 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);
}

关于c++ - 使用 libxml2 (c++) 创建字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4155730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com