PDA

View Full Version : Reading a xml file and parsing it using DOM parser



Aashu10
26th November 2015, 11:39
I am not that good with xml, but my basic xml file looks somthing like this.


<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(); \\THIS IS WHERE´IT DETERMINES WHICH XML FILE IT IS
qDebug()<<string;
QDomDocument document;
//load the file
QFile file(string);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug()<<"Failed to open the file";

}

else
{
if(!document.setContent(false))
{
qDebug()<<"Failed to load document";

}
file.close();
}
QDomElement root = document.firstChildElement();
qDebug()<<"finished";

}

how do I make it search for the exact tag and store it inside another variable?

anda_skoa
26th November 2015, 14:18
Your setContent() line is wrong, it is not getting the file.

Anyway, QDomDocument::elementsByTagName().

Cheers,
_

jefftee
27th November 2015, 22:29
The example you show isn't valid XML. It should have a closing tag for <MAIN_HEADER>, as in </MAIN_HEADER>.