0

我有一个加载pugi::xml_document例如<node></node>并想将 xml 文本结构添加到这个 pugi xml 文档!

xml 文本结构示例:(存储在 std::string 中)

<cmd name="Test"><tag>some text</tag></cmd>

最终的 xml 文档应如下所示:

<node><cmd name="Test"><tag>some text</tag></cmd></node>

在 pugixml 中执行此操作的最佳方法是什么?

谢谢!

4

1 回答 1

0

加载 doc ( <node></node>) 的一些函数:

bool Class::ReadXmlString(std::string xml)
{
    try
    {               
        pugi::xml_parse_result parseResult = m_xmlDoc->load(xml.c_str());    
        return parseResult;         
    }
    catch(std::exception &exp) 
    {       
        return false; 
    }
}

要添加的功能,例如:<cmd name="Test"><tag>some text</tag></cmd>

bool Class::AddFragment(std::string node, std::string xmlValue)
{
    try
    {
        //  temporary document to parse the data from a string
        pugi::xml_document doc;
        if (!doc.load_buffer(xmlValue.c_str(), xmlValue.length())) return false;

        // select node from class member pugi::xml_document
        pugi::xml_node xmlNode = m_xmlDoc->select_single_node(("//" + node).c_str()).node();

        for (pugi::xml_node child = doc.first_child(); child; child = child.next_sibling())
        {
            xmlNode.append_copy(child);
        }
    }
    catch(std::exception &exp)
    {       
        return false;
    }
}
于 2014-05-22T11:58:16.413 回答