Modeling Databases in XML
Model a database in XML using Java
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 Solution
In the JAXB solution, we
will model the rental property database as an XML document. This process involves the following steps:
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.
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
Rental Property Database Schema
FIELD TYPE
prop_num NUMBER
Name VARCHAR2
street_address VARCHAR2
City VARCHAR2
State VARCHAR2
zip_code VARCHAR2
size_sq NUMBER
bed_count NUMBER
bath_count NUMBER
monthly_rent NUMBER
voice_phone VARCHAR2
fax_phone VARCHAR2
rental_property.mdb
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.
DatabaseField XMLElementName
prop_num <prop_id>
Name <name>
street_address <street>
City <city>
State <state>
zip_code <postal_code>
size_sq <square_footage>
bed_count <bedrooms>
bath_count <bath>
monthly_rent <price>
voice_phone <phone>
fax_phone <fax>
A rental property is described with a root element of <rental_property>, as shown in the following
code:
<rental_property>
<prop_id>1</prop_id>
<name>The Meadows</name>
<address>
<street>251 Eisenhower Blvd</street>
<city>Houston</city>
<state>TX</state>
<postal_code>77033</postal_code>
</address>
<square_footage>500.0</square_footage>
<bedrooms>1.0</bedrooms>
<bath>1.0</bath>
<price>600</price>
<contact>
<phone>555-555-1212</phone>
<fax>555-555-1414</fax>
</contact>
</rental_property>
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.
rental_property.dtd
<!ELEMENT rental_property_list (rental_property)*>
<!ELEMENT rental_property (prop_id, name, address, square_footage,bedrooms, bath, price, contact)>
<!ELEMENT prop_id (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT address (street, city, state, postal_code)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT postal_code (#PCDATA)>
<!ELEMENT square_footage (#PCDATA)>
<!ELEMENT bedrooms (#PCDATA)>
<!ELEMENT bath (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT contact (phone, fax)>
<!ELEMENT phone (#PCDATA)>
<!ELEMENT fax (#PCDATA)>
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.
Ste
p 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.
Figure: Generating Java classes with the JAXB compiler
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.