PDA

View Full Version : How do you combine many QDomDocumentFragments into a QDomDocument?



falconium
23rd March 2011, 20:42
I have the following code, but it doesn't seem to work. First of all, 'fragment' seems to be empty with debugger despite I call createDocumentFragment().
Below part for parsing XML document inside 'data' is to ensure that the source has data. All iterated item has.
Why fragment is empty? Thanks!


if (mergedXML.documentElement().isNull())
{
QDomElement root = mergedXML.createElement("MergedMeasurements");
mergedXML.appendChild(root);
}

foreach (XMLStruct data, mainXMLList)
{
QDomDocumentFragment fragment = data.getXMLDocument()->createDocumentFragment(); // This creates the fragment from QDomDocument.
mergedXML.firstChild().appendChild(fragment);
for (int i=0; i<data.getXMLDocument()->childNodes().count(); i++)
{
if (data.getXMLDocument()->childNodes().at(i).nodeName()=="OMeS")
for (int j=0; j<data.getXMLDocument()->childNodes().at(i).childNodes().count(); j++)
{
if (data.getXMLDocument()->childNodes().at(i).childNodes().at(j).nodeName()=="PMSetup")
{
for (int k=0; k<data.getXMLDocument()->childNodes().at(i).childNodes().at(j).childNodes() .count(); k++)
{
if (data.getXMLDocument()->childNodes().at(i).childNodes().at(j).childNodes() .at(k).nodeName()=="extraInfoSetup")
{
QDomNode x = data.getXMLDocument()->childNodes().at(i).childNodes().at(j).childNodes() .at(k);
log(x.firstChildElement("measurementName").text());
}
}
}
}
}

}

wysota
23rd March 2011, 22:20
What is this supposed to do? Because right now you are creating an empty document fragment and attaching this empty fragment somewhere.

falconium
23rd March 2011, 22:31
Hi wysota,

'data' is a class (XMLStruct) storing XML document. This XML data is stored in form of a QDomDocument variable, and this data is gathered by calling method getXMLDocument() of class. I want to combine all these XML docs by iterating the QDomDocuments of XMLStruct list into one QDomDocument (called mergedXML).

Thanks!

wysota
23rd March 2011, 22:46
So since you have the documents, what do you need the fragments for?


#include <QtXml>

int main(int argc, char **argv){
QDomDocument doc1;
doc1.setContent(QString("<doc1><tag1>text</tag1></doc1>"));
QDomDocument doc2;
doc2.setContent(QString("<doc2><tag2>text</tag2></doc2>"));
QDomDocument merged;
merged.setContent(QString("<merged><app1/><app2/></merged>"));
QDomElement app1 = merged.documentElement().firstChildElement("app1");
QDomElement app2 = merged.documentElement().firstChildElement("app2");
app1.appendChild(doc1.documentElement());
app2.appendChild(doc2.documentElement());
qDebug() << merged.toString();
return 0;
}

Result:
<merged>
<app1>
<doc1>
<tag1>text</tag1>
</doc1>
</app1>
<app2>
<doc2>
<tag2>text</tag2>
</doc2>
</app2>
</merged>

falconium
23rd March 2011, 23:10
Thanks for this simple example! I have learnt a lot. So instead of creating a fragment of documents, one only need to append the document elements of the children documents. I suppose I can also append lower level elements, not just roots.