December 14, 2010

cazador7907 cazador7907
Lab Rat
78 posts

Reading an XML document

 

I am trying to read the following XML file.

  1. <?xml version="1.0"?>
  2. <Cities>
  3.     <City name="Chicago" heuristic="2132" />
  4.     <City name="St. Louis" heuristic="2056" />
  5. </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.

 Signature 

Laurence -

 

15 replies

December 14, 2010

stuk stuk
Ant Farmer
561 posts

you can use this [xmlvalidation.com]
or simply xml validator of w3c http://validator.w3.org [validator.w3.org]

December 14, 2010

Volker Volker
Ant Farmer
5428 posts

For checking xml syntax etc. I regularly use xmllint [xmlsoft.org] which is part of the libxml2 package [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?

December 14, 2010

cazador7907 cazador7907
Lab Rat
78 posts

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?

 Signature 

Laurence -

 

December 14, 2010

peppe peppe
Ant Farmer
1028 posts

Use a hexadecimal editor/dumper, like od -c file:

  1. $ cat test.txt
  2. foo
  3. $ od -c test.txt
  4. 0000000 357 273 277   f   o   o  \n
  5. 0000007

See the BOM there?
(UTF8 with BOM, which is quite pointless, but many programs silently add it).

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

December 14, 2010

cazador7907 cazador7907
Lab Rat
78 posts

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.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Cities>
  3.  <City name="Chicago" heuristic="2132"/>
  4.  <City name="St. Louis" heuristic="2056"/>
  5.  <City name="Shreveport" heuristic="2101"/>
  6.  <City name="New Orleans" heuristic="2198"/>
  7. </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.

 Signature 

Laurence -

 

December 14, 2010

Volker Volker
Ant Farmer
5428 posts

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

  1. hexdump -C x.xml
  2. 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:

  1. head -1 yourfile.xml | hexdump -C

December 14, 2010

Tobias Hunger Tobias Hunger
Robot Herder
3387 posts

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.

December 14, 2010

Volker Volker
Ant Farmer
5428 posts

Tobias, you’re completely right and very welcome :-)
I better should have stated, that this is sample output of hexdump, not of valid utf-16. I must construct this stuff by hand, as none of my editors creates utf-16 with BOM :-/

December 14, 2010

cazador7907 cazador7907
Lab Rat
78 posts

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.

  1.     Graph newGraph;
  2.     QString filename;
  3.     QFile file;
  4.  
  5.     filename = "GraphData.xml";
  6.  
  7.     file.setFileName(filename);
  8.     bool results = file.open(QIODevice::ReadOnly);
  9.  
  10.     if(!results)
  11.         qDebug() << file.errorString();
  12.     else
  13.     {
  14.         Doing lots of good things .....

 Signature 

Laurence -

 

December 14, 2010

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

one way is to include your xml file in a resource file

December 14, 2010

Gerolf Gerolf
Robot Herder
3286 posts

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.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

December 14, 2010

Volker Volker
Ant Farmer
5428 posts

If the file contents is static, you can add it to your application resources and use it in a QFile there:

  1. 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.

June 1, 2011

perego perego
Lab Rat
1 posts

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

June 1, 2011

Volker Volker
Ant Farmer
5428 posts
perego wrote:
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

Please answer in English in this forum.

If you want to write in native language, please have a look at the respective forums.

June 1, 2011

mariusg mariusg
Ant Farmer
1099 posts
perego wrote:
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

“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)

 Signature 

Project Manager - Qt Development Frameworks

 
  ‹‹ SQLite date field      [SOLVED] How to build Release with debug info (MinGW) ››

You must log in to post a reply. Not a member yet? Register here!