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: ...

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.