[SOLVED] (Beginner) Very simple XML Parser



  • 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!



  • 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?



  • 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


  • Moderators

    Did you add the line
    @
    QT += xml
    @

    to your .pro file?



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



  • [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?



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



  • 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?



  • 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!)



  • 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@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.