And More...

As you saw in the section on elements, several constructs are allowed in binding schemas that are often used throughout a binding schema. These become, essentially, global variables for the schema. They are defined at the top level of a binding schema (nested just within the xml-java-binding-schema element). I’ll cover each in turn in this section, addressing them in relation to the constructs you’ve already seen.

You should keep in mind that these are helper constructs. They are useful only when referenced by other elements and attributes. You should take care not to clutter up your binding schemas with enumerations, conversions, and the rest that are not used by other elements and attributes in the schema. This will keep your schemas concise, as well as easily maintainable.

Enumerations

An enumeration is useful when you need to constrain the set of allowed values for an attribute or element. This is particularly useful when your DTD already has these constraints in place; JAXB does not do anything by default to enforce these constraints in your generated classes, but you can add this functionality by using the enumeration element. The definition for this element is shown here:

<!ELEMENT enumeration EMPTY >
<!ATTLIST enumeration
          name    CDATA    #REQUIRED
          members NMTOKENS #REQUIRED >

To see this definition in action, add the following attribute to the movie definition in your movies.dtd constraint set:

<!ELEMENT movie (title, cast, assistants, crew, director?, producer*)> ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.