QtXML : read all the nodes of a xml file
Hi,
I have a simple XML document, to test QtXML:
Code:
<?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 :
Code:
if (!m_domDoc->setContent(m_xmlFile)) {
// QDomNode n1 = dom_elt.parentNode();
if (!elt.isNull())
strElt = elt.tagName();
else
strElt="ERROR";
while (!noeud.isNull()) {
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
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,
_
Re: QtXML : read all the nodes of a xml file
Change line 21 to:
Code:
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:
Code:
<?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>
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 !