Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't JAXB want to validate

  1. I wrote some Java classes and annotated them with the JAXB annotations.
  2. After that I used schemagen to generate an xsd.
  3. Then I build an object graph and marshalled it to a xml file.
  4. 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;
   }
 }
like image 704
Michael Schwert Avatar asked Nov 06 '22 10:11

Michael Schwert


1 Answers

Your code should work. A couple of things to look out for:

  • Is the URL for your schema coming back as null?
  • Is your last line a typo "JAXB.unmarshal(file, Customer.class)", or is JAXB another unmarshaller without a schema set on it.

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)
like image 200
bdoughan Avatar answered Nov 12 '22 19:11

bdoughan