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

Elements and Attributes

The most interesting portion of any binding schema is the instruction set for converting elements and attributes in an XML document to their Java equivalents. This is accomplished through the element and attribute elements in the binding schema and is the subject of the next several sections. I begin by covering elements, then move on to the content allowed within elements, and finally discuss attribute definitions.

Elements

The most common construct you’ll use in your binding schemas is the element element (if that’s confusing, it’s the element named element). This element allows you to specify how conversion occurs from an XML element (like the movie element) to its Java class (currently, the Movie class). The complete DTD declaration for the element element is shown here:

<!ELEMENT element ((attribute | constructor | enumeration | conversion)*,
                    content?,
        (attribute | constructor | enumeration | conversion)*) >
<!ATTLIST element
          name    ID              #REQUIRED
          type    (value | class) #REQUIRED
          convert NMTOKEN         #IMPLIED
          class   NMTOKEN         #IMPLIED
          root    (true | false)  #IMPLIED >

I’ll begin by dealing with the allowed attributes and move on to the element’s content. The name attribute is simple enough; it indicates the XML name of the element. In the same fashion, the class attribute allows you to specify an alternate name to be used for the Java class. If you wanted to map the movies element in XML to a class named MovieDatabase, you would use the following schema fragment: ...

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