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. [SOLVED] (Beginner) Very simple XML Parser
Forum Update on Monday, May 27th 2025

[SOLVED] (Beginner) Very simple XML Parser

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 6.5k 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.
  • P Offline
    P Offline
    Philili
    wrote on 7 May 2012, 16:27 last edited by
    #1

    Hi! I'm trying to use the SAX parser for XML in Qt. I've got some problems to implement this... It's very simple, only using a command console for the moment:
    MyHandler.h :
    @#include <QtXml/QXmlDefaultHandler>

    class MyHandler : public QXmlDefaultHandler
    {
    public:
    bool readFile(const QString &fileName);

    protected:
    bool startElement(const QString &namespaceURI,
    const QString &localName,
    const QString &qName,
    const QXmlAttributes &attributes);
    bool endElement(const QString &namespaceURI,
    const QString &localName,
    const QString &qName);
    bool characters(const QString &str);
    bool fatalError(const QXmlParseException &exception);
    };

    #endif
    @

    MyHandler.cpp :
    @#include <QtDebug>
    #include "MyHandler.h"

    bool MyHandler::readFile(const QString &fileName)
    {
    QFile file(fileName);
    QXmlInputSource inputSource(&file);
    QXmlSimpleReader reader;
    reader.setContentHandler(this);
    reader.setErrorHandler(this);
    return reader.parse(inputSource);
    }

    bool MyHandler::startElement(const QString & /* namespaceURI /,
    const QString & /
    localName */,
    const QString &qName,
    const QXmlAttributes &attributes)
    {
    qDebug() << "Start of element " << qName;
    for (int i=0; i<attributes.length(); i++)
    qDebug() << " " << attributes.qName(i) << "=" << attributes.value(i);
    return true;
    }

    bool MyHandler::endElement(const QString & /* namespaceURI /,
    const QString & /
    localName */,
    const QString &qName)
    {
    qDebug() << "End of element " << qName;
    return true;
    }

    bool MyHandler::fatalError(const QXmlParseException &exception)
    {
    qDebug() << "Parse error at line " << exception.lineNumber()
    << ", " << "column " << exception.columnNumber() << ": ";
    return false;
    }
    @

    and main.cpp :
    @#include "MyHandler.h"

    int main(int argc, char *argv[])
    {
    MyHandler handler;
    handler.readFile("MyFile.xml");
    return 0;
    }
    @

    But, I've got 48 errors (!) about "undefined reference in function QXmlContentHandler". Maybe it's a problem with the "include"?
    Thanks a lot!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on 7 May 2012, 16:44 last edited by
      #2

      This sounds more like a linker error message.

      Including the required header (.h) file is one thing, but you will also have to link your binary against the library file (.lib) that actually implements those functions that are declared in the header file!

      Do you link your project against the "QtXml4.lib" library?

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Philili
        wrote on 7 May 2012, 17:43 last edited by
        #3

        Thanks for your reply. It's great. I don't have any QtXml4.lib bit I've got QtXml4.dll in a lib directory.
        I've tried in MyProject.pro :
        @LIBS += "C:\QtSDK\Desktop\Qt\4.7.4\mingw\lib\QtXml4.dll"
        @ but the error is now: File not found

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on 7 May 2012, 17:46 last edited by
          #4

          Did you add the line
          @
          QT += xml
          @

          to your .pro file?

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on 7 May 2012, 17:47 last edited by
            #5

            Nope, you can't link DLL files directly.

            There always is a .lib file (called an "import library") that corresponds to the DLL file.

            The linker needs the .lib file, the resulting EXE file then needs the .dll at runtime.

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Philili
              wrote on 7 May 2012, 18:17 last edited by
              #6

              [quote author="mlong" date="1336412783"]Did you add the line
              @
              QT += xml
              @

              to your .pro file?
              [/quote]
              That's it! I forgot that. here's my .pro :
              @QT += xml
              CONFIG += console
              HEADERS = MyHandler.h
              SOURCES = main.cpp
              MyHandler.cpp@

              But the error is now: "collect2: ld returned 1 exit status - File not Found" What's missing?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MuldeR
                wrote on 7 May 2012, 18:35 last edited by
                #7

                Looks like you told the linker to add the required .lib file, but it's missing from your system.

                Is there a "QtXml4.lib" file on your system? Should be in your "Qt\lib" folder...

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Philili
                  wrote on 7 May 2012, 18:54 last edited by
                  #8

                  Thanks for your help! No, it's not here. I've searched the entire drive. I have only the .dll version. where can I find this file?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MuldeR
                    wrote on 7 May 2012, 18:56 last edited by
                    #9

                    How did you install Qt?

                    Everything you need to build Qt apps is included in the Qt SDK download:
                    http://www.developer.nokia.com/dp?uri=http://sw.nokia.com/id/23f71960-21d3-4b7a-9329-4d5484c49c68/Qt_SDK_Win_online

                    Be sure to install the following package:
                    Development Tools > Desktop Qt > Qt 4.8.1 (Desktop) > MinGW -or- MSVC 2008/2010

                    (BTW: You shouldn't think about the .lib and .dll files as different "versions". The .lib file is the required import library for the .dll file. The .lib is needed to build the executable, the .dll will be needed when you run it!)

                    My OpenSource software at: http://muldersoft.com/

                    Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                    Go visit the coop: http://youtu.be/Jay...

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Philili
                      wrote on 8 May 2012, 18:09 last edited by
                      #10

                      The .lib is in the right directory (4.8.1) now, thanks! I didn't know tha there was this update.
                      I've got problems for the Path setup, Is it different now?
                      I've tried:
                      in User variables :
                      @PATH : C:\QtSDK\Desktop\Qt\4.8.1\msvc2010\bin;C:\QtSDK\Desktop\Qt\4.7.4\mingw\bin@
                      and
                      @QTDIR : C:\QtSDK\Desktop\Qt\4.8.1@
                      and
                      @QMAKESPEC : win32-g++@ (don't know whether it's used)

                      like I've seen: http://sector.ynet.sk/qt4-tutorial/preparations.html
                      But I can't build:
                      @The program has unexpectedly finished.
                      C:\test\test2\debug\test2.exe exited with code -1073741511@

                      1 Reply Last reply
                      0

                      1/10

                      7 May 2012, 16:27

                      • Login

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