user5603723
user5603723

Reputation: 193

QT: Reading a xml file and parsing it using DOM parser

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?

Upvotes: 1

Views: 9346

Answers (2)

ramtheconqueror
ramtheconqueror

Reputation: 1964

why do you have setContent(false) again? Looks like you just copied The Badger's code. Try this.

void MainWindow::XMLParser()
{
    // don't worry about path separator, Qt will take of it
    QString string = MainWindow::getWorkingDirectory() + "/0_Config/" + ui->ConfigFiles_combo->currentText();
    qDebug()<<string;
    QDomDocument document;
    //load the file
    QFile xmlFile(string);
    if (!xmlFile.exists() || !xmlFile.open(QFile::ReadOnly | QFile::Text)) {
        qDebug() << "Check your file";
        return;
    }
    QDomDocument domDocument;
    domDocument.setContent(&xmlFile);
    QDomElement topElement = domDocument.documentElement();
    QDomNode domNode = topElement.firstChild();
    while (!domNode.isNull()) {
        QDomElement domElement = domNode.toElement();
        if (!domElement.isNull()) {
            //qDebug() << domElement.tagName();
            if (domElement.tagName() == "HEADER") {
                QDomNode node = domElement.firstChild();
                while (!node.isNull()) {
                    QDomElement element = node.toElement();
                    if (!element.isNull()) {
                        const QString tagName(element.tagName());
                        if (tagName == "NAME") {
                            qDebug() << "Name is:" << element.text();
                        } else if (tagName == "SEX") {
                            qDebug() << "Sex is:" << element.text();
                        }
                    }
                    node = node.nextSibling();
                }
            }
        }
        domNode = domNode.nextSibling();
    }
}

This is my console output

Name is: "John"
Sex is: "male"
Name is: "Doe"
Sex is: "male"

Upvotes: 2

CJCombrink
CJCombrink

Reputation: 3950

Have a look at the QDomDocument docs, the example should be good enough: I am assuing the file is opened (your setContent() function call is not correct).

if (document.setContent(&file) == false) {
    file.close();
    return;
}
QDomElement docElem = doc.documentElement();

QDomElement headerElement = docElem.firstChildElement("HEADER");
if(headerElement.isNUll() == true) {
    return;
}
/* Get the name */
QDomElement nameElement = headerElement.firstChildElement("NAME");
QString name = nameElement.text();

/* Get the sex */
QDomElement sexElement = headerElement.firstChildElement("SEX");
QString sex = sexElement.text();

Edit: Look at the docs on QDomElement as well, there is some code that you can also use. My above code look similar to the last snippet in the description.

Upvotes: 1

Related Questions