4

我正在使用 Qt 5.11.1(MSVSC2015 32bit) 和 QtCreator 4.6.2。我无法使用 QXmlStreamReader 解析 XML。代码是基于Qt 的示例编写的。当我的代码被执行时,它会在 checkWarnMessage 函数的 QIODevice.cpp 中产生访问冲突。此图像显示调用堆栈和发生访问冲突的确切行。

实际的 XML 更复杂并且具有嵌套元素。解析 XML 的函数的实现方式与 Qt 示例中的 void XbelReader::readXBEL() 函数相同(根据元素名称调用适当的函数来解析该元素)。但是通过这个简单的例子,我设法重现了我在实际解决方案中遇到的问题。

XML 是:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <element1>1</element1>
    <element2>2</element2>
    <element3>3</element3>
    <element4>4</element4>
    <element5>5</element5>
    <element6>6</element6>
</root>

解析此 XML 的代码是:

#include <string>
#include <stdexcept>
#include <iostream>

#include <QCoreApplication>
#include <QXmlStreamReader>
#include <QFile>
#include <QString>

#define ASSERT_ELEMENT_NAME(NAME) Q_ASSERT(xmlReader.isStartElement() && xmlReader.name() == NAME);

using namespace std;

void OpenFile(const QString& fileName, QXmlStreamReader& xmlReader)
{
    QFile configFile(fileName);
    if (configFile.open(QFile::ReadOnly | QFile::Text) == false)
        throw runtime_error(string("Failed to open file: ") + configFile.errorString().toStdString());

    xmlReader.setDevice(&configFile);
    if (xmlReader.readNextStartElement() == false)
        throw runtime_error("File does not have root element");

    if (xmlReader.name() != "root")
        throw runtime_error("File has invalid root element");
}

void ParseElement1(QXmlStreamReader& xmlReader)
{
    ASSERT_ELEMENT_NAME("element1");

    auto text = xmlReader.readElementText().trimmed();
    auto isOk = false;
    auto value = text.toInt(&isOk);

    if (isOk == false)
        throw runtime_error(string("invalid value: ") + text.toStdString());
    else
        cout << "element1: " << value << endl;
}

void ParseElement2(QXmlStreamReader& xmlReader)
{
    ASSERT_ELEMENT_NAME("element2");

    auto text = xmlReader.readElementText().trimmed();
    auto isOk = false;
    auto value = text.toInt(&isOk);

    if (isOk == false)
        throw runtime_error(string("invalid value: ") + text.toStdString());
    else
        cout << "element2: " << value << endl;
}

int main()
{
    QXmlStreamReader xmlReader;

    OpenFile("config.xml", xmlReader);

    while(xmlReader.readNextStartElement())
    {
        if(xmlReader.name() == "element1")
            ParseElement1(xmlReader);
        if(xmlReader.name() == "element2")
            ParseElement2(xmlReader);
        else
            xmlReader.skipCurrentElement();
    }
}

如果我在主函数中注释两行:

        if(xmlReader.name() == "element2")
            ParseElement2(xmlReader);

不会发生访问冲突。

我真的不知道我做错了什么。或者 QXmlStreamReader 中是否存在错误?我认为即使我做错了什么,Qt 的库中也不应该发生访问冲突。

可以从此链接下载整个项目(XmlParser.pro、main.cpp 和 config.xml)

编辑

我已经按照 Manthan 的建议修复了我的示例,并且它按预期工作。我在 XML 中又添加了一件东西。我在 element1 之前添加了大的多行注释。注释本身有 8019 个字符,包括空白字符,而整个 XML 文件有 8266 个字符。

XML 现在看起来像

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <!--
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, 
    comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment, comment,
    comment, comment, co
    -->
    <element1>1</element1>
    <element2>2</element2>
    <element3>3</element3>
    <element4>4</element4>
    <element5>5</element5>
    <element6>6</element6>
</root>

我检查了 XML 在带有 XML 工具插件的 Notepad++ 以及 xmlvalidation.xml 中是否有效。当我使用新的 XML 执行固定示例时,我再次在先前链接的图像上描述的完全相同的位置发生访问冲突。

现在,如果我从 XML 中的注释中删除最后一个“o”字符(或注释中的任何其他字符,或者例如 element6 的文本中的“6”,或者事实上,在保持 XML 有效的情况下来自 XML 的任何字符),示例是成功执行。这是我原来的问题。我的原始 XML 文件中有很多注释,导致文件大于 8KB。目前,作为一种解决方法,我删除评论以避免访问冲突。

我不清楚评论(或文件)长度如何导致访问冲突。

整个项目可以从此链接下载

4

1 回答 1

4

问题在while循环内。更新如下。

while(xmlReader.readNextStartElement())
{
    if(xmlReader.name() == "element1")
        ParseElement1(xmlReader);
    else if(xmlReader.name() == "element2")
        ParseElement2(xmlReader);
    else
        xmlReader.skipCurrentElement();
}

在您的代码中,第一个元素的类型为“element1”,然后首先处理(使用 first if),然后它们再次到达else尝试跳过它的位置,这会导致问题。

于 2018-08-23T06:10:01.543 回答