This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
I agree. Here's the link:
subject: XML Validation using JAXB-2.0
Similar Threads
Axis2 POJO web service - wsdl generating xs:anyType
@WebService method returning a custom class containing a HashMap / JAX-WS
returning a 2-d array
How to make Axis2 bottom-up webservice make minOccurs="1" nillable="false"
annotations on entity class with spring oxm and jaxb for ArrayList