PDA

View Full Version : Qt XML validtion Error handling



S.D.
16th February 2017, 13:33
Hi :)

I'm trying to find a solution that in case that a validation is not successful because there is more then one error in the XML file the QT MessageHandler(line, column, description etc. ) is able to show every error in the XML data not just the first one that occurs in the XML file.

Example: I have an error in line: 65 (see pic)

12340

but there are also errors in line :78,83,95 but it dose not show it only shows the first one.

Is there a solution for this case? And if yes how?

My Code looks like this:



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



And my MessageHandler class looks like this:


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

thanks ;)

Lesiok
16th February 2017, 13:59
Further analysis does not make sense because the successive errors can be the consequence of the first.