PDA

View Full Version : QDomDocument can't read the file content



baluk
23rd September 2010, 13:50
Hi,

In my Qt mobile application I am trying to read the xml file with QDomDocument, but QDom is not bale to read the content from the file even the file was existed and has some data.



file = new QFile("file.xml");
if(!file->open(QIODevice::ReadWrite | QIODevice::Text))
{
exit(1);
}
QDomDocument doc;
if(!doc.setContent(file))
{
exit(1);
} else
{
......
}


After the if(!doc.setContent(file)) statement the program simply exited with status code 1. I would be grateful for any help.

Thank you,
Baluk

Lykurg
23rd September 2010, 13:53
Then it is probably not well formed. Use all parameters of setContent and see what error message you get.

baluk
23rd September 2010, 14:40
yes, I have now used all the parameters and am getting the error ""unexpected end of file" at line: 2 col: 1 ". But my xml file format is correct with no sysntax errors.

Thank you,
Baluk

Lykurg
23rd September 2010, 15:05
Can you past the first 5 lines of your xml file.

baluk
23rd September 2010, 15:46
Here is the xml data. this format is working fine when I tried in other application in Windows. Now I am making mobile application in Linux environment.


<?xml version="1.0" encoding="UTF-8"?>

<list>

<expenses>

<name>city</name>

<date>21/09/2010</date>
<price>4000</price>

</expenses>
</list>


Thank you,

Baluk

Lykurg
23rd September 2010, 15:49
The only thing I can think of right know is, that it may be a problem with the encoding of your line breaks. Try to convert them to simple "\n" and try again of your device. (or remove all "\n" and "\r" for testing if that is the error.)

baluk
24th September 2010, 11:37
I am sorry but what do you mean by removing "\n" and "\r" from the file. I have tried placing the data in one line without any breaks but still not working.

Thank you,
Baluk

nish
24th September 2010, 11:56
what does file->readAll() return?

wysota
24th September 2010, 12:11
Are you sure you are using the correct file? The error suggests your file has only one line.

baluk
24th September 2010, 12:13
nothing returned to the byte array.

I have used this code


file = new QFile("file.xml");
QByteArray array;
array = file->readAll();
if(array.isNull())
{
qDebug() << "error" ;
exit(1);
}


program existed after the if statement.

baluk
24th September 2010, 12:22
Yes it has data. I am checking it every time I runthrough the program.

Thank you,

Baluk

wysota
24th September 2010, 12:22
You have to open the file before reading it.

wysota
24th September 2010, 12:23
I am checking it every time I runthrough the program.
How are you doing that?

baluk
24th September 2010, 12:27
Ya I did that , but getting the same result.



QByteArray array;
file = new QFile("expenses.xml");

if(!file->open(QIODevice::ReadWrite | QIODevice::Text))
{
exit(1);
}


array = file->readAll();
if(array.isNull())
{
qDebug() << "error" ;
exit(1);
}


Thank you,
Baluk

baluk
24th September 2010, 12:35
I am just checking the file if the file data is erased after I execute the program by some means.
Thank you,
Baluk

wysota
24th September 2010, 12:43
Please run the following code with your file:

#include <QtXml>
#include <QtCore>

int main(int argc, char **argv){
if(argc<2) return 1;
QFile file(argv[1]);
qDebug() << "File path:" << QFileInfo(file).absoluteFilePath();
qDebug() << "File exists:" << file.exists();
file.open(QFile::ReadOnly|QFile::Text);
qDebug() << "File open:" << file.isOpen();
QDomDocument dom;
QString error;
int line, int column;
if(dom.setContent(&file, &error, &line, &column)){
qDebug() << dom.toString(4);
} else {
qDebug() << "Error:" << error << "in line " << line << "column" << column;
}
return 0;
}

nish
24th September 2010, 13:03
can you attach your xml file here? if not confidential of course.

baluk
24th September 2010, 13:08
I run the code but and I get the error "../newxml-build-desktop/newxml exited with code 1".

Thank You,
Baluk.

baluk
24th September 2010, 13:16
Hi,

Ya sure, I am still in the beginning so no issue. Here is the file

Thank you,
Baluk

wysota
24th September 2010, 13:19
I run the code but and I get the error "../newxml-build-desktop/newxml exited with code 1".
Did you pass it the path to your file?

baluk
24th September 2010, 13:34
Hi Wysota,

Yes I run the code now I have figured out the problem after running your code. what my mistake is when I am creating the project in Qt crestor 4.7 I get two folders "NewXML"(projectname) and "newxml-build desktop" ( I don't know what it is for). I used to copy the file in the first one, but when i copy the file to the second one it is detecting the file.

I am extremely sorry to all of you.

Thank you,
Baluk

tbscope
24th September 2010, 13:43
The "*-build something" directory is the directory used to build the program. It contains the executable.
The other dir contains the source files.

You can change this behavior in Qt Creator.