Wifsimster
Wifsimster

Reputation: 137

how to detect unexpected element in jaxb

I use JAXB 2.2.7 to get data from my XML files, the problem is I need to check if there are some unexpected element in my XML files but JAXB don't care and work perfectly without any error.

Example :

    <classe> 
  <detail>
        <knowElementFromXSD>value</knowElementFromXSD>
        <unknowElementFromXSD>OtherValue</unknowElementFromXSD>
  </detail>
</classe>

For example I need JAXB to throw an error for the in this XML file because isn't define in my XSD.

If someone have an idea, I can't find any solution on the internet.

Upvotes: 2

Views: 2060

Answers (1)

bdoughan
bdoughan

Reputation: 149047

There are a couple of different ways to accomplish your use case:

Option #1 - ValidationEventHandler

You can set a ValidationEventHandler on your Unmarshaller to be notified of things like unexpected elements. By default a JAXB (JSR-222) implementation will just ignore unmapped elemeents.

Option #2 - Schema Validation

If you set an instance of javax.xml.validation.Schema on the Unmarshaller then the XML input will be validated as it is unmarshalled.

Upvotes: 3

Related Questions