Results 1 to 2 of 2

Thread: QDomDocument::setContent reead only 1 chield but in file is more chield

  1. #1
    Join Date
    Apr 2012
    Posts
    40
    Platforms
    Unix/X11 Windows

    Default QDomDocument::setContent reead only 1 chield but in file is more chield

    Hi i make QDomDocument and add 2 root chields (Accouts and Tests) next i save to file. Now if i want read this form file, QDomDocument read only 1 root chield (Accounts), Why ??

    //Code
    Qt Code:
    1. #include <QtCore>
    2. #include <QtXml>
    3.  
    4. void save()
    5. {
    6. QDomDocument XMLDocument;
    7.  
    8. QDomElement Accounts = XMLDocument.createElement("Accounts");
    9. XMLDocument.appendChild(Accounts);
    10.  
    11. QDomElement Account1;
    12. Account1 = XMLDocument.createElement("Account");
    13. Account1.setAttribute("Name", "Account1");
    14. Accounts.appendChild(Account1);
    15.  
    16. QDomElement Account2;
    17. Account2 = XMLDocument.createElement("Account");
    18. Account2.setAttribute("Name", "Account2");
    19. Accounts.appendChild(Account2);
    20.  
    21.  
    22. QDomElement Tests = XMLDocument.createElement("Tests");
    23. XMLDocument.appendChild(Tests);
    24.  
    25. QDomElement Test1;
    26. Test1 = XMLDocument.createElement("Test");
    27. Test1.setAttribute("Name", "Test1");
    28. Tests.appendChild(Test1);
    29.  
    30. QDomElement Test2;
    31. Test2 = XMLDocument.createElement("Test");
    32. Test2.setAttribute("Name", "Test2");
    33. Tests.appendChild(Test2);
    34.  
    35.  
    36. QFile XMLFile("d:/test.xml");
    37.  
    38. if( XMLFile.open(QIODevice::ReadWrite | QIODevice::Text) )
    39. {
    40. QTextStream tStream(&XMLFile);
    41. tStream << XMLDocument;
    42. }
    43.  
    44. XMLFile.flush();
    45. XMLFile.close();
    46.  
    47. }
    48.  
    49.  
    50. void load()
    51. {
    52. QFile XMLFile("d:/test.xml");
    53. QDomDocument XMLDocument;
    54.  
    55. if(XMLFile.open(QIODevice::ReadWrite | QIODevice::Text))
    56. {
    57. XMLDocument.setContent(&XMLFile);
    58. XMLFile.close();
    59. }
    60.  
    61. qDebug() << XMLDocument.childNodes().count();
    62.  
    63. }
    64.  
    65.  
    66. int main()
    67. {
    68. save();
    69. load();
    70.  
    71. }
    To copy to clipboard, switch view to plain text mode 

    //XMLFile
    Qt Code:
    1. <Accounts>
    2. <Account Name="Account1"/>
    3. <Account Name="Account2"/>
    4. </Accounts>
    5. <Tests>
    6. <Test Name="Test1"/>
    7. <Test Name="Test2"/>
    8. </Tests>
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: QDomDocument::setContent reead only 1 chield but in file is more chield

    You document is missing the outer element, i.e. the root node.

    So the parser assumes that Accounts is the root node and stops parsing when it reaches its end.

    In other words: there is no such thing as two top level nodes

    Cheers,
    _

Similar Threads

  1. DOM setContent first or last?
    By CoderMan in forum Qt Programming
    Replies: 2
    Last Post: 13th December 2011, 09:55
  2. QDomDocument can't read the file content
    By baluk in forum Newbie
    Replies: 21
    Last Post: 24th September 2010, 13:43
  3. Replies: 6
    Last Post: 6th September 2010, 13:38
  4. Strange error: doc.setContent(data) returns false
    By jiveaxe in forum Qt Programming
    Replies: 3
    Last Post: 9th January 2009, 01:38
  5. QDomDocument Speed by 24MB file
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 30th April 2007, 21:35

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
  •  
Qt is a trademark of The Qt Company.