2012-01-11 13 views
2

は私がxmlNodePtrを行い、以下を持っているし、私は、文字列の中に、そのノードを変換したい:xmlNodePtrをすべてのXML形式で文字列に変換するにはどうすればよいですか? 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を使ってみましたが、どちらも動作できませんでした。ノードから文字列に変換する方法の例は、ありがたくありがとう、ありがとう。

+0

は、http://stackoverflow.com/questions/8232094/libxml-xmlnodeptr-to-raw-xml-stringの複製と似ていますか? – timB33

答えて

1

キーが追加された:

xmlChar *s; 
    int size; 

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

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

は、この例で試してみてください。Ricart Yランボーによって

xmlBufferPtr buf = xmlBufferCreate(); 

// Instead of Null, you can write notekey->doc 
xmlNodeDump(buf, NULL, noteKey, 1,1); 
printf("%s", (char*)buf->content); 

サイン。

関連する問題