Lubiz
Lubiz

Reputation: 3

QDomDocument: setContent() return false

I have a problem while i try to use setContent() into a QDomDocument object..

Here is the code:

QFile f("database.xml");
if(!f.open(QFile::ReadOnly))
    cout << "Error: file not correctly opened." << endl;

QDomDocument doc("database");
QString errorStr;
int errorLine;
int errorColumn;
if(!doc.setContent(&f, false, &errorStr, &errorLine, &errorColumn)){
    cout << "Error: " << errorStr.toStdString() << " at line " << errorLine << " column " << errorColumn << endl;
}
f.close();

and it print:

Error: unexpected end of file at line 1 column 1

How can i solve this mistake?

Here is the contents of the file :

<?xml version="1.0" encoding="UTF-8"?>
<myLibrary>
<movie>
    <price>5</price>
    <title>Star Wars 4</title>
    <register>George Lukas</register>
    <year>1977</year>
    <durate>192</durate>
</movie>
<movie>
    <price>8</price>
    <title>Rambo 1</title>
    <register>Joe Clarkson</register>
    <year>2012</year>
    <durate>167</durate>
</movie>
</myLibrary>

Upvotes: 0

Views: 1414

Answers (1)

kefir500
kefir500

Reputation: 4404

So the problem is an invalid path to your XML file.

If you are trying to open the file which is located in the same directory with your executable, use the QCoreApplication::applicationDirPath() method which return the needed path. Otherwise the process working directory will be used (which is not always an executable directory).

QFile f(QCoreApplication::applicationDirPath() + "/database.xml");

I guess you did not notice the Error: file not correctly opened. warning, did you?

Upvotes: 1

Related Questions