PDA

View Full Version : QtXML : read all the nodes of a xml file



olap74
3rd September 2016, 11:08
Hi,

I have a simple XML document, to test QtXML:


<?xml version = "1.0" encoding="UTF-8" standalone="yes" ?>
<player>
<name>John</name>
<informations>
<date format="JJ/MM/AAAA">31/08/2016</date>
<points>100</points>
</informations>

<name>Jack</name>
<informations>
<date format="JJ/MM/AAAA">31/08/2016</date>
<points>100</points>
</informations>
</player>

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

Below is the code :





if (m_xmlFile->open(QIODevice::ReadOnly)) {
if (!m_domDoc->setContent(m_xmlFile)) {
QDomElement dom_elt = m_domDoc->documentElement();
QDomNode noeud = dom_elt.firstChild();
// QDomNode n1 = dom_elt.parentNode();
QDomElement elt = noeud.toElement();
QString strElt;
if (!elt.isNull())
strElt = elt.tagName();
else
strElt="ERROR";
QMessageBox::information(this, "", strElt);

while (!noeud.isNull()) {
QMessageBox::information(this, "while", "...");
QDomElement noeud_element = noeud.toElement();
if(!noeud_element.isNull()) {
QMessageBox::information(this, "nom de la balise XML","noeud:"+noeud_element.tagName()
+";texte:"+noeud_element.text());
}
noeud.nextSibling();
}
}
}

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

anda_skoa
3rd September 2016, 17:23
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,
_

d_stranz
3rd September 2016, 18:31
Change line 21 to:



noeud = noeud.nextSibling();


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:



<?xml version = "1.0" encoding="UTF-8" standalone="yes" ?>
<players>
<player>
<name>John</name>
<informations>
<date format="JJ/MM/AAAA">31/08/2016</date>
<points>100</points>
</informations>
</player>
<player>
<name>Jack</name>
<informations>
<date format="JJ/MM/AAAA">31/08/2016</date>
<points>100</points>
</informations>
</player>
</players>

olap74
4th September 2016, 23:32
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 !