Results 1 to 5 of 5

Thread: How do you combine many QDomDocumentFragments into a QDomDocument?

  1. #1
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How do you combine many QDomDocumentFragments into a QDomDocument?

    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!

    Qt Code:
    1. if (mergedXML.documentElement().isNull())
    2. {
    3. QDomElement root = mergedXML.createElement("MergedMeasurements");
    4. mergedXML.appendChild(root);
    5. }
    6.  
    7. foreach (XMLStruct data, mainXMLList)
    8. {
    9. QDomDocumentFragment fragment = data.getXMLDocument()->createDocumentFragment(); // This creates the fragment from QDomDocument.
    10. mergedXML.firstChild().appendChild(fragment);
    11. for (int i=0; i<data.getXMLDocument()->childNodes().count(); i++)
    12. {
    13. if (data.getXMLDocument()->childNodes().at(i).nodeName()=="OMeS")
    14. for (int j=0; j<data.getXMLDocument()->childNodes().at(i).childNodes().count(); j++)
    15. {
    16. if (data.getXMLDocument()->childNodes().at(i).childNodes().at(j).nodeName()=="PMSetup")
    17. {
    18. for (int k=0; k<data.getXMLDocument()->childNodes().at(i).childNodes().at(j).childNodes().count(); k++)
    19. {
    20. if (data.getXMLDocument()->childNodes().at(i).childNodes().at(j).childNodes().at(k).nodeName()=="extraInfoSetup")
    21. {
    22. QDomNode x = data.getXMLDocument()->childNodes().at(i).childNodes().at(j).childNodes().at(k);
    23. log(x.firstChildElement("measurementName").text());
    24. }
    25. }
    26. }
    27. }
    28. }
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How do you combine many QDomDocumentFragments into a QDomDocument?

    What is this supposed to do? Because right now you are creating an empty document fragment and attaching this empty fragment somewhere.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do you combine many QDomDocumentFragments into a QDomDocument?

    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!
    Last edited by falconium; 23rd March 2011 at 22:36.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How do you combine many QDomDocumentFragments into a QDomDocument?

    So since you have the documents, what do you need the fragments for?

    Qt Code:
    1. #include <QtXml>
    2.  
    3. int main(int argc, char **argv){
    4. doc1.setContent(QString("<doc1><tag1>text</tag1></doc1>"));
    5. doc2.setContent(QString("<doc2><tag2>text</tag2></doc2>"));
    6. QDomDocument merged;
    7. merged.setContent(QString("<merged><app1/><app2/></merged>"));
    8. QDomElement app1 = merged.documentElement().firstChildElement("app1");
    9. QDomElement app2 = merged.documentElement().firstChildElement("app2");
    10. app1.appendChild(doc1.documentElement());
    11. app2.appendChild(doc2.documentElement());
    12. qDebug() << merged.toString();
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Result:
    xml Code:
    1. <merged>
    2. <app1>
    3. <doc1>
    4. <tag1>text</tag1>
    5. </doc1>
    6. </app1>
    7. <app2>
    8. <doc2>
    9. <tag2>text</tag2>
    10. </doc2>
    11. </app2>
    12. </merged>
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    falconium (23rd March 2011)

  6. #5
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do you combine many QDomDocumentFragments into a QDomDocument?

    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.
    Last edited by falconium; 23rd March 2011 at 23:14.

Similar Threads

  1. combine Symbian C++ and Qt
    By Manjula in forum Newbie
    Replies: 0
    Last Post: 9th March 2011, 14:53
  2. Qt Assistant how to combine QtAssistant with Eclipse?
    By silence in forum Qt Tools
    Replies: 1
    Last Post: 14th April 2010, 12:25
  3. Split QDomDocument to new QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 28th January 2009, 09:59
  4. Combine C++ Qt with PyQT
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 7th January 2009, 16:14
  5. QDomDocument inside other QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2008, 15:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.