1

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?

5
  • What version of Qt are you using? If you are using Qt 5 then the DOM classes should be replaced with the XmlStreamRreader Commented Nov 26, 2015 at 11:04
  • Then I would suggest look at the example code for the XmlStreamReader that is part of the docs, it should be good enough to get you started. Otherwise if you really need DOM and can't find examples or answers I might be able to provide some Commented Nov 26, 2015 at 12:07
  • Yeah i really need dom, it's pretty complicated with XmlStreamRreader. Can you please provide some example which are simliar to my case. Commented Nov 26, 2015 at 12:15
  • So are you preferring DOM or the stream reader? My question is based on your question on the Qt forum. Commented Nov 26, 2015 at 14:45
  • Yeah! DOM is very simple and easy Commented Nov 26, 2015 at 15:06

2 Answers 2

2

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"
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! this totally works. but the problem is, the loop doesn't stop, not sure where to put the break .age is: "5" name is: "john"age is: "5" name is: "john"age is: "5" name is: "john"age is: "5" name is: "john"age is: "5"
@ramtheconqueror, I reformatted your code, hope you do not mind.
@user5603723, you added the code that prints out the age, my guess is you did something wrong. Can you perhaps share what you have added
I cannot upvote you, because of my low rep. Thanks! this toally worked. You can now close the thread :)
@user5603723 Threads cant really be closed, but since you accepted the answer that is good enough. I do suggest that you edit your original post and remove the last code snipped that you posted for me, for people that might look at the topic if they have the same problem. They will see that the marked post worked and work with that code as an example. The added code in the post can confuse people.
|
1

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.

1 Comment

Thanks! i think i am pretty close to solving this, what i am actually doing is i am choosing a xml file from the list of xml files, but when i choose one the program stops. Any idea why? Please take a look at EDIT1 in my main code. I have put everything there. @The Badger

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.