An XML document can be given a formal specification. There are several methods
A DTD for login-requests is
This says that a
<!ELEMENT login-request (name, password)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT password (#PCDATA)>
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>
There are a number of ways of handling XML parsing in Java
We will use JAXB: a mechanism for
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