xsd:Java类中的唯一

时间:2017-05-17 09:24:11

标签: java xml xsd

我的架构中的 dcy = (Second.Bottom + Second.Top) - (First.Bottom + First.Top) if dcy >=0 then shift Second.Top by (First.Bottom - Second.Top) //down else shift Second.Top by -(Second.Bottom - First.Top) //up dcx = (Second.Left + Second.Right) - (First.Left + First.Right) if dcx >=0 then shift Second.Left by (First.Right - Second.Left) //right else shift Second.Left by -(Second.Right - First.Left) //left 约束定义了少数元素的主键和外键。

虽然架构不会引发任何错误。

虽然未添加生成java类xsd:unique

xsd:unique

1 个答案:

答案 0 :(得分:1)

JAXB不会强制执行验证规则。

要强制执行验证规则,您需要在解析 XML时指定XSD架构。

假设您要求Unmarshaller为您解析XML,例如你没有解组DOM节点,你这样做:

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaFile); // file or URL

JAXBContext jaxbContext = JAXBContext.newInstance();
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(handler); // To specify how validation errors should be handled
Object obj = unmarshaller.unmarshal(source);

这在Unmarshaller

的javadoc中有所描述
  

验证和良好形成

     

客户端应用程序可以通过setSchema(javax.xml.validation.Schema) API启用或禁用JAXP 1.3验证机制。复杂的客户端可以指定自己的验证SAX 2.0兼容解析器,并使用unmarshal(Source) API绕过JAXP 1.3验证机制。

     

由于在JAXB 2.0中定义了解组无效XML内容,因此Unmarshaller默认验证事件处理程序比JAXB 1.0更宽松。当JAXB 1.0绑定编译器生成的模式派生代码向JAXBContext注册时,默认的unmarshal验证处理程序为DefaultValidationEventHandler,并在遇到致命错误或错误后终止编组操作。对于JAXB 2.0客户端应用程序,没有显式定义的默认验证处理程序,默认事件处理仅在遇到致命错误后终止解组操作。

相关问题