Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将所有xml格式化后的xmlNodePtr转换为字符串?_C++_Xml_String_Libxml2 - Fatal编程技术网

C++ 如何将所有xml格式化后的xmlNodePtr转换为字符串?

C++ 如何将所有xml格式化后的xmlNodePtr转换为字符串?,c++,xml,string,libxml2,C++,Xml,String,Libxml2,下面是一个xmlNodePtr,然后我想将该节点转换为字符串,保留所有xml格式和内容: std::string toString() { std::string xmlString; xmlNodePtr noteKey = xmlNewNode(0, (xmlChar*)"noteKeyword"); std::vector<Note *>::iterator iter = notesList_.begin(); while(iter != notesList

下面是一个
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 to字符串()
{
std::string-xmlString;
xmlNodePtr noteKey=xmlNewNode(0,(xmlChar*)“noteKeyword”);
std::vector::iterator iter=notesList_uuz.begin();
while(iter!=notesList.end())
{
xmlNodePtr noteNode=xmlNewNode(0,(xmlChar*)“Note”);
xmlNodePtr userNode=xmlNewNode(0,(xmlChar*)“User”);
xmlNodePtr dateNode=xmlNewNode(0,(xmlChar*)“日期”);
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;
//这似乎不起作用,我需要在这里分配一些内存吗?
//还是做点别的?
xmlOutputBufferPtr输出;
xmlnodempoutput(输出,noteDoc,noteKey,0,1,“UTF-8”);
//以某种方式将输出转换为字符串?
返回xmlString;
}
我的问题是,节点看起来很好,但我不知道如何将节点转换为std::string。我还尝试过使用
xmlNodeListGetString
xmlDocDumpFormatMemory
,但这两种方法都无法正常工作。非常感谢您提供一个如何从节点转换为字符串的示例。

关键是添加:

  xmlChar *s;
  int size;

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

  xmlString = (char *)s;
  xmlFree(s);
请尝试以下示例:

xmlBufferPtr buf = xmlBufferCreate();

// Instead of Null, you can write notekey->doc
xmlNodeDump(buf, NULL, noteKey, 1,1);
printf("%s", (char*)buf->content);
里卡特·兰博的签名。

看起来像是《什么?