1

Below is my code & xml file. I am trying to read a xml file and store the contents. But its not working. Please help

int ParseConfigFile::populateUserValues ( string &OS, string &dir, string &logPath )
{
    QDomDocument doc;
    QDomElement element;

    QFile file("config_v1.xml");

    if ( doc.setContent( file.readAll() ) == false )
        return 1;

    element = doc.documentElement();

    QDomNodeList list = element.childNodes();
    QDomElement firstChild = list.at(0).toElement(); // will return the first child
    QDomElement secondChild = list.at(1).toElement();
    QDomElement thirdChild = list.at(2).toElement();


    QString s1,s2,s3;
    s1 = firstChild.text();
    s2 = secondChild.text();
    s3 = thirdChild.text();

    OS = s1.toStdString();
    dir = s2.toStdString();
    logPath = s3.toStdString();

    return 0;
}

and my XML

<?xml version="1.0"?>
<config>
  <type>LINUX</type>
  <dir>/home/</dir>
  <path>/var/log/</path>
</config>
2
  • 1
    What is your error? Besides, this aint a good way to parse xml. As soon as you change the order in the xml your code eeads the wrong values. Commented Dec 17, 2010 at 15:41
  • I am not getting any output i.e NULL. Can you please suggest me which is best way to parse. I am newbie to XML. Commented Dec 17, 2010 at 15:47

1 Answer 1

1

You should instantiate the QDomDocument like this and then your code will work:

if ( doc.setContent(&file) == false )
        return 1;

QDomDocument can work with an IO device (like QFile) making this the optimum way to set the QDomDocument's content from a file.

Anyway, if you're trying to write a configuration file reader/writer I suggest you stick with QSettings

QSettings settings("MyCompany", "MyApp");
QString s1,s2,s3;
s1 = settings.value("type").toString();
s2 = secondChild..value("dir").toString();
s3 = thirdChild..value("path").toString();

The configuration file format won't be XML (it will be key=value) but you will hardly ever need to worry about the file itself. To set any value on the file is also easy:

QSettings settings("MyCompany", "MyApp");
settings.setValue("path", "/var/log/");

Your configuration file will look like this:

[General]
path=/var/log/

See: QSettings documentation

Sign up to request clarification or add additional context in comments.

5 Comments

What do you mean by QSetting?
@Winbros - QSettings is a class that helps you to read and write from a configuration file. It's extremely easy to use and it will save the configuration file on the appropriate user folder on the platform the program is running (i.e Users/me/Application Data/myApp on windows and home/me/.config/myApp on Linux) doc.troll.no/4.7/qsettings.html
Just edited the answer do you can see how this would be accomplished with QSettings
@Raphael but where will you mention the config filename
@Winbros - It's the second argument passed to QSettings constructor. Basically it creates a file with the name of the second argument on a folder with the name of the first argument

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.