How to Parse an XML file using QDomDocument
-
Hi,
I need to parse an XML FIle shown below using QDomDocument and QDomElement```
<?xml version="1.0" encoding="UTF-8"?> <CONF> <General> <IO>General</IO> <IO>Audio</IO> <IO>Bash</IO> <IO>Bluetooth</IO> <IO>Battery</IO> <IO>Camera</IO> <IO>Ethernet</IO> <IO>BioScanner</IO> <IO>GSM-GPRS</IO> <IO>GPS</IO> <IO>IFD</IO> <IO>Innovatrics</IO> <IO>V_Info</IO> <IO>JFFS2</IO> <IO>LCD</IO> <IO>Keypad</IO> <IO>MagSwipe</IO> <IO>Printer</IO> <IO>SAM</IO> <IO>Script</IO> <IO>SD-Card</IO> <IO>Settings</IO> <IO>TurnOff</IO> <IO>USB</IO> <IO>Production</IO> </General> <Production> <Analog> <PPS>1</PPS> <PSDMU>1</PSDMU> <PUSBMU>1</PUSBMU> <PETHP>1</PETHP> <PGSMSP>1</PGSMSP> <PSAM>1</PSAM> <PA>1</PA> <PW>1</PW> <POTG>1</POTG> <PEMID>1</PEMID> <PBT>1</PBT> </Analog> <Digital> <PFPEV>1</PFPEV> <PMAG>1</PMAG> <PSDMU>1</PSDMU> <PA>1</PA> <PSAM>1</PSAM> <PEMID>1</PEMID> </Digital> <F-Stage> <PRTC>1</PRTC> <PRTCU>1</PRTCU> </F-Stage> <SubContract> <PPS>1</PPS> <PPAD>1</PPAD> <PKPD>1</PKPD> <PLCDC>1</PLCDC> <PFPEV>1</PFPEV> <PMAGD>1</PMAGD> <PMAG>1</PMAG> <PBTR>1</PBTR> <PA>1</PA> <PSDMU>1</PSDMU> <PUSBMU>1</PUSBMU> <PSAM>1</PSAM> <PGSMSP>1</PGSMSP> <PRTC>1</PRTC> </SubContract> <Priliminary> <PRTC>1</PRTC> <PPAD>1</PPAD> <PFPEV>1</PFPEV> <PMAGD>1</PMAGD> <PMAG>1</PMAG> <PLCDC>1</PLCDC> <PA>1</PA> <PBTR>1</PBTR> <PGSMS1>1</PGSMS1> <PGPRSS2>1</PGPRSS2> <PSIMSW>1</PSIMSW> <PRTC>1</PRTC> <PGSMIMEI>1</PGSMIMEI> <PMID>1</PMID> </Priliminary> <QC> <PPS>1</PPS> <PA>1</PA> <PJFFS2E>1</PJFFS2E> <PSAM>1</PSAM> <PSDMU>1</PSDMU> <PRTC>1</PRTC> </QC> </Production> </CONF>
Here is my ".cpp" file
qDebug("Inside the PARSE Slot"); QDomDocument xmlDocument; QFile f("Config.XML"); if(!f.open(QIODevice::ReadOnly)) { qDebug("Error While Reading the File"); } xmlDocument.setContent(&f); f.close(); qDebug("File was closed Successfully"); QDomElement root=xmlDocument.documentElement(); QString startTag = root.tagName(); qDebug()<<"The ROOT tag is"<<startTag; // Get root names and attributes QDomElement General=root.firstChild().toElement(); QString data = General.tagName(); qDebug()<<"The FirstChild is"<<data; qDebug("After the General DOM Element"); while(!General.isNull()) { qDebug("Inside the GENERAL WHILE Loop"); if(General.tagName()=="General") { qDebug("Inside the General IF Condition"); QDomElement Component = General.firstChild().toElement(); QString cmp = Component.tagName(); qDebug()<<"The data in Component Element is"<<cmp; QString IO; while(!Component.isNull()) { //qDebug("Inside the Component WHILE Loop"); if(Component.tagName() == "IO") { IO = Component.firstChild().toText().data(); qDebug()<<"The Name of the IO is:"<<IO; if(IO.contains("Audio",Qt::CaseInsensitive)==true) { qDebug("Audio was present"); } if(IO.contains("Production",Qt::CaseInsensitive)==true) { qDebug("Inside the Production"); break; } } Component =Component.nextSibling().toElement(); } if(General.tagName()=="Production") { qDebug("Inside the Production"); break; } } }
The Problem is thw while loop was not exiting as i have written break to exit the while when i needed.
Please guide me,if there is an any other class to parse the above shown XML file that would help to make my code less please suggest.Any sort of help wolud be appriciated.Thanks in advance,
Rohith.G -
You have this loop:
while(!General.isNull()) {}
General is not changed inside the loop, so how do you want to exit the loop if none of the conditions for break are met?
Your code is just wrong. You should fix it instead to look for another XML parsing class.
Here you can see some examples how to parse XML: http://doc.qt.io/qt-5.5/qdomdocument.html -
Hi,
You can use QXmlPutGet class .