我目前正在修改一些使用 QDomDocument 来解析 XML 文件内容的旧代码。示例代码如下:
QFile file(file_.filePath());
if (file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
file.close();
QString errorMsg;
int errorLine;
int errorColumn;
if (!doc.setContent(in.readAll(), &errorMsg, &errorLine, &errorColumn))
{
qWarning("Invalid XML in file %s. Error = %s, Line = %d, Column = %d", qPrintable(file_name), qPrintable(errorMsg), errorLine, errorColumn);
}
}
不幸的是,错误报告非常有限。删除文件中途的结束标记只会报告以下错误:
文件 config.xml 中的 XML 无效。错误 = 文件意外结束,行 = 1,列 = 1
这是相当无用的。
关于如何从 Qt 的 XML 解析器中获取更多描述性错误的任何建议?准确的行号将是一个好的开始。
附言我使用的是 Qt 4.7.4 版。
最佳答案
QDomDocument::setContent
应该为您提供正确的信息以了解问题出在哪里。
例如,使用这段代码:
#include <QtXml>
#include <QtCore>
int main()
{
QFile file(":/myxml_error.xml");
qDebug() << "File path:" << QFileInfo(file).absoluteFilePath();
qDebug() << "File exists:" << file.exists();
file.open(QFile::ReadOnly|QFile::Text);
qDebug() << "File open:" << file.isOpen();
QDomDocument dom;
QString error;
int line, column;
if(dom.setContent(&file, &error, &line, &column)){
qDebug() << dom.toString(4);
} else {
qDebug() << "Error:" << error << "in line " << line << "column" << column;
}
return 0;
}
还有这个 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</Ffrom>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
我们看到以下错误:
错误:第 4 行第 19 列中的“标签不匹配”
我在阅读@kh25 的一些评论后更新了这个答案。
- 问题可能是由文件编码引起的。在这种情况下,我们可以尝试删除回车和换行。
dos2unix
如果我们在 Linux 上工作,可以提供帮助。 - 另一种选择是使用 online tool检查 XML。
但在这种特殊情况下,问题似乎与关闭文件有关 -- file.close()
-- 在调用 in.readAll()
之前。在这种情况下,QDomDocument::setContent
正在读取一个空字符串,我们看到错误unexpected end of file
。
获取错误的另一种方法是在文件到达流末尾时调用 QDomDocument::setContent
。
所以,如果我们调用 QTextStream::readAll()
两次,我们会得到同样的错误。即:
QTextStream in(&file);
in.readAll();
if(dom.setContent(in.readAll(), &error, &line, &column)) {
qDebug() << "Content: " << dom.toString(4);
} else {
qDebug() << "Error:" << error << "in line " << line << "column" << column;
}
@MrEricSir 对使用 QXMLStreamReader
发表了评论。如果您需要示例,我在 GitHub 中有一个小项目我在哪里使用那个类(class)。
关于c++ - 使用 QDomDocument 时报告详细的 XML 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36576660/