Modeling Databases in XML
When we model a database, we provide an
external representation of the database contents.
For our sample program, we’ll utilize a database
that contains information on
rental properties.
We’ll model the rental property
database as an XML document.
JAXB
Java Architecture for Xml Binding
JAXB provides a framework for representing
XML documents as Java objects.
Using the JAXB framework, we can guarantee
that the documents processed by our system are
well formed.
we have the option of validating the XML data
against a schema.
Marshalling is the process of transforming Java
objects into XML documents. Unmarshalling is
the process of reading XML documents into Java
objects.
JAXB
JAXB Process
1. Review the database schema.
2. Construct the desired XML document.
3. Define a schema for the XML document.
4. Create the JAXB binding schema.
5. Generate the JAXB classes based on the schema.
6. Develop a Data Access Object (DAO).
7. Develop a servlet for HTTP access
JAXB Process
Figure: “The rental property application architecture” illustrates
the application architecture. RentalXMLServlet communicates
with RentalDAO to retrieve information from the database. Once
the information is retrieved by RentalDAO, RentalXMLServlet
generates an XML document
Step1 : Reviewing the Database Schema
Step2 : Constructing the Desired XML
Document
The desired output XML document describes
the rental property.
However, the XML document does not use the
exact field names listed in the database
schema.
Instead, the XML document provides a custom
mapping of the database fields to XML
element names
Step3 : Defining a Schema for the XML Document
The DTD schema format was chosen because
JAXB 1.0 (early access) only supports DTDs.
In the future, JAXB is supposed to support the
formal XML Schema definition.
Step3 : Defining a Schema for the XML Document
The DTD schema format was chosen because
JAXB 1.0 (early access) only supports DTDs.
In the future, JAXB is supposed to support the
formal XML Schema definition.
Step 4: Creating the JAXB Binding Schema
The JAXB binding schema is an XML document that contains
instructions on how to bind a DTD to a Java class.
<element name=”square_footage” type=”value” convert=”double”/>
<element name=”bedrooms” type=”value” convert=”double”/>
<element name=”bath” type=”value” convert=”double”/>
<element name=”price” type=”value” convert=”BigDecimal”/>
<conversion name=”BigDecimal” type=”java.math.BigDecimal”/>
The schema files normally use the filename extension .xjs (for
XML Java schema).
Step 5: Generating the JAXB Classes Based
on Schemas
JAXB provides a schema compiler for generating the
Java source files.
The schema compiler takes the DTD
(rental_property.dtd) and binding schema
(rental_property.xjs) to the JAXB schema compiler
with the xjc command
Step 6: Developing a Data Access Object (DAO)
A Data Access Object (DAO) provides access to the backend
database.
Application interacts with the DAO using method call for a list
of objects from the database
The DAO is then interacted the data base using SQL queries
and received the result set
The DAO converts a result set to a collection of object which is
then sent to the application.
Step 7: Developing a Servlet for HTTP
Access
The DAO is capable of retrieving information
from the database and provides the collection
of objects to the sevlet
The Servlet converts the objects into XML
using marshaling