Can one validate marshalled XML with JAXB 2.0?

后端 未结 2 958
隐瞒了意图╮
隐瞒了意图╮ 2020-12-28 08:19

Apparently in version 2 of JAXB - the validator class has been deprecated - does this mean the marshaller is automatically validating your XML? If so it doesn\'t seem to be

2条回答
  •  时光说笑
    2020-12-28 08:54

    Validation capabilities have been expanded in JAXB 2.0 through the use of the JAXP 1.3 Schema Validation Framework.

    Where before you did:

    unmarshaller.setValidating(true);
    

    now you need to do:

    SchemaFactory sf = SchemaFactory.newInstance(
        javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("myschema.xsd"));
    unmarshaller.setSchema(schema);
    

    If you pass null into setSchema, it disables validation.

    Please check this reference.

提交回复
热议问题