【发布时间】:2017-07-04 10:07:07
【问题描述】:
我正在尝试找到一种解决方案,以防验证不成功,因为 XML 文件中存在多个错误,Qt MessageHandler(line, column, description etc.) 能够显示每个错误XML 数据不只是出现在 XML 文件中的第一个数据。
示例: 我有一个错误:65(见图)
但是 :78,83,95 行也有错误,但它没有显示它只显示第一个。
这种情况有解决方案吗?如果是的话怎么做?
我的代码如下所示:
MessageHandler messageHandler;
QFile xsdfile("....xsd");
xsdfile.open(QIODevice::ReadOnly);
QXmlSchema schema;
schema.setMessageHandler(&messageHandler);
bool errorOccurred = false;
if (schema.load(&xsdfile, QUrl::fromLocalFile(xsdfile.fileName())) == false)
errorOccurred = true;
else
{
QXmlSchemaValidator xmlvalidator(schema);
QFile xmlfile("......xml");
xmlfile.open(QIODevice::ReadOnly);
if (!xmlvalidator.validate(&xmlfile, QUrl::fromLocalFile(xmlfile.fileName())))
errorOccurred = true;
xmlfile.close();
}
xsdfile.close();
if (errorOccurred) {
QString qs = messageHandler.statusMessage();
cout << "Line: " << messageHandler.line() << "\n" << "Row: " << messageHandler.column() << "\n" << "ErrorMessage: ";
std::cout << qs.toUtf8().constData() << std::endl;
return -1;
}
else {
return 0;
}
我的 MessageHandler 类看起来像这样:
class MessageHandler : public QAbstractMessageHandler
{
public:
MessageHandler()
: QAbstractMessageHandler(0)
{
}
QString statusMessage() const
{
return m_description;
}
int line() const
{
return m_sourceLocation.line();
}
int column() const
{
return m_sourceLocation.column();
}
protected:
virtual void handleMessage(QtMsgType type, const QString &description,
const QUrl &identifier, const QSourceLocation &sourceLocation)
{
Q_UNUSED(type);
Q_UNUSED(identifier);
m_description = description;
m_sourceLocation = sourceLocation;
}
private:
QString m_description;
QSourceLocation m_sourceLocation;
};
谢谢:)
【问题讨论】:
-
发布您的代码。
-
我认为你必须编写一个自定义验证器来捕获所有错误。我可以发布一个我在大学项目中使用的示例。所以你可以根据自己的需要进行修改。
标签: c++ qt error-handling xml-validation qtxml