• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validate POJOS with an XSD schema?  RSS feed

 
Philippe Desrosiers
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Believe me, this is not my idea.

That said, I have a simple POJO, say a Customer object. And I want to validate this object against an XSD schema (say, "customer.xsd"). This schema will have an element defined for each public readable property of the Customer object.

Is there a framework or method that already does this?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the XMLEncoder/XMLDecoder classes to serialise your object into an xml file and then validate that with the xml schema
 
Philippe Desrosiers
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:
You could use the XMLEncoder/XMLDecoder classes to serialise your object into an xml file and then validate that with the xml schema


Thanks Joanne! I don't have control over the XSD schema, though. Will this strategy allow me to tailor the serialization to conform with the schema?

The other thing is that I've got about fifty of these classes to worry about, and, as this is fairly early in the development cycle, the customer is likely to change the XSD schemas very often...
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Philippe,

In short yes, as long as your solution generically converts your POJO to XML to then be tested then it will work just fine as the XSD changes. Naturally you may find that validation fails if your POJO doesn't keep up with the XSD expectations. It may be worth versioning your POJO and your XSD together.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!