Parsing XML file with QXmlStreamReader
-
wrote on 5 Jan 2021, 15:34 last edited by
Hello;
Please find below the code (I found in a website) and I use it to parse the xml file
test1_number = 0; if (reader.readNextStartElement()) { if (reader.name() == "root"){ while(reader.readNextStartElement()){ if(reader.name() == "FirstChild"){ while(reader.readNextStartElement()){ if(reader.name() == "test1"){ qDebug << "Hello Test1 \n"; test1_number ++ ; } else reader.skipCurrentElement(); } } else reader.skipCurrentElement(); } } else reader.raiseError(QObject::tr("Incorrect file")); }
xml file :
<?xml version="1.0" encoding="UTF-8"?> <!--example with no DTD--> <root> <FirstChild> <test1 type= "girl"> <name> asmaa </name> <age> 14</age> </test1> <test1 type= "boy"> <name> boy </name> <age> 10</age> </test1> </FirstChild> <SecondChild>text3</SecondChild> </root>
I want to count number of test1 Item in the xml file but I always get 1 even if we have more than that in the file
Can anyone help me please
-
Lifetime Qt Championwrote on 6 Jan 2021, 12:23 last edited by SGaist 1 Jun 2021, 12:24
This works as expected:
#include <QCoreApplication> #include <QFile> #include <QXmlStreamReader> #include <QtDebug> int main(int argc, char **argv) { QCoreApplication app(argc, argv); QFile file("test.xml"); if(!file.open(QFile::ReadOnly | QFile::Text)){ qDebug() << "Cannot read file" << file.errorString(); exit(0); } QXmlStreamReader reader(&file); int test1_number = 0; if (reader.readNextStartElement()) { if (reader.name() == "root" ){ while(reader.readNextStartElement()) { qDebug() << "First level start element name" << reader.name(); if(reader.name() == "FirstChild") { while(reader.readNextStartElement()) { qDebug() << "Second level start element name" << reader.name(); if(reader.name() == "test1"){ qDebug() << "Hello Test1 \n"; test1_number++ ; } reader.skipCurrentElement(); } } else { reader.skipCurrentElement(); } } } else { reader.raiseError(QObject::tr("Incorrect file")); } } qDebug() << "Number of 'test1'" << test1_number; return 0; }
-
Hi,
Are you following this one ?
-
I think you are missing a "reader.skipCurrentElement();" after your counter update.
You have more data under test1 and these you do not seem to want to parse. You current code will continue getting name and age and then stop since there are no more start element after it.
-
I think you are missing a "reader.skipCurrentElement();" after your counter update.
You have more data under test1 and these you do not seem to want to parse. You current code will continue getting name and age and then stop since there are no more start element after it.
wrote on 6 Jan 2021, 09:04 last edited by@SGaist but after the counter update I add a reader.skipCurrentElement(). please take a look to this part
if(reader.name() == "FirstChild"){ while(reader.readNextStartElement()){ if(reader.name() == "test1"){ qDebug << "Hello Test1 \n"; test1_number ++ ; } else reader.skipCurrentElement(); } } else reader.skipCurrentElement();
-
@SGaist but after the counter update I add a reader.skipCurrentElement(). please take a look to this part
if(reader.name() == "FirstChild"){ while(reader.readNextStartElement()){ if(reader.name() == "test1"){ qDebug << "Hello Test1 \n"; test1_number ++ ; } else reader.skipCurrentElement(); } } else reader.skipCurrentElement();
wrote on 6 Jan 2021, 09:25 last edited by@Mery_lamb
Debug outreader.name()
at every stage to see where your reader is getting to. -
@SGaist but after the counter update I add a reader.skipCurrentElement(). please take a look to this part
if(reader.name() == "FirstChild"){ while(reader.readNextStartElement()){ if(reader.name() == "test1"){ qDebug << "Hello Test1 \n"; test1_number ++ ; } else reader.skipCurrentElement(); } } else reader.skipCurrentElement();
@Mery_lamb said in Parsing XML file with QXmlStreamReader:
@SGaist but after the counter update I add a reader.skipCurrentElement(). please take a look to this part
if(reader.name() == "FirstChild"){ while(reader.readNextStartElement()){ if(reader.name() == "test1"){ qDebug << "Hello Test1 \n"; test1_number ++ ; } else reader.skipCurrentElement(); } } else reader.skipCurrentElement();
No you did not, it's in the else clause.
-
@Mery_lamb said in Parsing XML file with QXmlStreamReader:
@SGaist but after the counter update I add a reader.skipCurrentElement(). please take a look to this part
if(reader.name() == "FirstChild"){ while(reader.readNextStartElement()){ if(reader.name() == "test1"){ qDebug << "Hello Test1 \n"; test1_number ++ ; } else reader.skipCurrentElement(); } } else reader.skipCurrentElement();
No you did not, it's in the else clause.
wrote on 6 Jan 2021, 09:48 last edited byThis post is deleted! -
@Mery_lamb
Debug outreader.name()
at every stage to see where your reader is getting to.wrote on 6 Jan 2021, 11:17 last edited by@JonB I did and I found that inside the if(reader.name() == "test1"), I get only one element which mean that maaaaybe I forget reader.skipCurrentElement() but I am not sure where to add it
-
Lifetime Qt Championwrote on 6 Jan 2021, 12:08 last edited by SGaist 1 Jun 2021, 12:09
right after
test1_number++
.Or just remove the
else
of that specificif
since you are not doing any other processing of what is inside that element. -
right after
test1_number++
.Or just remove the
else
of that specificif
since you are not doing any other processing of what is inside that element. -
@JonB I did and I found that inside the if(reader.name() == "test1"), I get only one element which mean that maaaaybe I forget reader.skipCurrentElement() but I am not sure where to add it
wrote on 6 Jan 2021, 12:12 last edited by@Mery_lamb
I haven't looked at it/used this XML reader code. But I think you'll find that after you read the<test1 type= "girl">
you still have go down into the<name> asmaa </name>
,<age> 14</age>
stuff, and then you have to come out again to move onto the<test1 type= "boy">
.Anyway, I wouldn't know without trying it myself.
-
Lifetime Qt Championwrote on 6 Jan 2021, 12:23 last edited by SGaist 1 Jun 2021, 12:24
This works as expected:
#include <QCoreApplication> #include <QFile> #include <QXmlStreamReader> #include <QtDebug> int main(int argc, char **argv) { QCoreApplication app(argc, argv); QFile file("test.xml"); if(!file.open(QFile::ReadOnly | QFile::Text)){ qDebug() << "Cannot read file" << file.errorString(); exit(0); } QXmlStreamReader reader(&file); int test1_number = 0; if (reader.readNextStartElement()) { if (reader.name() == "root" ){ while(reader.readNextStartElement()) { qDebug() << "First level start element name" << reader.name(); if(reader.name() == "FirstChild") { while(reader.readNextStartElement()) { qDebug() << "Second level start element name" << reader.name(); if(reader.name() == "test1"){ qDebug() << "Hello Test1 \n"; test1_number++ ; } reader.skipCurrentElement(); } } else { reader.skipCurrentElement(); } } } else { reader.raiseError(QObject::tr("Incorrect file")); } } qDebug() << "Number of 'test1'" << test1_number; return 0; }
-
This works as expected:
#include <QCoreApplication> #include <QFile> #include <QXmlStreamReader> #include <QtDebug> int main(int argc, char **argv) { QCoreApplication app(argc, argv); QFile file("test.xml"); if(!file.open(QFile::ReadOnly | QFile::Text)){ qDebug() << "Cannot read file" << file.errorString(); exit(0); } QXmlStreamReader reader(&file); int test1_number = 0; if (reader.readNextStartElement()) { if (reader.name() == "root" ){ while(reader.readNextStartElement()) { qDebug() << "First level start element name" << reader.name(); if(reader.name() == "FirstChild") { while(reader.readNextStartElement()) { qDebug() << "Second level start element name" << reader.name(); if(reader.name() == "test1"){ qDebug() << "Hello Test1 \n"; test1_number++ ; } reader.skipCurrentElement(); } } else { reader.skipCurrentElement(); } } } else { reader.raiseError(QObject::tr("Incorrect file")); } } qDebug() << "Number of 'test1'" << test1_number; return 0; }
1/14