PDA

View Full Version : Getting next child element from XML file



di_zou
17th September 2009, 21:49
I have a XML file in this format:


<heading>
<element1>text</element1>
<element2>text</element2>
<element3>text</element3>
</heading>

I have code like this:



QDomElement element = xmlFile.firstChildElement("heading");

QString variable = element.firstChildElement("element1").text();


How do I get the text of element2? How do I get the text of a specific element? Thanks.

SudaNix
18th September 2009, 00:41
use nextSibling();

here an example from the docs:


QDomDocument doc("mydocument");
QFile file("mydocument.xml");
if (!file.open(QIODevice::ReadOnly))
return;
if (!doc.setContent(&file)) {
file.close();
return;
}
file.close();


QDomElement docElem = doc.documentElement();

QDomNode n = docElem.firstChild();
while(!n.isNull()) {
QDomElement e = n.toElement();
if(!e.isNull()) {
cout << qPrintable(e.text()) << endl;
}
n = n.nextSibling();
}

NoRulez
18th September 2009, 07:24
http://doc.trolltech.com/4.5/qdomdocument.html#details

LG NoRulez