在一个函数中,我使用 pugi 首先加载一个 XML 文件。然后,我遍历树的子 xml 节点并将一些子 xml 节点(xml_node 类型的对象)推送到 xml_node 的向量。但是一旦我退出这个函数,从 XML 文件加载的原始 XML 树结构对象被删除,导致 xml 节点向量中的元素变得无效。
下面是一个示例代码(快速编写)来显示这一点:
#include "pugixml.hpp"
#include <vector>
void ProcessXmlDeferred( std::vector<pugi::xml_node> const &subTrees )
{
for( auto & const node: subTrees)
{
// parse each xml_node node
}
}
void IntermedProcXml( pugi::xml_node const &node)
{
// parse node
}
std::vector<pugi::xml_node> BuildSubTrees(pugi::xml_node const & node )
{
std::vector<pugi::xml_node> subTrees;
pugi::xml_node temp = node.child("L1");
subTrees.push_back( temp );
temp = node.child.child("L2");
subTrees.push_back( temp );
temp = node.child.child.child("L3");
subTrees.push_back( temp );
return subTrees;
}
void LoadAndProcessDoc( const char* fileNameWithPath, std::vector<pugi::xml_node> & subTrees )
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load( fileNameWithPath );
subTrees = BuildSubTrees( result.child("TOP") );
IntermedProcXml( result.child("CENTRE") );
// Local pugi objects("doc" and "result") destroyed at exit of this
// function invalidating contents of xml nodes inside vector "subTrees"
}
int main()
{
char fileName[] = "myFile.xml";
std::vector<pugi::xml_node> myXmlSubTrees;
// Load XML file and return vector of XML sub-tree's for later parsing
LoadAndProcessDoc( fileName, myXmlSubTrees );
// At this point, the contents of xml node's inside the vector
// "myXmlSubTrees" are no longer valid and thus are unsafe to use
// ....
// Lots of intermediate code
// ....
// This function tries to process vector whose xml nodes
// are invalid and thus throws errors
ProcessXmlDeferred( myXmlSubTrees );
return 0;
}
因此,我需要一种方法来保存/复制/克隆/移动原始 XML 树的子树(xml 节点),以便即使在删除原始 XML 根树对象之后,我也可以在以后安全地解析它们。如何在 pugi 中做到这一点?