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;
}
}
Your code should work. A couple of things to look out for:
Below is a fragment of code that definitely throws errors when invalid XML is unmrshalled. This code works correctly with both the MOXy and Metro (RI) JAXB implementations.
public static void main(String[] args) throws Exception {
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
File xsd = new File("customer.xsd");
Schema schema = sf.newSchema(xsd);
JAXBContext context = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema( schema );
FileInputStream xml = new FileInputStream("invalid.xml");
unmarshaller.unmarshal(xml);
}
With Metro the error looks like:
Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'phone-numbers'. One of '{phoneNumbers}' is expected.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:514)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
at example.gettingstarted.Demo2.main(Demo2.java:23)
With MOXy the error looks like:
Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.0.3.qualifier): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred unmarshalling the document
Internal Exception: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'phone-numbers'. One of '{phoneNumbers}' is expected.]
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:114)
at example.gettingstarted.Demo2.main(Demo2.java:23)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With