Class Generation

Class generation in Castor is similar in operation to both JAXB and Zeus; you use a helper class to pass in a set of XML constraints, and the result is source code that can then be compiled and used in your programs. This section details the constraint models that can be used by Castor, as well as the options that Castor makes available beyond what you have already seen in JAXB.


Oddly enough, Castor is exactly the opposite of most available data binding packages. Instead of supporting DTDs and having a fairly immature XML Schema implementation, Castor began with support for XML Schema. Because of the work involved in supporting the ever-changing XML Schema specification, and perhaps because of a lack of interest, Castor has never provided support for class generation from DTDs. If you need to support class generation from DTDs, you will need to use one of the other data binding packages discussed previously.

XML Schema

While DTDs are not supported by Castor, excellent support for XML Schema is included. Because Castor has worked on schemas for quite a while, the project has a rich feature set for dealing with that language. The basic class you will use for schema-to-Java generation is org.exolab.castor.builder.SourceGenerator.


Castor includes a script, sourceGen.bat and sourceGen (for Windows and Unix, respectively), with the Castor download. However, this script has incorrect paths in it and does not work properly. Instead of using this script, simply ...

Get Java & XML Data Binding now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.