 | Level: Intermediate Joan Haggarty, Staff Software Developer, IBM Toronto Lab
05 Feb 2002 This is Part 3 of a series that focuses on the XML tools provided with WebSphere Studio Application Developer. Part 3 focuses on the XML tools provided with Application Developer, and discusses the features available to incorporate data access and XML in your application.
Introduction IBM ®
WebSphere® Studio Application Developer is an application
development product that supports the building of a large spectrum
of applications using different technologies, such as JSPTM,
servlets, HTML, XML, Web services, databases, and EJBs. In particular,
Application Developer provides tight integration between XML and
relational data. Application Developer supports all of the databases that WebSphere
Application Server supports, including DB2®, Oracle, Sybase, and Microsoft® SQL
Server. This is Part 3 of a series of articles focusing
on the XML tools provided with Application Developer. Part 3 discusses
the features available to incorporate data access and XML in your
application. Using the SQL query that was created in Part
2, I will now demonstrate how to do the following using Application
Developer. In the section, "Generate XML and related files from an SQL
statement with the XML from SQL Query wizard," you will see how the SQL to XML
generation tool that is included with Application Developer allows you to easily
generate XML, DTD, XSD, XSL, and query template (XST) files from SQL queries.
In the section, "Deploy the sample servlet to WebSphere Application
Server from a Web project," I describe the dynamic use of SQL queries
to generate XML within a servlet and show how to deploy the servlet
to WebSphere Application Server within Application Developer. An
SQLtoXML JavaTM class library is shipped with Application Developer;
you can use this class library in your Java application or servlet
to execute your SQL statements and construct the results as XML.
The generated XST file is used to execute the SQL statement at run
time in WebSphere Application Server using the XMLIntegratorServlet
sample. You can modify this servlet for use in your own application. We recommend that you read the other articles
in this series:
- Part 1 Learn
how to use Application Developer to develop XML Schema.
- Part
2 Learn how to create an SQL query using Application Developer's
SQL Builder.
- Part
3 Learn about the Application Developer features available
to incorporate data access and XML in your application.
- Part 4
Learn how to use the XML Editor, a visual tool for creating and
editing XML documents.
- Part 5
Learn how to use the RDB to XML Mapping Editor to create DAD files
for use with DB2 XML Extender.
- Part 6 Learn
how to use the XML Schema Editor and the XML Editor together to
develop XML applications that make use of XML namespaces.
Scenario and sample query The sample application we will be working with is a retail
video store application. The application allows the store employees to create,
maintain and query customers, inventory and rental information. The store database
holds data related to the store customers, inventory, video rentals and employees.
The application requires a query that allows the store employees to look up
the video titles that a particular customer has rented and what day of the week
they are due. The query takes a customer name as a parameter at run time. The
query looks as follows in the SQL Builder: Figure 1. The query as it appears in the SQL Builder

Generate XML from an SQL query In this section, I will demonstrate how to generate XML and
related files based on the customer rentals query that we have just created.
The XML from SQL Query wizard is used to accomplish this:
- Right-click on the customerRentals statement in the Data view and
select Generate new XML from the pop-up menu. The XML From SQL wizard
opens as shown in Figure 2 below.
Figure 2. The XML from SQL Query wizard

- Select to show the result table columns as elements in the XML that is generated.
In addition to the XML document, we will generate a DTD definition for it.
As you can see in Figure 2 above, the schema definition generation is optional
and can be an XML Schema instead of a DTD if you choose.
- Next, select to generate a query template file (XST script). This will later
be used by the video store Java application to query a database and retrieve
the results as XML. Click Finish.
- Switch to the Navigator view and observe the generated HTML, XSD,
XST, XSL, and XML files. You can use the XML tools to work further with these
files. For example, you could trace through the generated XSL file with the
XSL Trace tool by applying it to the XML file. To invoke the XSL Trace tool,
select both the XSL and XML file, and select Apply XSL => As XML from
the pop-up menu. You can use the XML Schema editor, HTML editor, or the XML
editor to browse or edit the XSD, HTML, and XML files respectively.
Applying the XSL file to the XML file as HTML or viewing the HTML file gives us
the following results:
| Title | Name | Due Date | | The Adventures of Kernighan and Ritchie | John Doe | Thursday | | The Taming of the Foo | John Doe | Thursday |
Using a query template file in a servlet This section describes how queries can be executed at run
time to dynamically generate XML from SQL. Snippets of code are presented to
give you an idea of how to incorporate this into your own servlet. A sample
servlet and HTML file are shipped with Application Developer that you can modify
for your own purposes. They use the XST file and code described in this section.
For an example of how to use the sample servlet, see the following section on
deploying the sample servlet.
You can use the query template (XST) file created with the
SQL to XML wizard in the video store application at run time to dynamically
generate XML from SQL. To do this, use the SQLtoXML Java class shipped with
Application Developer. The generated XST file contents are as follows. The XST
file provides the SQLtoXML class with database connection information, the SQL
query and the options you selected to generate the XML result. <?xml version="1.0"?>
<SQLGENERATEINFORMATION>
<DATABASEINFORMATION>
<LOGINID>JOAN</LOGINID>
<PASSWORD><
Import application sample files To use the sample application files, we need to import them
into the Web project:
- From the menu, select File => Import. Select to import from File
System, and click Next.
- Browse to the
plugins\com.ibm.etools.sqltoxml\samples\was4.0\
directory.
- Expand the directory tree and select the webApplication check box.
- In the Folder field (this is the destination directory for the import),
enter VideoWeb. Click Finish.
- Select Yes to overwrite
web.xml with the imported copy.
Modify HTML and servlet to point to our application Some paths in the sample code need to be modified to match
the path for the VideoWeb project. Namely, the context root for the POST command
in the HTML and the servlet variable path in the servlet. First, we will modify
the path for the context root:
- In the Navigator view, expand the webApplication view and find the
XMLIntegrator.html file.
- Right-click on the
XMLIntegrator.html file,
and select Open with => Source Editor.
- The HTML source is now displayed in the editor.
- Change the FORM tag:
<FORM name="sqltoxmlsample" METHOD="POST"
ACTION="/sqltoxml/servlet/XMLIntegratorServlet"> |
to:
<FORM name="sqltoxmlsample" METHOD="POST"
ACTION="/VideoWeb/servlet/XMLIntegratorServlet"> |
- Save the file by selecting File => Save
XMLIntegrator.html.
Next, we will modify the XMLIntegratorServlet SERVLET variable:
- In the Navigator view, expand the VideoWeb project source directories
until you find the
XMLIntegratorServlet.java
file.
- Double-click on
XMLIntegratorServlet.java
to open the file in the Java editor.
- Find the line:
private static final String SERVLET =
"/sqltoxml/servlet/XMLIntegratorServlet"; |
and change it to:
private static final String SERVLET =
"/VideoWeb/servlet/XMLIntegratorServlet"; |
- Save the file by selecting File => Save
XMLIntegratorServlet.java.
Modify the servlet and HTML to accept a parameter The sample XMLIntegrator.html
file has one input field to get the name of the XST file from the user. The
customerRentals query has a parameter that also requires customer input: the
customer name. To modify the HTML file so that there is an input field for the
parameter, do the following:
- If the file is not already open, open it by right-clicking and selecting
Open with => Source Editor.
- Look for the line:
<input type="Hidden" name="params" VALUE="" SIZE=50 MAXLENGTH=50> |
- Add a label, change the type to Text, and put the label and text
in a table row as follows:
<tr>
<td>
<B>Customer Name:</B>
</td>
<td>
<input type="Text" name="params" VALUE="" SIZE=50 MAXLENGTH=50>
</td>
</tr> |
- Save the file. The XMLIntegratorServlet already has code to process parameters
so this is the only change we need to make.
Import the SQLToXML JAR file into the WEB-INF directory At run time, the SQLToXML JAR file will be needed to access
things like the QueryProperties class that was described in the last section.
To make it available at run time, the JAR file needs to be in the Web project's
WEB-INF directory. Import the JAR file as follows:
- In the Navigator view, select the lib directory under
VideoWeb\webApplication\WEB-INF.
- From the menu, select File => Import.
- Select to import from the File system and click Next.
- Browse to
plugins\com.ibm.etools.sqltoxml\jars\
under the directory where Application Developer is installed.
- Click on jars in the left pane. In the right pane, check the
sqltoxml.jar
check box.
- Set the destination folder to be
VideoWeb/webApplication/WEB-INF/lib.
- Click Finish. The JAR file appears in the
lib
directory for the Web application.
Run the application on the server using the
query template file Now we are ready to run the application on the server. I will
show you how start the server and then how to run the query using the query
template file.
- Right-click on the
XMLIntegrator.html file
in the Navigator view.
- Choose Run on Server from the pop-up menu. A publishing message appears
followed by a message that the server is starting. The Server perspective
now opens.
- Messages in the Console at the bottom of the Server perspective show you
what is happening with the application server. You will see messages indicating
that the server is starting.
- Once the server is running, the
XMLIntegrator.html file will
appear in the Application Developer Web browser. See Figure 4 below.
Figure 4. The XMLIntegrator.html file as it appears in the
Application Developer Web browser

- In the entry field, enter the location of the
customerRentals.xst
file that was generated earlier.
- Enter the parameter
'John Doe' for the query in the parameter
entry field.
- Click Submit to submit the query. You will see the results in the
Web browser as shown in Figure 5 below.
Figure 5. The results of the query displayed in the Web browser

To stop the server when you are finished running the application:
- Click the Servers tab in the console.
- Right-click on the running server, and select Stop from the menu.
Summary In this article, I discussed the support that WebSphere Studio
Application Developer provides for using SQL with XML in a Web application.
You have seen how to use Application Developer to:
- Generate XML from an SQL query and other XML-related files: XSL, DTD, XML
Schema and query template file.
- Dynamically generate XML from SQL queries at run time using the generated
query template, DTD and the SQLToXML class library within a servlet.
- Deploy this servlet from a Web project to WebSphere Application Server using
the sample servlet.
In future articles, you will be shown the integration of the
SQL Builder with other XML tools. For example, the creation of DAD files using
the RDB to XML mapping tool and the use of DAD files for DADX Web services.
Download | Name | Size | Download method |
|---|
| createVideoStore.zip | 3 KB | FTP | HTTP |
About the author  | |  | Joan Haggarty is a Staff Software Developer at the
IBM Toronto Lab. She is a member of the XML tools team for WebSphere Studio
Application Developer, focusing specifically on the SQL Builder. You can contact
Joan at joan@ca.ibm.com. |
Rate this page
|  |