2 Attachment(s)
QXmlStreamReader not reading text within tags
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/xm...ories-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/xm...ories-file.xml
i have attached my .h and .cpp files along with the post
Re: QXmlStreamReader not reading text within tags
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.
Re: QXmlStreamReader not reading text within tags
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:
Code:
#include <QtCore>
#include <QDebug>
int main(int argc, char *argv[])
{
QFile in
("categories-file.xml");
QXmlStreamReader s(&in);
while (!s.atEnd()) {
s.readNext();
if (s.isStartElement() && s.name() == "en_main") {
qDebug() << s.name();
qDebug() << s.readElementText();
}
}
}
return 0;
}
output:
Code:
"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"
...