#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QXmlSchemaValidator>
#include <QXmlSchema>
#include <QFile>
#include <QtDebug>
#include <QAbstractMessageHandler>
class MessageHandler : public QAbstractMessageHandler {
public:
MessageHandler() : QAbstractMessageHandler(nullptr) {}
QString statusMessage() const { return m_description; }
int line() const { return m_sourceLocation.line(); }
int column() const { return m_sourceLocation.column(); }
protected:
void handleMessage(QtMsgType type, const QString &description,
const QUrl &identifier, const QSourceLocation &sourceLocation) override {
Q_UNUSED(type);
Q_UNUSED(identifier);
m_description = description;
m_sourceLocation = sourceLocation;
}
private:
QString m_description;
QSourceLocation m_sourceLocation;
};
void validateXML(const QString &schemaPath, const QString &xmlPath) {
QFile xsdFile(schemaPath);
if (!xsdFile.open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open schema file.";
return;
}
MessageHandler messageHandler;
QXmlSchema schema;
schema.setMessageHandler(&messageHandler);
if (!schema.load(&xsdFile, QUrl::fromLocalFile(xsdFile.fileName()))) {
qDebug() << "Schema loading failed.";
qDebug() << "Error:" << messageHandler.statusMessage();
qDebug() << "Line:" << messageHandler.line();
qDebug() << "Column:" << messageHandler.column();
return;
}
QFile xmlFile(xmlPath);
if (!xmlFile.open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open XML file.";
return;
}
QXmlSchemaValidator validator(schema);
if (!validator.validate(&xmlFile, QUrl::fromLocalFile(xmlFile.fileName()))) {
qDebug() << "XML validation failed.";
qDebug() << "Error:" << messageHandler.statusMessage();
qDebug() << "Line:" << messageHandler.line();
qDebug() << "Column:" << messageHandler.column();
} else {
qDebug() << "XML is valid.";
}
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
validateXML("1.xsd","1.xml");
}
MainWindow::~MainWindow()
{
delete ui;
}
ASSERT failure in virtual qulonglong QPatternist::Decimal::toUnsignedInteger() const: "It makes no sense to call this function, see Numeric::toUnsignedInteger().", file data\qdecimal.cpp, line 174