PDA

View Full Version : QDom & xhtml



Potch
19th February 2010, 00:21
Hi,

This is my first experience with both DOM and xml. My aim is to use QDom to parse xhtml data.
I do not understand why the QDomNodeList contains 0 elements (according to gdb).



<html>
<body>
<p>
<report id="1">dd:</report>
</p>
<p>
<report id="2">MM:</report>
</p>
<p>
<report id="3">yyyy:</report>
</p>
</body>
</html>




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

QDomNodeList nodeList = doc.elementsByTagName("report");


How can I populate such list ?
any clue ?

thanks in advance

prof.ebral
19th February 2010, 00:55
I've worked with a Dom XML structure in my software and it looks to be the same. I am using Python so my answer might be incorrect, but I think you need to find the root of the document. I'll show you a snippet of code.


tree = xml.parseXml(txt)._get_documentElement()
ini.close()
node_list = tree.getElementsByTagName("MetaServerBaseURL")

Like I said, it's python, not pure Qt, but it definitely looks similar.

I should mention I don't like the DOM. Python has Element Tree and that is a much cleaner and more efficient way of finding data in XML files. I am a Qt Newbie, so I don't know if they have something like that in the language. If they do I would recommend moving away from the DOM.

Lykurg
19th February 2010, 08:51
I do not understand why the QDomNodeList contains 0 elements (according to gdb).
Then you a) don't understand how to use gdb correctly or b) you open the wrong file.
QString str = "<html><body><p><report id=\"1\">dd:</report></p>"
"<p><report id=\"2\">MM:</report></p>"
"<p><report id=\"3\">yyyy:</report></p></body></html>";
QDomDocument doc;
doc.setContent(str);
QDomNodeList nodeList = doc.elementsByTagName("report");
qWarning() << nodeList.count(); // returns 3

Potch
19th February 2010, 23:41
Ok for a)
: /

THanks !