rocket mass heater kickstarter
The moose likes Web Services and the fly likes XML Validation using JAXB-2.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "XML Validation using JAXB-2.0" Watch "XML Validation using JAXB-2.0" New topic

XML Validation using JAXB-2.0

carlo sciandrone

Joined: Feb 26, 2012
Posts: 5
Hi everybody!!
I have this requirement to fulfill: validate an XML against an Xml Schema ignoring certain attributes.
XML Tutorials suggest the usage of the <xs:anyAttribute> tag for this kind of requests. I've seen that JAXB 2.0 has support for this feature.
So I've annotated my class with a property of type Map<QName,String> and annotation @XmlAttribute.
Then I generated by schemaGen the .xsd (which I pasted below)
To insure validation in my reader class I'm doing this:

If I try with an Xml WITHOUT the added attribute the validation goes well, but if I add a random attribute to my element JAXB complains telling me that the attribute is not defined in the .xsd ("Attribute 'xxxx' is not allowed to appear in element 'modello'")
If I try the validation with tools like Xml spy everything goes fine, so what am I doing wrong?

Here is my xsd definition

And here is my Xml File, where attributeToIgnore is the attribute I added to test validation (if I remove it everything works fine)

Here is my java class

And here is the java class

Thanks a lot
subject: XML Validation using JAXB-2.0