Legal | Privacy

Generating Large XML Documents from Databases

This module describes how to easily and efficiently generate very large XML documents from a database query using the Oracle XDK Java Components

This module will discuss the following topics:

Place the cursor on this icon to display all screenshots. You can also place the cursor on each icon to see only the screenshot associated with it.

What is XML?

XML stands for eXtensible Markup Language. It is a markup language similar to HTML. XML is a markup language for structuring data rather than formatting information. You use XML to create a document that contains structured data that can be used or interpreted by other applications. The format or structure is straightforward and can be used by any person or program that can read text.

The Benefits of Using XML

One significant benefit of XML is that you can exchange data between applications in a simple, nonproprietary format. Because the language is extensible, you create tags that are specific to your business. For example, your document may contain tags to structure information about books. Those tags may include <title>, <author>, and <desc>. With these tags, other applications can parse the document for specific information, ignore some information, or even search for a specific title.

<?xml version='1.0'?>
   <LIBRARY>
    <BOOK> 
     <TITLE>Is This Your Child</TITLE>
     <AUTHOR>Doris Rapp M.D.</AUTHOR>   
     <DESC>Discovering and treating unrecognized allergies 
           in children and adults.</DESC>
   </BOOK>
   </LIBRARY>

XML and Parsing

In order to programmatically access an XML document, you need to parse it.  There are several standards-based ways to do so with the most common one being DOM (Document Object Model).  DOM parsing is a W3C standard and while available in the Oracle XML Parser, it is not an optimum choice when working with large XML documents because it constructs an in-memory document tree that can be more than 10 times the size of the original document.

There is an alternative to DOM called SAX (Simple API for XML), which is an event-driven form of parsing and thus consumes very few resources regardless of the document size.  SAX parsing is also available in the Oracle XML Parser.  While SAX parsing does not support in-memory manipulation of a document, it can effectively be used to do simple transformations that do not require navigating backwards or significant structure change of the entire document.

Obviously, preparing a document to be printed is a simple transformation that is well suited for SAX parsing.  Instead of requiring you to create the necessary event handlers to implement printing, the Oracle SAX Parser provide this new class built-in XMLSAXSerializer.

XML, Databases and the XDK

The ability to extract data with its meta-data providing the context is a common XML function.  Unfortunately, from a programmer’s viewpoint, this data can many times consist of hundreds if not thousands of rows that then need to be encapsulated into XML.  The standard way to implement this would be to use the DOM to construct this document, however because of the need to construct it entirely in memory before it could be serialized, this approach is not practical.

The Oracle XDK includes the XML SQL Utility, which simplifies the extraction of the data already tagged in XML, and with its ability to generate SAX events as its XML output along with the new XMLSAXSerializer class makes this implementation easy and efficient.

Before starting this module, you should have:

1.

Completed the Configuring Linux for the Installation of Oracle Database 10g lesson

2.

Completed the Installing the Oracle Database 10g on Linux lesson

3.

Completed the Installing Oracle9i JDeveloper on Linux lesson.


Having installed Oracle Database 10g as part of the pre-requisites, you will have installed the XDK.  The main part of the XDK can be found at <ORACLE_HOME>/xdk, the Java XDK library (xmlparserv2.jar) and  the Java XSU library (xsu12.jar) in <ORACLE_HOME>/lib.  Oracle's JDBC driver can also be found at <ORACLE_HOME>/jdbc/lib/classes12.jar.

In order to build an XML Application using JDeveloper , you need to perform the following steps:

1.

Create a JDeveloper Project.

2.

Configure the project for XML

Create a JDeveloper Project

You need to create a workspace and project before you start creating the application.

1.

Open JDeveloper.

 

2.

Click File > New . Select Workspace. Click OK.

Move your mouse over this icon to see the image

 

3.

Change the Directory and Filename as easyxml. Make sure Add a New Empty Project is checked . Click OK.

Move your mouse over this icon to see the image

 

4.

Enter the project directory and file name as easyxml and click OK.

Move your mouse over this icon to see the image

 

5.

Once the Project is created you will see it in the System Navigator.

Move your mouse over this icon to see the image

 

Configure the project for XML

You need set the projects classpath to include the XML libraries. Perform the following:

1.

Right click on the easyxml.jpr project and select Project Settings.

Move your mouse over this icon to see the image

 

2.

Select the Libraries entry in the settings navigator.  Here you can configure the CLASSPATH of your project.  In the Available Libraries you might already have entries for the Oracle XML Parser V2 and the Oracle XML SQL Utility.  Since you are using an older version of JDeveloper these are not the 10g libraries and you need to create some new librarys.  Click the New button.

Move your mouse over this icon to see the image

 

3.

Name the library 10gXML and press the Edit button for the Class Path.

Move your mouse over this icon to see the image

 

4.

Click Add Entry.

Move your mouse over this icon to see the image

 

5.

In the Directory Name field enter/u01/app/oracle/product/10.1.0/db_1/lib and press enter.

Move your mouse over this icon to see the image

 

6.

Select xmlparserv2.jar and xsu12.jar and click Select.

Move your mouse over this icon to see the image

 

7.

Click OK to add the jar files to the classpath.

Move your mouse over this icon to see the image

 

8.

Click OK to create the library.

Move your mouse over this icon to see the image

 

9.

You need to also create a library for JDBC. Click the New button again.

Move your mouse over this icon to see the image

 

10.

Name the library 10gJDBC and press the Edit button for the Class Path.

Move your mouse over this icon to see the image

 

11.

Click Add Entry.

Move your mouse over this icon to see the image

 

12.

In the Directory Name field enter /u01/app/oracle/product/10.1.0/db_1/jdbc/lib and press enter.

Move your mouse over this icon to see the image

 

13.

Select classes12.jar and nls_charset12.jar and click Select.

Move your mouse over this icon to see the image

 

14.

Click OK to add the jar files to the classpath.

Move your mouse over this icon to see the image

 

15.

Click OK to create the library.

Move your mouse over this icon to see the image

 

16.

Click OK to close the Project Settings window.

Move your mouse over this icon to see the image

 

You can not retrieve the database data as an XML Document by performing the following:

1.

Download the example source file available here.  Unzip it into the easyxml project directory /home/oracle/jdevhome/mywork/easyxml/easyxml.

 

2.

In JDeveloper select the easyxml project in the navigator and press the add file button in the System Navigator pane.  

Move your mouse over this icon to see the image

 

3.

Browse to the src/oracle/xml/sample directory and add the XSUSAXPrint.java file to the project.

Move your mouse over this icon to see the image

 

4.

You can now look at the source code and see how the database data is retrieved by SAX streaming. You need to make sure that the following import statements are included:

import oracle.xml.parser.v2.XMLSAXSerializer (SAX output)
import oracle.xml.sql.query.OracleXMLQuery (XQL utility queries)

Move your mouse over this icon to see the image

 

5.

You need to set up the JDBC connection to the database.  In this example, you will connect to the SH sample schema assuming the password for SH is "SH".

Move your mouse over this icon to see the image

 

6.

You need to create an instance of the SAXSerializer.

Move your mouse over this icon to see the image

 

7.

To set up the XSU to send out SAX events you call the OracleXMLQuery.getXMLSAX() interface and pass in the SAX content handler as input.  In this example, you pass in the SAXSerializer instance "sample".

Move your mouse over this icon to see the image

 

8.

You are now ready to run the easyxml project. Right click on easyxml.jpr and select Run XSUSAXPrint.java.

Move your mouse over this icon to see the image

 

8.

You should receive an exit code 0 in the log window.

Move your mouse over this icon to see the image

 

In this lesson, you will use the SAXSerializer to print out the query results to a file.  However the SAXSerializer allows you to print out the SAX output into any output stream supported in Java. To print database data as an XML document, perform the following:

1.

You open a FileOutputStream and pass it to the SAXSerializer.

Move your mouse over this icon to see the image

 

2.

Right Click on the easyxml.jpr project and select Project Settings. Select the Runner option. Browse to the /home/oracle/jdevhome/mywork/easyxml/easyxml/src/oracle/xml/sample directory and add the XSUSAXPrint.java file as the run target. Then click OK.

Move your mouse over this icon to see the image

 

3.

Run the program by right clicking the project and select Run easyxml.jpr

Move your mouse over this icon to see the image

 

4.

Take a look at the Run Directory, you will see a large XML document out.xml in the directory. Open the file in your browser to review the contents.

Move your mouse over this icon to see the image

 

 

Place the cursor on this icon to hide all screenshots.

 

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