For unmarshalling the same XML document that we created in the previous section, create a Java class—JAXB2UnMarshaller.java
. In the unmarshaller
class create a JAXBContext
object as in the previous section and create an Unmarshaller
object using the createUnmarshaller()
method:
JAXBContext jaxbContext = JAXBContext.newInstance("jaxbeclipse"); Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
For XML Schema validation we need to set an XML Schema on the Unmarshaller
object. Create a SchemaFactory
object using the static
method newInstance()
, a Schema
object from the SchemaFactory
object using the newSchema()
method, and set the schema on the Unmarshaller
object using the setSchema()
method:
SchemaFactory schemaFactory = SchemaFactory .newInstance("http://www.w3.org/2001/XMLSchema"); Schema schema = schemaFactory.newSchema(new File("src/catalog.xsd")); unMarshaller.setSchema(schema);
Create a custom validation handler class, CustomValidationEventHandler...