通过TinyXML深度复制XML

通过TinyXML深度复制XML

本文介绍了通过TinyXML深度复制XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我使用tinyxml。



如何复制或创建现有XMLDocument的副本? b



我通过这个链接说过使用克隆复制节点。但是这是受保护的,我不想去推导出这样的类和类似的。



我也不想保存现有的XMLDocument到文件然后让另一个XMLDocument对象读取该文件以具有它的副本。



我也不能使用 memcpy 因为我不知道整个XML的大小。



我也不想让两个对象一个接一个使用,如:

  XMLDocumentObj1 = add_some_data 

XMLDocumentObj2 = add_the_same_data,等等

我想要第二个副本的主要原因是,第一个副本可能被代码的不同部分修改,而同一个副本在多个地方被读取 。我需要确保在读取XMLDocument时不会发生错误,因为有可能这可能已经在后台被运行的线程修改,我没有程序崩溃。

解决方案

我发现这一点,我认为这可能会对你有帮助。



  const char * pub =<?xml version ='1.0'?>< element> ;< sub />< / element><! -  comment  - ><!DOCTYPE>; 
XMLDocument doc;
doc.Parse(pub);

XMLDocument clone;
for(const XMLNode * node = doc.FirstChild(); node; node = node-> NextSibling()){
XMLNode * copy = node-> ShallowClone(& clone);
clone.InsertEndChild(copy);
}

clone.Print();

int count = 0;
const XMLNode * a = clone.FirstChild();
const XMLNode * b = doc.FirstChild();
for(; a& b; a = a-> NextSibling(),b = b-> NextSibling()){
++ count;
XMLTest(Clone and Equal,true,a-> ShallowEqual(b));
}
XMLTest(Clone and Equal,4,count);


I am using tinyxml.

How do I duplicate or create a copy of an existing XMLDocument?

http://www.grinninglizard.com/tinyxmldocs/classTiXmlDocument.html#a4e8c1498a76dcde7191c683e1220882

I went through this link that says using Clone to replicate a node. But this is protected and I do not want to go for deriving a class out of this and the like.

I also do not want to save the existing XMLDocument to a file and then making another XMLDocument object read the file to have a copy of it.

I am also not able to perform a deep copy using memcpy because I am unaware of the size of the entire XML.

I also do not want to having two objects being used one after the other like:

XMLDocumentObj1 = add_some_data

XMLDocumentObj2 = add_the_same_data, and so on

The primary reason I want a second copy is that, the first might be modified by different sections of the code, while the same copy is being 'read' at multiple places. I need to ensure that there occur no errors when XMLDocument is read, because there are chances that this might have been modified in the background by a running thread, and I get no program crashes.

解决方案

I found this and i think this may help you.

https://github.com/leethomason/tinyxml2/blob/master/xmltest.cpp

const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
XMLDocument doc;
doc.Parse( pub );

XMLDocument clone;
for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
    XMLNode* copy = node->ShallowClone( &clone );
    clone.InsertEndChild( copy );
}

clone.Print();

int count=0;
const XMLNode* a=clone.FirstChild();
const XMLNode* b=doc.FirstChild();
for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
    ++count;
    XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
}
XMLTest( "Clone and Equal", 4, count );

这篇关于通过TinyXML深度复制XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 15:04