Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one validate marshalled XML with JAXB 2.0?

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 complaining about some of the incorrect XML I am forming! Can anyone give me some advice on how I can validate marshalled XML to make sure it conforms to the XSD schema.

Many thanks.

like image 452
Vidar Avatar asked Apr 30 '09 08:04

Vidar


People also ask

What Java Architecture for XML Binding JAXB annotation should you use when you want to prevent the mapping of a JavaBean property to its XML representation?

1.7) @XmlTransient Prevents the mapping of a JavaBean property/type to XML representation.

What is JAXB marshalling and Unmarshalling?

JAXB definitionsMarshalling is the process of transforming Java objects into XML documents. Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API.

How do I disable schema validation in JAXB?

To turn off the schema validation you should set the schema-validation-enabled property to false .


1 Answers

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.

like image 62
bruno conde Avatar answered Oct 02 '22 22:10

bruno conde