JAXB

Document Type Definitions (DTDs)

An XML document can be given a formal specification. There are several methods

The DTD method was the first and is still widely used. It is good at specifying document structure. It is not very good at specifying data types. XML Schema is good at specifying data types, but not so good at structure.

A DTD for login-requests is

<!ELEMENT login-request (name, password)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT password (#PCDATA)>
This says that a login-request must contain a name and a password, and that their values are implicitly strings (#PCDATA).

A schema for login-requests is


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="loginrequest" type="LoginRequest"/>

<xsd:complexType name="LoginRequest">
    <xsd:sequence>
        <xsd:element name="name" type="xsd:string"/>
        <xsd:element name="password" type="xsd:string"/>
    </xsd:sequence>
</xsd:complexType>

</xsd:schema>

XML Schema data types

JAXB

There are a number of ways of handling XML parsing in Java

We will use JAXB: a mechanism for

JAXB "early release" used DTDs. JAXB release 1.0 switched to schema. JAXB 1.0 requires JDK 1.4.1 or later.

The JAXB compiler is xjc and is found in the jaxb/bin directory of Sun's Web services pack. It can be used by


    xjc.sh -p package_dir login.xsd
It generates a bunch of classes including the interface

It also generates an ObjectFactory which includes methods You need to use this factory to load the implementation of the interface - you are not supposed to access the implementation classes directly.

A test program is


This page is maintained by Jan Newmarch http://jan.newmarch.name