- I wrote some Java classes and annotated them with the JAXB annotations.
- After that I used schemagen to generate an xsd.
- Then I build an object graph and marshalled it to a xml file.
- I modified the xml file so that it was not valid anymore.
I wanted to use the xsd in the hope the JAXB unmarshalling fails. But it doesn't. Why?
JAXB is reading a schema (if the schema XML is wrong JAXB gives an exception) but it seams that JAXB is ignoring the schema while reading.
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(getClass().getResource( "/schema1.xsd"));
JAXBContext context = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema( schema );
Customer c = JAXB.unmarshal(file, Customer.class);
The written XML starts like that:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:customer xmlns:ns2="http://bla.com/">
Even the attached ValidationEventCollector didn't give any information:
unmarshaller.setEventHandler(new JAXBEventCollector());
JAXBEventCollector is:
class JAXBEventCollector extends ValidationEventCollector
{
@Override
public boolean handleEvent(ValidationEvent event)
{
System.out.println(event.getLocator());
return true;
}
}