Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reading an XML document
QtWS25 Last Chance

Reading an XML document

Scheduled Pinned Locked Moved General and Desktop
16 Posts 8 Posters 13.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stukdev
    wrote on 14 Dec 2010, 13:26 last edited by
    #2

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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 14 Dec 2010, 13:52 last edited by
      #3

      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?

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cazador7907
        wrote on 14 Dec 2010, 15:02 last edited by
        #4

        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?

        Laurence -

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on 14 Dec 2010, 15:35 last edited by
          #5

          Use a hexadecimal editor/dumper, like od -c file:
          @
          $ cat test.txt
          foo
          $ od -c test.txt
          0000000 357 273 277 f o o \n
          0000007
          @
          See the BOM there?
          (UTF8 with BOM, which is quite pointless, but many programs silently add it).

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

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cazador7907
            wrote on 14 Dec 2010, 15:57 last edited by
            #6

            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.

            Laurence -

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on 14 Dec 2010, 16:30 last edited by
              #7

              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
              @

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tobias.hunger
                wrote on 14 Dec 2010, 18:04 last edited by
                #8

                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.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on 14 Dec 2010, 18:11 last edited by
                  #9

                  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 :-/

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    cazador7907
                    wrote on 14 Dec 2010, 18:28 last edited by
                    #10

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

                    @

                    Laurence -

                    1 Reply Last reply
                    0
                    • ? This user is from outside of this forum
                      ? This user is from outside of this forum
                      Guest
                      wrote on 14 Dec 2010, 18:33 last edited by
                      #11

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

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on 14 Dec 2010, 18:35 last edited by
                        #12

                        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.

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

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on 14 Dec 2010, 18:40 last edited by
                          #13

                          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.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            perego
                            wrote on 1 Jun 2011, 17:07 last edited by
                            #14

                            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

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goetz
                              wrote on 1 Jun 2011, 17:21 last edited by
                              #15

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

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mgran
                                wrote on 1 Jun 2011, 17:23 last edited by
                                #16

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

                                Project Manager - Qt Development Frameworks

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved