0
QT MessageHandler(行、列、説明など)がXMLファイルに複数のエラーが存在するため、検証が成功しない場合に備えて解決策を見つけることを試みています。 XMLファイルで発生する最初のエラーだけでなく、XMLデータ内のすべてのエラーを表示します。QT XML validationエラー処理
例:(写真を参照)
65が、そこにエラーがラインにも、次のとおりです: 私は行でエラーが発生している78,83,95が、それはそれだけで示して表示されませ用量最初の1つ。
このケースの解決策はありますか?そしてもしそうなら、どのように?
は私のコードは次のようになります。
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;
};
感謝:)
はあなたのコードを投稿してください。 – has
すべてのエラーをキャッチするカスタムバリデーターを作成する必要があると思います。私は大学のプロジェクトで使った一例を投稿することができます。あなたのニーズに応じて変更することができます。 – has