Reading a xml file and parsing it
-
wrote on 26 Nov 2015, 12:02 last edited by Aashu10
I am not that good with xml, but my basic xml file looks somthing like this.
XML CODE:
<MAIN_HEADER> <HEADER> <TITLE>my_title</TITLE> <AUTOR>DNL</AUTOR> <NAME>John</NAME> <AGE>abc</AGE> <SEX>male</SEX> <PLACE>abc</PLACE> <INI_FILE>abc</INI_FILE> </HEADER>
what I want to do is, I need to find 2-3 tags, say for example NAME & SEX and store the attribute(John,Male) in another variable.
until now, I have been able to make it read the xml file.
void MainWindow::XMLParser() { QString path=MainWindow::getWorkingDirectory()+"\\0_Config\\"; QString string; string = path + ui->ConfigFiles_combo->currentText(); QXmlStreamReader xmlreader; QFile file(string); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::information(this,tr("info"),tr("Failed to open file")); } else { qDebug()<<"ok"; } while(!xmlreader.atEnd()) { if(xmlreader.isStartElement()) { if(xmlreader.name()== "NEOTUX_CFG") { xmlreader.readNext(); } else if(xmlreader.name() == "HEADER") { while(!xmlreader.atEnd()) { if(xmlreader.isEndElement()) { if(xmlreader.name() == "TITLE") { xmlreader.readNext(); break; } xmlreader.readNext(); } else if(xmlreader.isCharacters()) { xmlreader.readNext(); } else if(xmlreader.isStartElement()) { if(xmlreader.name() == "PLATFORM") { xmlreader.readNext(); } else if(xmlreader.name() == "AUTHOR") { xmlreader.readNext(); } else if(xmlreader.name() == "TEST_VARIANT") { QString name=xmlreader.readElementText(); // found qDebug()<< name; } } else { xmlreader.readNext(); } } if(xmlreader.isEndElement()) { if(xmlreader.name() == "HEADER") { break; } xmlreader.readNext(); } else { xmlreader.readNext(); } } } } }
how do I make it search for the exact tag and store it inside another variable?
-
wrote on 26 Nov 2015, 12:10 last edited by Ratzz
Hi and welcome,
You can useisStartElement()
to read the start of the tag.name()
to match exact name.
writeStartElement
to start write the tag header andwriteTextElement
to write a text element . -
wrote on 26 Nov 2015, 12:17 last edited by
Hey!
I do not want to write anything on the XML, I need to fetch the attribute of that tag so that I can use it for later. Can you please show me some example which is related to my case. because every where I see I get examples of XmlStreamRreader.
-
wrote on 26 Nov 2015, 12:39 last edited byThis post is deleted!
-
Hey!
I do not want to write anything on the XML, I need to fetch the attribute of that tag so that I can use it for later. Can you please show me some example which is related to my case. because every where I see I get examples of XmlStreamRreader.
wrote on 26 Nov 2015, 13:03 last edited by Ratzz@Aashu10
You can read this way.qxmlstreamreader *xmlreader; while(!xmlreader->atEnd()) { if(xmlreader->isStartElement()) { if(xmlreader->name()== "MAIN_HEADER") { xmlreader->readnext(); } else if(xmlreader->name() == "HEADER") { while(!xmlreader->atEnd()) { if(xmlreader->isendelement()) { if(xmlreader->name() == "HEADER") { xmlreader->readNext(); break; } xmlreader->readNext(); } else if(xmlreader->isCharacters()) { xmlreader->readnext(); } else if(xmlreader->isStartElement()) { if(xmlreader->name() == "TITLE") { xmlreader->readNext(); } else if(xmlreader->name() == "AUTHOR") { xmlreader->readNext(); } else if(xmlreader->name() == "NAME") { qstring name=xmlReader->readElementText()) ;// found } ................. else { xmlreader->readNext(); } } else if(xmlreader->isEndElement()) { if(xmlreader->name() == "MAIN_HEADER") { break; } xmlreader->readNext(); } else { xmlreader->readnext(); } } }
-
wrote on 26 Nov 2015, 13:19 last edited by
So i do not have to consider the main tag? <MAIN_HEADER>
-
wrote on 26 Nov 2015, 13:37 last edited by
I am very new to Xmlstreamreader, Please bear with me
I am getting a error as " C2039: 'readnext' : is not a member of 'QXmlStreamReader' -
I am very new to Xmlstreamreader, Please bear with me
I am getting a error as " C2039: 'readnext' : is not a member of 'QXmlStreamReader' -
wrote on 26 Nov 2015, 13:52 last edited by Aashu10
Ah there's sytax error, trying real hard to find it out. I'm getting:
" error: C2181: illegal else without matching if"
on line:
else if(xmlreader.isEndElement()). any idea why? -
Probably } before else if(xmlreader.isEndElement()) is missing
-
Ah there's sytax error, trying real hard to find it out. I'm getting:
" error: C2181: illegal else without matching if"
on line:
else if(xmlreader.isEndElement()). any idea why? -
wrote on 26 Nov 2015, 14:16 last edited by
Thanks a ton! i think i am near to solving the problem, i have edited the program. You can see what exactly i am doing. I choosing a xml file from a dropdown. and QT parse that XML file. Now when i choose an xml file. Whole program gets stuck.
-
Thanks a ton! i think i am near to solving the problem, i have edited the program. You can see what exactly i am doing. I choosing a xml file from a dropdown. and QT parse that XML file. Now when i choose an xml file. Whole program gets stuck.
-
wrote on 26 Nov 2015, 14:36 last edited by Aashu10
Thanks alot! still can't figure out why does my gui hangs.
Can you please tell me what does String holds in:
"QString name=xmlreader.readElementText(); // found"I had a debug there to check, unfortunately my gui is hanging
-
Thanks alot! still can't figure out why does my gui hangs.
Can you please tell me what does String holds in:
"QString name=xmlreader.readElementText(); // found"I had a debug there to check, unfortunately my gui is hanging
-
wrote on 27 Nov 2015, 06:53 last edited by
Thanks for the help! this works too! But i think DOM is turning out to be simpler, i really appriciate your help. don't exactly know how to accept your answer on this forum.
-
Thanks for the help! this works too! But i think DOM is turning out to be simpler, i really appriciate your help. don't exactly know how to accept your answer on this forum.
wrote on 27 Nov 2015, 06:58 last edited by Ratzz@Aashu10
There is nothing like "answer" for particular question . You can Upvote the answer(s) that helped you to solve the issue.
Once your problem is solved please use the Topic Tools button to mark as Solved.
Thank you!
1/18