Using the Qt XML Library for Easy Operation

Basically I want to use the XML parser from Qt in my existing project. I've only used Qt before, and this has been with Qt Designer, and I haven't had much luck finding anything on Google about how to simply use the XML library.

I have loaded a webpage with one big list and I want to parse it and add each list item to a C ++ list. I found this sample code on the Ubuntu forums ...

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

I want to use this, except I need to know what exactly I need to add to the project in order to access it.

Another little question - QDomDocument seems to be for files (makes sense), but I have XML in line. What part of the XML library works for string content?

+2


a source to share


2 answers


The following code should give you a quick idea of โ€‹โ€‹what to do with Qt XML functions 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 the core Qt library as well as the XML library.

+4


a source


You have to add QtXml

to .pro file and includeQDomDocument

or include QDomDocument

, for example



include <QtXml/QDomDocument>

      

+1


a source







All Articles