【问题标题】:Parsing XML in Qt在 Qt 中解析 XML
【发布时间】:2016-01-18 12:34:37
【问题描述】:

我有像这样解析 XML 的成员函数:

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();
        }
    }
}

我的 xml 树看起来像这样 http://pastebin.com/nFMJKcmU

我不知道为什么它没有在根元素信息中显示所有可用的标签

【问题讨论】:

    标签: c++ xml qt xml-parsing


    【解决方案1】:

    您在从官方文档示例重新输入时犯了一些错误。请看一下QDomDocument Class documentation 中描述的典型用法。所以你的代码必须是这样的:

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-06
      • 2013-07-11
      • 2012-10-06
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 1970-01-01
      相关资源
      最近更新 更多