Results 1 to 4 of 4

Thread: QtXML : read all the nodes of a xml file

  1. #1
    Join Date
    Sep 2016
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QtXML : read all the nodes of a xml file

    Hi,

    I have a simple XML document, to test QtXML:

    Qt Code:
    1. <?xml version = "1.0" encoding="UTF-8" standalone="yes" ?>
    2. <player>
    3. <name>John</name>
    4. <informations>
    5. <date format="JJ/MM/AAAA">31/08/2016</date>
    6. <points>100</points>
    7. </informations>
    8.  
    9. <name>Jack</name>
    10. <informations>
    11. <date format="JJ/MM/AAAA">31/08/2016</date>
    12. <points>100</points>
    13. </informations>
    14. </player>
    To copy to clipboard, switch view to plain text mode 

    When I try to read the XML nodes, I can't leave the first child : <name>John

    Below is the code :



    Qt Code:
    1. if (m_xmlFile->open(QIODevice::ReadOnly)) {
    2. if (!m_domDoc->setContent(m_xmlFile)) {
    3. QDomElement dom_elt = m_domDoc->documentElement();
    4. QDomNode noeud = dom_elt.firstChild();
    5. // QDomNode n1 = dom_elt.parentNode();
    6. QDomElement elt = noeud.toElement();
    7. QString strElt;
    8. if (!elt.isNull())
    9. strElt = elt.tagName();
    10. else
    11. strElt="ERROR";
    12. QMessageBox::information(this, "", strElt);
    13.  
    14. while (!noeud.isNull()) {
    15. QMessageBox::information(this, "while", "...");
    16. QDomElement noeud_element = noeud.toElement();
    17. if(!noeud_element.isNull()) {
    18. QMessageBox::information(this, "nom de la balise XML","noeud:"+noeud_element.tagName()
    19. +";texte:"+noeud_element.text());
    20. }
    21. noeud.nextSibling();
    22. }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    the message information is always :
    noeud: name;texte:john
    infinite loop

    Why the code doesn't go further ? What I have to add ?
    thank you

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

    Default Re: QtXML : read all the nodes of a xml file

    QDomNode::nextSibling() is a const method, it can't change the "noeud" object.
    It returns the "next sibling".

    If you don't need the DOM tree for document manipulation, you could alternatively look at QxmlStreamReader.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QtXML : read all the nodes of a xml file

    Change line 21 to:

    Qt Code:
    1. noeud = noeud.nextSibling();
    To copy to clipboard, switch view to plain text mode 

    A comment about your XML: What you have proposed is not a very useful structure for representing player information. All of the elements are at the same level. There is no real way to tell which "<informations>" element belongs to which player except for the way you have (probably) hand-coded the XML example. If a program wrote this same data, it might do it in a completely different order - all of the <name> elements first, followed by all of the <informations> elements - or maybe in some completely different order. It all depends on the particular DOM implementation. For example, some DOM implementations, like xerces-c, put the attributes in alphabetical order. Qt's DOM implementation doesn't. If you write code that depends on elements being in a certain order, you might end up with problems in a future Qt release.

    Your player information is really hierarchical, not flat. Your game has a set of players; each player has a name and score information. So a better way to represent this in XML is to make the document element <players> and add <player> elements for each person:

    Qt Code:
    1. <?xml version = "1.0" encoding="UTF-8" standalone="yes" ?>
    2. <players>
    3. <player>
    4. <name>John</name>
    5. <informations>
    6. <date format="JJ/MM/AAAA">31/08/2016</date>
    7. <points>100</points>
    8. </informations>
    9. </player>
    10. <player>
    11. <name>Jack</name>
    12. <informations>
    13. <date format="JJ/MM/AAAA">31/08/2016</date>
    14. <points>100</points>
    15. </informations>
    16. </player>
    17. </players>
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Sep 2016
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtXML : read all the nodes of a xml file

    Thank you guys for your reply.
    I understood what you wrote d_stranz, and i agree.
    The XML structure you proposed helped me, thank you very much !

Similar Threads

  1. Replies: 6
    Last Post: 24th August 2015, 07:25
  2. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 09:03
  3. is qt phonon can read realmedia file and divx file
    By fayssalqt in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2009, 16:42
  4. Having trouble with direct child nodes using QtXml
    By thelackey3326 in forum Qt Programming
    Replies: 2
    Last Post: 16th July 2008, 22:12
  5. Accessing DTD in an XML file via QtXml module?
    By jorma in forum Qt Programming
    Replies: 1
    Last Post: 6th May 2006, 19:09

Tags for this Thread

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.