O'Reilly logo

Java & XML Data Binding by Brett McLaughlin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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*)> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required