5
  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;
   }
 }
5
  • Duplicate: stackoverflow.com/questions/805989/… Commented Jul 25, 2010 at 12:08
  • Of course not. Please take the time and read both questions carefully. Commented Jul 25, 2010 at 12:58
  • as per lexicore exact dupe - voted to close Commented Jul 25, 2010 at 16:38
  • 2
    Sorry, but I think both of you either don't program Java or don't understand this question. It is totally different from then the quoted one. Commented Jul 25, 2010 at 19:24
  • The question is not a duplicate of the one quoted. Can you provide more details as to which implementation of JAXB you are using (Metro, MOXy, etc) and version. Is the last line just a typo, you create an unmarshaller called unmarshaller but then do JAXB.unmarshal? Commented Jul 26, 2010 at 13:14

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

If the URL to the Schema would be wrong there would be a NullPointerException. So this is not the error. But you directed me to the error and I could bang my head against the wall. Thanks! You make my day :) Of course JAXB.unmarshal() doesn't have the context, right is Customer c = (Customer) unmarshaller.unmarshal( file );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.