Reading an XML document
I am trying to read the following XML file.
- <?xml version="1.0"?>
- <City name="Chicago" heuristic="2132" />
- <City name="St. Louis" heuristic="2056" />
The error that I’m getting though – Unexpected end of file at line 1 column 1 – has me a little baffled. I suspect that the XML file is not well formed. I’ve looked at this silly file for the last hour and it seems ok to me. Perhaps a different set of eyes will see something that I’m not?
Also, is there a particular tool that people use to validate XML documents? The tools that I used to use have gone to that heaven where software goes when computer’s crash and burn.
Your XML is perfectly valid. As the error occurs on line 1, column 1, could it be you read an XML file from disk that contains an unicode byte order mark (BOM) at the very beginning?
Right. Based on everyone’s replies, I did two things.
I downloaded a program from the Mac site called EditiX (to create a new XML document) and then downloaded a Hex Editor to make sure that if a BOM was prepended to the file, I could strip it out. The new file text is below.
- <?xml version="1.0" encoding="UTF-8"?>
- <City name="Chicago" heuristic="2132"/>
- <City name="St. Louis" heuristic="2056"/>
- <City name="Shreveport" heuristic="2101"/>
- <City name="New Orleans" heuristic="2198"/>
However, I’m still receiving the same error. I don’t have access to the the program code at the moment but will post it later this afternoon.
Thanks for the fast responses. It’s a very bumpy road right now, but I’m learning fast.
I prefer hexdump with switch -C to show the output – I like hex numbers more than octal/decimal values. It’s available on Macs by default.
- hexdump -C x.xml
- 00000000 ff fe 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d |..<?xml version=|
If the file is long you can use the following to just print the first line:
- head -1 yourfile.xml | hexdump -C
Volker: I need to nitpick, sorry…
Your hexdump seems to be utf-8 encoded, but “ff fe” is the UTF-16 encoded BOM (actually “ff fe” or “fe ff”, depending on byte order).
In UTF-8 the proper encoding should be: “ef bb bf” (or in ISO-8859-1 the sequence of these characters: ï»¿). Note that a Byte Order Mark does not really makes sense in a bytewise encoding… so its use is actually discouraged in a utf-8 context. BOM in UTF-8 does break e.g. scripts on Unix systems which require the shebang to be first in a file.
I think that I’ve almost solved this problem. The issue was not BOM but that the dang fool program cant’ seem to find the file to open it! I admit it. I am a dolt sometimes.
My question for the group though is how to I specify the relative path to the file? If I specify just the filename (with not path), it can’t find it. The file is co-located with the rest of the source code so I would expect it to immediately locate the file. In the project explorer though, it appears under the “Other Files” node of the tree view. I suppose that I could hard code a file path but that would defeat the purpose of relative paths.
Source code is below.
If the file contents is static, you can add it to your application resources and use it in a QFile there:
- QFile f(":/GraphData.xml");
See the docs on the resource system [doc.qt.nokia.com] for further details.
You can manage them nicely with Qt Creator too, then there is no need to fiddle around in the qrc XML file manually.