PDA

View Full Version : QXmlStreamReader not reading text within tags



Ceaser88
22nd July 2011, 15:41
I am trying to read the text in between xml tags using a simple QXmlStreamReader

i have filled a QXmlStreamReader called sectionReader with xml from this link:http://www.waseet.net/apps/iphone/xml/categories-file.xml

this code shows all start elements normally:
void MainWindow::parseXml2()
{
while (!sectionReader.atEnd())
{
sectionReader.readNext();
if(sectionReader.isStartElement())
{
ui->textEdit->append(sectionReader.name().toString());
}
}
}

whereas this code gives whitespaces:
void MainWindow::parseXml2()
{
while (!sectionReader.atEnd())
{
sectionReader.readNext();
if(sectionReader.isStartElement())
{
ui->textEdit->append(sectionReader.readElementText());
}
}
}

can anyone indicate what is going wrong? why aren't i reading any text in between the start and end tags?
I can verify that the xml has text in between the tags. here is the link if anyone wants to check: http://www.waseet.net/apps/iphone/xml/categories-file.xml

i have attached my .h and .cpp files along with the post

ChrisW67
22nd July 2011, 23:16
I haven't looked in detail at your code, but the XML is unusual in that everything inside the elements is contained in CDATA markers. This may exclude it from being considered as text by QXmlStreamReader because it could be any arbitrary binary.

ChrisW67
24th July 2011, 10:06
On closer inspection:

Your code starts and encounters the <root> element
isStartElement() is true
You read the entire content of the <root> element to extract any immediate child text nodes
There are no child text nodes... no output.
The file is now exhausted.


A more selective scan is more fruitful:


#include <QtCore>
#include <QDebug>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

QFile in("categories-file.xml");
if (in.open(QIODevice::ReadOnly)) {
QXmlStreamReader s(&in);
while (!s.atEnd()) {
s.readNext();
if (s.isStartElement() && s.name() == "en_main") {
qDebug() << s.name();
qDebug() << s.readElementText();
}
}
}
return 0;
}

output:

"en_main"
"Automotive"
"en_main"
"Community (The Big Heart)"
"en_main"
"Electronics & HiTech"
"en_main"
"Events & Announcements"
"en_main"
"Hi Tech"
"en_main"
"Hobbies & Collectibles"
...