Reading an XML document
-
I am trying to read the following XML file.
@<?xml version="1.0"?>
<Cities>
<City name="Chicago" heuristic="2132" />
<City name="St. Louis" heuristic="2056" />
</Cities>
@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.
-
you can use "this":http://www.xmlvalidation.com/
or simply xml validator of w3c "http://validator.w3.org":http://validator.w3.org -
For checking xml syntax etc. I regularly use "xmllint":http://xmlsoft.org/xmllint.html which is part of the "libxml2 package":http://xmlsoft.org.
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?
-
Well, the project is located on a thumb drive so that I can work on the program from multiple computers. Do you think that that is the cause?
How would I be able to tell if there is a unicode byte order mark at the 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"?>
<Cities>
<City name="Chicago" heuristic="2132"/>
<City name="St. Louis" heuristic="2056"/>
<City name="Shreveport" heuristic="2101"/>
<City name="New Orleans" heuristic="2198"/>
</Cities>
@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.
try
@
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.
@
Graph newGraph;
QString filename;
QFile file;filename = "GraphData.xml"; file.setFileName(filename); bool results = file.open(QIODevice::ReadOnly); if(!results) qDebug() << file.errorString(); else { Doing lots of good things .....
@
-
one way is to include your xml file in a resource file
-
if you locate the file somewhere in the filesystem, you need a path. If you don't specify a path, the program normally looks in the current folder (from which the executable is started). I donÄ't know, whether that is your project folder.
MSVS uses the project folder as current directory. -
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":http://doc.qt.nokia.com/latest/resources.html 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.
-
[quote author="perego" date="1306948065"]Gostaria de saber se alguem consegue me explicar como eu faço pra baixar ou melhor salvar um arquivo xml via qhttp ou qurl etc[/quote]
Please answer in English in this forum.
If you want to write in native language, please have a look at the respective forums.
-
[quote author="perego" date="1306948065"]Gostaria de saber se alguem consegue me explicar como eu faço pra baixar ou melhor salvar um arquivo xml via qhttp ou qurl etc[/quote]
"I wonder if someone can explain me how can i download or better to save a file via xml or qhttp qurl etc."
(from Google Translate)