1

我必须重新排列 XML 文件中的节点。为此,我需要将节点从复制QDomDocument到另一个QDomDocument

原始 xml 文件的节点按以下顺序排列:--
0,1,2,3,4,5
现在我要按此顺序排列节点:---
0,2,4,1,3,5

1> 我有一个正在读取的 XML 文件QDomDocument(这个 xml 文件有 6 个节点)。
2> 现在我正在尝试QDomElement从一个复制QDomDocument并将它们放入另一个 QDomDocumentUpdated。
3> 然后我将更新的内容复制QDomDocumentUpdated回 xml 文件。
4> 复制节点后,只有 3 个节点复制到更新的 XML 文档。

我面临的问题不是所有节点都被复制到QDomDocumentUpdated. 因此 xml 文件包含的节点数量少于我附加的数量。

请建议如何纠正它。

现在我们可以看到 XML 文档中有 6 个节点。原始 XML 文件:---

<Animals>
 <Type>
  <Name>Lion</Name>
 </Type>
 <Type>
  <Name>Tiger</Name>
 </Type>
 <Type>
  <Name>Lepord</Name>
 </Type>
 <Type>
  <Name>Bear</Name>
 </Type>
 <Type>
  <Name>Wolf</Name>
 </Type>
 <Type>
  <Name>Camel</Name>
 </Type>
</Animals>

源代码文件:----

QFile   xmlFile;
QTextStream xmlStream;
// xml dom document object
QDomDocument xmlDomDocument;
// xml dom document object
QDomDocument xmlDomDocumentSorted;
QTextStream xmlStream;
QDomNodeList listchildNodes;
QDomNode domNodeChild;
QDomElement xmlRoot;
// xml root for sorted xml
QDomElement xmlRootSorted;


//set the name of the file
xmlFile.setFileName("animals.xml");
xmlFile.open(QIODevice::ReadWrite|QIODevice::Text);
// Assign file to the stream
xmlStream.setDevice(&xmlFile);

xmlDomDocument.clear();
xmlDomDocument.setContent(&xmlFile);
// Make the root element
xmlRoot = xmlDomDocument.documentElement();
listchildNodes = xmlRoot.childNodes();


/****  Prepare Sorted list ****/
xmlDomDocumentSorted.clear();
// Make the root element
xmlRootSorted = xmlDomDocumentSorted.createElement("Animals");
// Add it to the document
xmlDomDocumentSorted.appendChild(xmlRootSorted);
// Append childs from original document
domNodeChild = listchildNodes.at(0);
xmlRootSorted.appendChild(domNodeChild);
domNodeChild = listchildNodes.at(2);
xmlRootSorted.appendChild(domNodeChild);
domNodeChild = listchildNodes.at(4);
xmlRootSorted.appendChild(domNodeChild);
domNodeChild = listchildNodes.at(1);
xmlRootSorted.appendChild(domNodeChild);
domNodeChild = listchildNodes.at(3);
xmlRootSorted.appendChild(domNodeChild);
domNodeChild = listchildNodes.at(5);
xmlRootSorted.appendChild(domNodeChild);

// Write the updated QDomDocument to XML file
///close file no flush
xmlFile.close();
//set the name of the file
xmlFile.setFileName(xmlFileName);
// open read & write mode
xmlFile.open(QIODevice::ReadWrite|QIODevice::Truncate|QIODevice::Text);
// Assign file to the stream
xmlStream.setDevice(&xmlFile);

// Write xml to the file
(xmlStream) << xmlDomDocumentSorted.toString();

// close the file
xmlFile.flush();
xmlFile.close();

输出 XML 文件:---

<Animals>
 <Type>
  <Name>Lion</Name>
 </Type>
 <Type>
  <Name>Bear</Name>
 </Type>
 <Type>
  <Name>Lepord</Name>
 </Type>
</Animals>

请建议缺少什么?

4

1 回答 1

2

我刚刚测试了你的代码,看看到底发生了什么。我曾经QString QDomDocument::toString ( int indent = 1 ) const调试过。

我做了这样的事情:

domNodeChild = listchildNodes.at(0);
xmlRootSorted.appendChild(domNodeChild);

qDebug() << xmlDomDocumentSorted.toString(1);

domNodeChild = listchildNodes.at(2);
xmlRootSorted.appendChild(domNodeChild);

qDebug() << xmlDomDocumentSorted.toString(1);
...

看来我有这个输出:

Lion

Lion
Bear

Lion
Bear

Lion
Bear
Lepord
(x3)

似乎当你这样做时:domNodeChild = listchildNodes.at(x);,你从这个列表中删除了元素。

让我们检查一下文档:

QDomNode &QDomNode::operator=(const QDomNode &n)

将 n 的副本分配给此 DOM 节点。

副本的数据是共享的(浅拷贝):修改一个节点也会改变另一个节点。如果要进行深层复制,请使用 cloneNode()。

于 2014-08-01T06:52:09.257 回答