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.
There are a couple of different ways to accomplish your use case:
Option #1 –
ValidationEventHandlerYou can set a
ValidationEventHandleron 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.Schemaon theUnmarshallerthen the XML input will be validated as it is unmarshalled.