tags:

views:

307

answers:

2

I basically want to use the XML parser from Qt in my existing project. I have only used Qt once before, and that was with Qt Designer, and I am not having much luck finding anything on Google about how to just use the XML library.

I have downloaded a web page that has one large list, and I want to parse it and add each list item to a c++ list. I found this sample code on Ubuntu forums...

http://www.uluga.ubuntuforums.org/showpost.php?p=9112973&postcount=6

I want to use that except I need to know what exactly I need to add to the project to get access to it.

One other small question is QDomDocument seems to be for files (makes sense) but I have the XML in a string. What part of the XML library works for contents of a string?

+1  A: 

The following code should give you a quick view of what to do with Qt XML features for what you need.

#include <list>
#include <QDomDocument>
#include <QFile>

int main()
{
    QString filename("myfile.xml");
    std::list<QString> result;
    int errorLine, errorColumn;
    QString errorMsg;
    QFile modelFile(filename);
    QDomDocument document;
    if (!document.setContent(&modelFile, &errorMsg, &errorLine, &errorColumn))
    {
            QString error("Syntax error line %1, column %2:\n%3");
            error = error
                    .arg(errorLine)
                    .arg(errorColumn)
                    .arg(errorMsg);
            return false;
    }
    QDomElement rootElement = document.firstChild().toElement();
    for(QDomNode node = rootElement.firstChild();
        !node .isNull();
        node = node .nextSibling())
    {
        QDomElement element = node.toElement();
        result.push_back(element.tagName());
    }
    return 0;
}

UPDATE I believe you only need core Qt library as well as XML library.

Benoît
Awesome, thank you!
David Powers
A: 

You have to add QtXml in the .pro file and include QDomDocument

or only include QDomDocument like

include <QtXml/QDomDocument>
Patrice Bernassola
What project level includes/libs do I need to just use QDomDocument? The example projects I looked at seemed to include everything, so I am not sure what I can leave out.
David Powers
I show you how to use one of the type (here QDomDocument). In the example you provide, you may have to do the same for `QDomNodeList` and `QDomElement` if not include by QDomDocument
Patrice Bernassola