문제

I have to rearrange nodes in XML file. For this i need to copy nodes from QDomDocument to another QDomDocument.

Original xml file have nodes in this order :--
0,1,2,3,4,5
Now i wan to arrange the nodes in this order :---
0,2,4,1,3,5

1> I am having a XML file which i am reading in an QDomDocument (this xml file have 6 nodes).
2> Now I am trying to copy QDomElement from one QDomDocument & putting them in another QDomDocumentUpdated.
3> Then i am copying the updated QDomDocumentUpdated back into xml file.
4> After copying the nodes there are only 3 nodes copied to updated XML document.

Problem I am facing is not all the nodes are copied into QDomDocumentUpdated. hence xml file contains less number of nodes than appended by me.

Please suggest how to correct it.

Now as we can see there are 6 nodes in the XML document. Original XML file :---

<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>

Source code file :----

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();

Output XML file :---

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

Please suggest what is missing ?

도움이 되었습니까?

해결책

I just tested your code to see what really happens. I used QString QDomDocument::toString ( int indent = 1 ) const to debug.

I did something like this:

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

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

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

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

and it appeared i have this output:

Lion

Lion
Bear

Lion
Bear

Lion
Bear
Lepord
(x3)

It seems when you do this: domNodeChild = listchildNodes.at(x);, you remove the element from this list.

Let's check the documentation:

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

Assigns a copy of n to this DOM node.

The data of the copy is shared (shallow copy): modifying one node will also change the other. If you want to make a deep copy, use cloneNode().

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top