我正在尝试使用QXmlSchema
加载以下XML架构,但QXmlSchema::load(const QUrl & source)
始终返回false
。有没有办法让Qt提供一些实际出错的地方?据我所知,模式在几个验证器中检查正常(w3c提供的神秘输出看起来像是通过了)。
<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="message">
<xsd:complexType>
<xsd:choice>
<xsd:element name="login-reply">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Accepted" />
<xsd:enumeration value="Rejected" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="login-request" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name="username" type="xsd:string" nillable="false"/>
<xsd:element name="password" type="xsd:string" nillable="false"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="logout-request">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="username" type="xsd:string" nillable="false"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="logout-reply">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Accepted" />
<xsd:enumeration value="Rejected" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="tasklist-request" />
<xsd:element name="tasklist-reply">
<xsd:complexType>
<xsd:sequence minOccurs="1">
<xsd:element name="package" minOccurs="1" nillable="false">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="taskgroup" minOccurs="1" nillable="false">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="id" type="xsd:integer" minOccurs="1" />
<xsd:element name="task" type="xsd:string" minOccurs="1" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:integer" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:integer" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="starttask-request">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="task-id" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="starttask-reply">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Accepted" />
<xsd:enumeration value="Rejected" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
答案 0 :(得分:1)
bool QXmlSchema :: load()本身只返回对调试没有用的布尔结果。但是,这是获得更合适的错误消息的更好方法。
您可以使用方法void QXmlSchema::setMessageHandler(QAbstractMessageHandler *handler)。
这是我项目的例子。
首先继承 QAbstractMessageHandler
class MessageHandler : public QAbstractMessageHandler
{
public:
MessageHandler()
: QAbstractMessageHandler(),
m_messageType(QtMsgType()),
m_description(),
m_sourceLocation(QSourceLocation())
{}
QString statusMessage() const
{
return m_description;
}
qint64 line() const
{
return m_sourceLocation.line();
}
qint64 column() const
{
return m_sourceLocation.column();
}
protected:
virtual void handleMessage(QtMsgType type,
const QString &description,
const QUrl &identifier,
const QSourceLocation &sourceLocation) Q_DECL_OVERRIDE
{
Q_UNUSED(type);
Q_UNUSED(identifier);
m_messageType = type;
m_description = description;
m_sourceLocation = sourceLocation;
}
private:
QtMsgType m_messageType;
QString m_description;
QSourceLocation m_sourceLocation;
};
然后在加载set message handler。
之前QFile file("myschema.xsd");
file.open(QIODevice::ReadOnly);
MessageHandler messageHandler;
QXmlSchema sch;
sch.setMessageHandler(&messageHandler);
if (sch.load(&file, QUrl::fromLocalFile(file.fileName()))==false)
{
QString error = messageHandler.statusMessage();
qint64 line = messageHandler.line();
qint64 column = messageHandler.column();
/*Do what need if error*/
}
答案 1 :(得分:0)
bool QXmlSchema :: load()根据this source documentation
在多个条件下返回false所以我的问题的答案:不,没有办法从Qt解析器获得每Qt 4.7.3的错误消息,并且没有办法通过Qts API来区分I / O错误和架构错误,因为load在两者上都返回false。