How-To call XML APIs from a servlet to manage the XML data, Readme File

XML Servlet Sample Application

Date: 10/03/2004

Table of Contents

Introduction
Application Overview
Code Snippet
Sample Application Files
Setting up the Sample Application
Additional References

Introduction 

Prerequisite

To work your way through this Sample Application, a basic understanding of the fundamental concepts of servlets, JSP, JDBC, XDK APIs, Oracle XML DB are required Additional References.

Technical Overview

XML is language-independent and platform-independent markup language for document containing structured information. XML support has become standard in browsers, application servers, and databases etc. Oracle XML DB fully absorbs the World Wide Web Consortium (W3C) XML data model into Oracle9i database and provides new standard access methods for navigating and querying XML data. Oracle XML DB is not any separate server but rather the name for a distinct group of technologies related to high-performance XML storage and retrieval that are available within the familiar Oracle9i database. Oracle XML DB supports the Java Document Object Model (DOM) API for XMLType. This is a generic API for client and server, for both XML schema-based and non- schema-based documents. Servlets can also be used to call these XML APIs to manipulate XML data stored into Oracle XML DB. To know more about Oracle XML DB/Servlet refer the Additional References section at the end.

Application Overview 

The sample application allows the user to select warehouse id. On submit, for the selected warehouse id, the user will be displayed detailed warehouse information. The warehouse information is stored in an XMLType column. The sample application also allows users to add, modify or delete the warehouse information. To perform all these actions, the XMLServlet sample application uses XDK and Oracle XML DB APIs.

Code Snippet

  1. Reading XML data from the warehouses table
  2. Modify the XML data
  3. Insert an XML data inside XMLType column
Reading XML data from the XMLType column

Following code snippet illustrates, how to use JDBC for the following :
i)   to query a table for XML data,
ii)  to retrieve it as a String object and
iii) to create XMLDocument from the retrieved XML String object.

 
.....
.....
// select query
String query =
"select w.warehouse_id , w.warehouse_spec.getStringVal() "+
"from warehouses w where w.warehouse_id = ?";
//create the prepareStatement object and type cast it to
//OraclePreparedStatement
opstmt = (OraclePreparedStatement)this.conn.prepareStatement(query);
//set the value of warehouse id
opstmt.setInt(1,warehouse_id);
//execute the query and retrieve the resultset
rset = opstmt.executeQuery();

// type cast to oracle result set.
OracleResultSet orset = (OracleResultSet) rset;

// create new object to dom parser
DOMParser parser = new DOMParser();
parser.setPreserveWhitespace(true);

String xmlstr = null;

// if there is any row in the table for given warehouse id
if (orset.next()) {
xmlstr = orset.getString(2);

if (xmlstr != null) {
// parse the xml string
parser.parse(new StringReader(xmlstr));

// Obtain the document.
doc = parser.getDocument();

// get the elements
data = this.getElements(doc);

// add to the collection
coll.add(data);
coll.add(xmlstr);
} else {
coll = null;
}
} else {
coll = null;
}
.....
.....
return coll;

The code snippet for getElement(...) function

Following code snippet shows how to retrieve all the elements and their values from the XMLDocument object.

 
private Object[][] getElements(Document doc) {
// get all the elements
NodeList nList = doc.getElementsByTagName("*");
Node node;

// get the no of elements
int noOfElement = nList.getLength();
Object[][] data = new Object[noOfElement][2];

for (int i = 0; i < noOfElement; i++) {
// get the ith node
node = nList.item(i);

// get the node name and save into data[i][0]
data[i][0] = node.getNodeName();

//get the value of the child node
if (node.getChildNodes().item(0) == null) {
// if it is null, than set data[ i ][ 1 ] = a blank string
data[i][1] = "";
} else {
// set the value of value of that node to data[ i ][ 1 ].
data[i][1] = node.getChildNodes().item(0).getNodeValue();
}
}

// return the element and its value as a Object[][]
return data;
}

Modify the XML data

Following code snippet shows how can you modify the XML data.

  for (int i = 1; i < data.length; i++) {
// create update query
updateQuery = "UPDATE warehouses SET warehouse_spec = " +
"UPDATEXML(warehouse_spec, '/Warehouse/" + data[i][0] +
"',XMLType('<" + data[i][0] + ">" + data[i][1] + "')) where warehouse_id = ?";

// where the structure of the data is like
// <Warehouse>
// <Building>Owned</Building>
// <Area>25000</Area>
// <Docks>3</Docks>
// <DockType>Rear load</DockType>
// <WaterAccess>N</WaterAccess>
// <RailAccess>Y</RailAccess>
// <Parking>Lot</Parking>
// <VClearance>30 ft</VClearance>
// </Warehouse>
// create reference to OraclePreparedStatement by using updateQuery
opstmt = (OraclePreparedStatement) conn.prepareStatement(updateQuery);

// set the value of warehouse_id
opstmt.setInt(1, warehouseId);

// execute the query
opstmt.executeUpdate();
}


Insert an XML data inside XMLType column

Following code snippet shows how to insert XML data into the XMLType column.

 
String xmlstr = "INSERT INTO warehouses (warehouse_id , WAREHOUSE_SPEC) VALUES
(144 , XMLType('<Warehouse>
<Building>data1</Building>
<Area>data2</Area>
<Docks>data3</Docks>
<DockType>data4</DockType>
<WaterAccess>data5</WaterAccess>
<RailAccess>data6</RailAccess>
<Parking>data7</Parking>
<VClearance>data8</VClearance>
</Warehouse>'))";

// get the OraclePreparedStatement object for the query

opstmt = (OraclePreparedStatement) conn.prepareStatement( query );

// execute the query to insert dummy data
opstmt.execute( );


Sample Application Files 

Readme file and Stylesheets

Directory File Description
XMLServlet/doc/ Readme.html This file gives introduction to the sample.
Install.html This file has instructions required to install the software and run this sample.

Java Server Page files for the Application

Directory File Description
XMLServlet/web/ addSuccess.jsp This page will displays the message to show, if the warehouse information is added successfully or not.
addWarehouse.jsp This page will ask user to enter the warehouse details.
deletePage.jsp This page will display the message, if the warehouse details are deleted successfully.
display.jsp This page displays the warehouse detailed information for the given warehouse id.
errorHandler.jsp The JSP displays error page.
footer.html This is the page containing the footer information.
header.html This is the page containing the header information.
modifyFailure.jsp If the XML data not modified successfully, then this page is displayed to display the failure message.
notFound.jsp This page displayes warning message when the warehouse details for the selected warehouse id cannot be found.
successPage.jsp This page displayed the success message, when the warehouse details are modified successfully.
welcome.jsp This is a welcome page where user will be asked to enter
warehouse id to see the detailed information for that warehouse id. Also user can click on Add New button to enter new warehouse details.

ANT build Script

Directory File Description

XMLServlet

build.xml This is an ANT build script.
build.properties

Contains the parameters used by the ant to run the build.xml. user need to modify this property file.

Configuration files for the Application

Directory File Description
XMLServlet/src/META-INF application.xml Contains application information.
data-sources.xml Contains the data source information.This file will be used while deployeing the sample by using ant.
orion-application.xml Contains the OC4J specific information.

 

Java Source files for the Application

Directory File Description
XMLServlet/src/oracle/otnsamples/servlet/xdk/ ConnectionDb.java This class provides actual implementation to create and close database connection.
ConnectionManager.java This interface will be used to get and close the database Connection and provides a layer of abstraction to establish the database connection.
DbInteraction.java This class is responsible to interact with the database and to provide all the necessary data to servlet from the database.
XMLServlet.java This is a HttpServlet, responsible to call different functions defined in DbInteraction.java bean to add , modify or delete warehouse details.

 

JDeveloper project files.

Directory File Description
XMLServlet/ XMLServlet.jpr JDeveloper Project File
XMLServlet.jws JDeveloper WorkSpace File
XMLServlet.deploy Deployment File

 

Setting up the Sample Application 

Refer to Install.html for step-by-step instructions on copying files, installing packages, and other configurations to successfully run the sample.

Additional References

We hope you find this README file helpful. Please enter your comments about this sample in the OTN Sample Code Discussion Forum.

XML Servlet Sample Application

Please rate this sample application :
Excellent
Good
Average
Below Average
Poor



E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy