Junior
Junior

Reputation: 259

Parsing XML in Qt

I have member function for parsing XML like this:

void xmlparser::parsingFunction()
{
    while(1)
    {
        QFile file("info.xml");
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug("Failed to open file for reading");
        }

        QDomDocument document;

        if(!document.setContent(&file))
        {
            qDebug("Failed to parse the file into a Dom tree");
            file.close();
        }
        file.close();

        QDomElement documentElement = document.documentElement();

        QDomNode node = documentElement.firstChildElement();

        while(!node.isNull())
        {
            if(node.isElement())
            {
                QDomElement first = node.toElement();
                emit xmlParsed(first.tagName());
                sleep(5);
            }
            node.nextSibling();
        }
    }
}

My xml tree looks like this http://pastebin.com/nFMJKcmU

I am not sure why It doesn't show all the available tags in root element info

Upvotes: 0

Views: 520

Answers (1)

NG_
NG_

Reputation: 7173

You've made some errors while retyping from official documentation example. Please, take a look at its typical usage described in QDomDocument Class documentation. So your code must look like:

QDomElement docElem = document.documentElement();
QDomNode n = docElem.firstChild();
while (!n.isNull()) {
    // Try to convert the node to an element.
    QDomElement e = n.toElement();
    if (!e.isNull()) {
        // The node really is an element.
        qDebug() << qPrintable(e.tagName()) << endl;
    }
    n = n.nextSibling();
}

Upvotes: 1

Related Questions