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

Creating the Constraints

The first step in getting ready for class generation, as you can see from Figure 3-1, is getting a set of constraints ready to generate classes from. As this isn’t a book on writing XML (and there are plenty of good ones on the subject already), I’m not going to spend time describing how to formulate constraints.

I’ll assume that you’re capable of figuring out how you want your data represented and then using DTDs or schemas or your constraint model of choice to describe that data. I do want to touch on a few points relevant to data binding, and JAXB specifically, though, and then provide several DTDs for working through the examples.

JAXB and DTDs

First, as I’ve mentioned several times, JAXB currently supports only DTDs. From what I can gather from the specification, newsgroups, and mailing lists, this is the plan all the way through the 1.0 final version of the specification and framework. There is a lot of momentum to follow up this release with a “version.next” that does support XML Schema, though.[5] JAXB does support all the features of DTDs, so you should be able to use any DTDs you’ve already developed for your data binding needs.

To get started, I want to present a simple DTD that I’ll use as a starting point for most of the rest of this chapter. Example 3-1 shows that DTD, which represents a simple movie database.[6]

Example 3-1. Movie database DTD

<!ELEMENT movies (movie+)> <!ATTLIST movies version CDATA #REQUIRED > <!ELEMENT movie (title, cast, ...

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