PDA

View Full Version : XML resource is garbled?



space_otter
19th June 2010, 04:58
I've added a .rc file with an xml document containing a list of unit conversions. Here's some code:


QFile rc(":/data/units.xml");
if(!rc.open(QIODevice::ReadOnly))
{
qDebug() << "File Error: " << rc.errorString();
return;
}
QTextStream stream( &rc );
qDebug() << "Start doc:";
qDebug() << stream.readAll();
qDebug() << "End doc";
rc.close();

After a certain point, a few random characters come out and the stream ends prematurely.
Start doc:
"<unitlist>

<!-- couple dozen lines of xml i wrote-->

<utype name="ti‘Ð’Ëi€End doc

Any idea what could be causing this?

agathiyaa
19th June 2010, 18:50
To avoid errors, specify the XML encoding, or save XML files as Unicode.

space_otter
20th June 2010, 03:53
I added <?xml version="1.0" encoding="utf-8"?> and explicitly saved in UTF-8 but the problem persists. It fails consistently in the same location. I've given the file. If you open it in a browser it gives the same complaint as Qt. So there is a problem with the file and not the resource. Is the encoding still messed up?

I also have some png resources, which display fine, but they're smaller than this file. It seems kind of weird that a 3.7KB file would be too much, but is there a limit on the size of a resource?

You might have to rename the attachment to units.xml, I don't know why the website added a period.

agathiyaa
20th June 2010, 04:31
Hi,

Top level item is missing in your file. In the below example "property" is the top level item. You cannot have multiple top-level items as in your file.

eg.,

<?xml version="1.0" encoding="utf-8"?>
<property>
<unitlist>
<utype name="mass">
</unitlist>
<powers>
<powers>
.
.
.
</property>

space_otter
20th June 2010, 05:46
Thanks, I don't really know xml that well :p that fixes the parsing problem. But I still don't understand why the readout still ends prematurely, or why the commented part also ends too early.

agathiyaa
20th June 2010, 08:30
Hi,

Reading your units.xml just works fine for me (the file is not well formed-your sample). Did you use notepad to edit the xml stuff ? Did you edit the items ?. Something may be wrong in your function or just before calling your function ?!.

space_otter
21st June 2010, 03:37
I used notepad++. The file is in a resource and I access it with ":/data/units.xml" and that might have something to do with it.

agathiyaa
21st June 2010, 05:25
Try reading line by line and see if that works !~

space_otter
22nd June 2010, 01:09
Ah! thanks. Looks like readAll can't be trusted?