[Qt-interest] QXmlSreamReader

Carlos Manuel Duclos Vergara carlos.duclos at trolltech.com
Tue Apr 21 09:05:43 CEST 2009


Hi,

> I have the book "C++ GUI Programming with Qt 4" and I want to make a
> program, which reads den Strings out of the .xml file, during the runtime.
>
> In the book, they load the file in the main.cpp like this:
>
> int main [int argc, char *argv[])
> {
> QApplication app(argc, argv);
> QStringList args = QApplication::arguments();
> ...
> QTreeWidget
> ...
> XmlStreamReader reader(&treeWidget);
> for (int i = 1; i < args.count(); i++)
>     reader.readFile(args[i]);
> return app.exec();
> }
>
> during the runtime I can't use the command
>
> reader.readFile(&file);
>
> does the commang reader.setDevice(&file); do the same or where can I
> write the command readFile to get the program working.
>

If you are using XmlStreamReader you need to use readNext(...) instead of 
readFile(...). The following code shows all the tags in an xml file:

    QFile xmlFile("test.xml");
    if(!xmlFile.open(QIODevice::ReadOnly | QIODevice::Text))
        return false;

    QXmlStreamReader xmlStream(&xmlFile);
    while(!xmlStream.atEnd())
    {
        xmlStream.readNext();
        if(xmlStream.isStartDocument())
        {
            qDebug("Start Document: ");
        }
        if(xmlStream.isEndDocument())
        {
            qDebug("End Document: ");
        }
        if(xmlStream.isStartElement())
        {
            qDebug("Start Element: ");
            qDebug() << xmlStream.name().toString();
            qDebug("Start Element attributes: ");
            QXmlStreamAttributes attributes = xmlStream.attributes();
            foreach(QXmlStreamAttribute a, attributes)
            {
                qDebug() << a.name().toString() << a.value().toString();
            }
            qDebug("Start Element text: ");
            qDebug() << xmlStream.text().toString();
        }
        if(xmlStream.isEndElement())
        {
            qDebug("End Element: ");
            qDebug() << xmlStream.name().toString();
            qDebug() << xmlStream.text().toString();
        }
    }
    if(xmlStream.hasError())
    {
        qDebug("error");
        return -1;
    }
    return 0;



-- 
Carlos Manuel Duclos Vergara
Senior Software Engineer, Team Lead / QA
Qt SW, a Nokia Company
Sandakerveien 116, NO-0484 Oslo, Norway



More information about the Qt-interest-old mailing list