QXmlSchemaValidator Class

The QXmlSchemaValidator class validates XML instance documents against a W3C XML Schema. 更多...

头: #include <QXmlSchemaValidator>
qmake: QT += xmlpatterns
Since: Qt 4.6

该类在 Qt 4.6 引入。

注意: 此类的所有函数 可重入 .

公共函数

QXmlSchemaValidator (const QXmlSchema & schema )
QXmlSchemaValidator ()
~QXmlSchemaValidator ()
QAbstractMessageHandler * messageHandler () const
QXmlNamePool namePool () const
QNetworkAccessManager * networkAccessManager () const
QXmlSchema schema () const
void setMessageHandler (QAbstractMessageHandler * handler )
void setNetworkAccessManager (QNetworkAccessManager * manager )
void setSchema (const QXmlSchema & schema )
void setUriResolver (const QAbstractUriResolver * resolver )
const QAbstractUriResolver * uriResolver () const
bool validate (const QUrl & source ) const
bool validate (QIODevice * source , const QUrl & documentUri = QUrl()) const
bool validate (const QByteArray & data , const QUrl & documentUri = QUrl()) const

详细描述

The QXmlSchemaValidator class loads, parses an XML instance document and validates it against a W3C XML Schema that has been compiled with QXmlSchema .

The following example shows how to load a XML Schema from a local file, check whether it is a valid schema document and use it for validation of an XML instance document:

    QUrl schemaUrl("file:///home/user/schema.xsd");
    QXmlSchema schema;
    schema.load(schemaUrl);
    if (schema.isValid()) {
        QFile file("test.xml");
        file.open(QIODevice::ReadOnly);
        QXmlSchemaValidator validator(schema);
        if (validator.validate(&file, QUrl::fromLocalFile(file.fileName())))
            qDebug() << "instance document is valid";
        else
            qDebug() << "instance document is invalid";
    }
					
					

XML 模式版本

This class implements schema validation according to the XML Schema 1.0 specification.

另请参阅 QXmlSchema and XML 模式验证范例 .

成员函数文档编制

QXmlSchemaValidator:: QXmlSchemaValidator (const QXmlSchema & schema )

Constructs a schema validator that will use schema for validation. If an empty QXmlSchema schema is passed to the validator, the schema used for validation must be referenced in the XML instance document via the xsi:schemaLocation or xsi:noNamespaceSchemaLocation 属性。

QXmlSchemaValidator:: QXmlSchemaValidator ()

Constructs a schema validator. The schema used for validation must be referenced in the XML instance document via the xsi:schemaLocation or xsi:noNamespaceSchemaLocation 属性。

QXmlSchemaValidator:: ~QXmlSchemaValidator ()

销毁此 QXmlSchemaValidator .

QAbstractMessageHandler *QXmlSchemaValidator:: messageHandler () const

Returns the message handler that handles parsing and validation messages for this QXmlSchemaValidator .

另请参阅 setMessageHandler ().

QXmlNamePool QXmlSchemaValidator:: namePool () const

Returns the name pool used by this QXmlSchemaValidator for constructing 名称 . There is no setter for the name pool, because mixing name pools causes errors due to name confusion.

QNetworkAccessManager *QXmlSchemaValidator:: networkAccessManager () const

Returns the network manager, or 0 if it has not been set.

另请参阅 setNetworkAccessManager ().

QXmlSchema QXmlSchemaValidator:: schema () const

Returns the schema that is used for validation.

另请参阅 setSchema ().

void QXmlSchemaValidator:: setMessageHandler ( QAbstractMessageHandler * handler )

改变 message handler for this QXmlSchemaValidator to handler . The schema validator sends all parsing and validation messages to this message handler. QXmlSchemaValidator 未拥有所有权对于 handler .

Normally, the default message handler is sufficient. It writes compile and validation messages to stderr . The default message handler includes color codes if stderr can render colors.

QXmlSchemaValidator calls QAbstractMessageHandler::message (), the arguments are as follows:

message() argument Semantics
QtMsgType type QtWarningMsg and QtFatalMsg are used. The former identifies a warning, while the latter identifies an error.
const QString & description An XHTML document which is the actual message. It is translated into the current language.
const QUrl &identifier Identifies the error with a URI, where the fragment is the error code, and the rest of the URI is the error namespace.
const QSourceLocation & sourceLocation Identifies where the error occurred.

另请参阅 messageHandler ().

void QXmlSchemaValidator:: setNetworkAccessManager ( QNetworkAccessManager * manager )

将网络管理器设为 manager . QXmlSchemaValidator 未拥有所有权对于 manager .

另请参阅 networkAccessManager ().

void QXmlSchemaValidator:: setSchema (const QXmlSchema & schema )

设置 schema that shall be used for further validation. If the schema is empty, the schema used for validation must be referenced in the XML instance document via the xsi:schemaLocation or xsi:noNamespaceSchemaLocation 属性。

另请参阅 schema ().

void QXmlSchemaValidator:: setUriResolver (const QAbstractUriResolver * resolver )

将 URI (统一资源标识符) 解析器设为 resolver . QXmlSchemaValidator 未拥有所有权对于 resolver .

另请参阅 uriResolver ().

const QAbstractUriResolver *QXmlSchemaValidator:: uriResolver () const

Returns the schema's URI resolver. If no URI resolver has been set, Qt XML Patterns will use the URIs in instance documents as they are.

The URI resolver provides a level of abstraction, or polymorphic URIs . A resolver can rewrite logical URIs to physical ones, or it can translate obsolete or invalid URIs to valid ones.

When Qt XML Patterns calls QAbstractUriResolver::resolve () the absolute URI is the URI mandated by the schema specification, and the relative URI is the URI specified by the user.

另请参阅 setUriResolver ().

bool QXmlSchemaValidator:: validate (const QUrl & source ) const

Validates the XML instance document read from source against the schema.

返回 true if the XML instance document is valid according to the schema, false 否则。

范例:

    const QXmlSchema schema = getSchema();
    const QUrl url("http://www.schema-example.org/test.xml");
    QXmlSchemaValidator validator(schema);
    if (validator.validate(url))
        qDebug() << "instance document is valid";
    else
        qDebug() << "instance document is invalid";
					

bool QXmlSchemaValidator:: validate ( QIODevice * source , const QUrl & documentUri = QUrl()) const

Validates the XML instance document read from source 采用给定 documentUri against the schema.

返回 true if the XML instance document is valid according to the schema, false 否则。

范例:

    const QXmlSchema schema = getSchema();
    QFile file("test.xml");
    file.open(QIODevice::ReadOnly);
    QXmlSchemaValidator validator(schema);
    if (validator.validate(&file, QUrl::fromLocalFile(file.fileName())))
        qDebug() << "instance document is valid";
    else
        qDebug() << "instance document is invalid";
					

bool QXmlSchemaValidator:: validate (const QByteArray & data , const QUrl & documentUri = QUrl()) const

Validates the XML instance document read from data 采用给定 documentUri against the schema.

返回 true if the XML instance document is valid according to the schema, false 否则。

范例:

    const QXmlSchema schema = getSchema();
    QByteArray data("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                    "<test></test>");
    QBuffer buffer(&data);
    buffer.open(QIODevice::ReadOnly);
    QXmlSchemaValidator validator(schema);
    if (validator.validate(&buffer))
        qDebug() << "instance document is valid";
    else
        qDebug() << "instance document is invalid";