gpt4 book ai didi

c++ - 如何将所有 xml 格式的 xmlNodePtr 转换为字符串?

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

我有以下内容,它生成一个 xmlNodePtr 然后我想将该节点转换为一个字符串,同时保留所有 xml 格式和内容:

std::string toString()
{
std::string xmlString;

xmlNodePtr noteKey = xmlNewNode(0, (xmlChar*)"noteKeyword");

std::vector<Note *>::iterator iter = notesList_.begin();
while(iter != notesList_.end())
{
xmlNodePtr noteNode = xmlNewNode(0, (xmlChar*)"Note");

xmlNodePtr userNode = xmlNewNode(0, (xmlChar*)"User");
xmlNodePtr dateNode = xmlNewNode(0, (xmlChar*)"Date");
xmlNodePtr commentNode = xmlNewNode(0, (xmlChar*)"Comment");

xmlNodeSetContent(userNode, (xmlChar*)(*iter)->getUser().c_str());
xmlNodeSetContent(dateNode, (xmlChar*)(*iter)->getDate().c_str());
xmlNodeSetContent(commentNode, (xmlChar*)(*iter)->getComment().c_str());

xmlAddChild(noteNode, userNode);
xmlAddChild(noteNode, dateNode);
xmlAddChild(noteNode, commentNode);

xmlAddChild(noteKey, noteNode);

iter++;
}

xmlDocPtr noteDoc = noteKey->doc;

//this doesn't appear to work, do i need to allocate some memory here?
//or do something else?
xmlOutputBufferPtr output;
xmlNodeDumpOutput(output, noteDoc, noteKey, 0, 1, "UTF-8");

//somehow convert output to a string?
return xmlString;
}

我的问题是该节点似乎可以正常运行,但我不知道如何将节点转换为 std::string。我也尝试过使用 xmlNodeListGetStringxmlDocDumpFormatMemory 但我无法让它们中的任何一个工作。非常感谢如何从节点转换为字符串的示例,谢谢。

最佳答案

关键是添加:

  xmlChar *s;
int size;

xmlDocDumpMemory((xmlDocPtr)noteKey, &s, &size);

xmlString = (char *)s;
xmlFree(s);

关于c++ - 如何将所有 xml 格式的 xmlNodePtr 转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8826032/

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