A Java Sax Parser Example

Objective: Demonstrate how to create and work with a Java Sax Parser in order to extract information from an XML file, and store them into a Oracle database.

 

Context: A company that sells CDs decides to implement a data warehouse to drive its business. They need to use a Java Sax parser to extract information from an XML file and store into a denormalized table created to store sales information.  The example start point of this example is the program called DWSAXParser.java code and the following statement is the command to run the example: C:> java DWSAXParser CDSales.xml

The parser code will , read itThen, after the whole document is read, the parser invokes the SaleManager class to persist the objects into the database, and to retrieve them and display in the screen.  All the codes are listed bellow

 

Steps:
  1. The Java Sax parser opens the xml file, reads its information line by line, and at the same time parse its information temporarary to variables and when the end of a sale is detected these information is transferred to a sale object that is stored in a vector. 
  2. After reading the whole file, the Sax parser creates a Sale Manager object that stablish a connection to the database, then reads the vector and for each object found invokes the method "add" of the SaleManager class.
  3. The objects are stored in the database and then retrieved and displayied in the screen in order to show the whole process of including and retrieving sales information from the database.

 

The following files compose the Java Sax Parser example:

Qty and Type File Name Brief Desrcription
1 TXT file DBProperties.txt  Contains the parameters to connect to the database
1 XML file CDSales.xml Contains the data to be parsed
1 SQL file ctSales.sql  Contains the script to create the table Sales into Oracle database
4 JAVA files DBConnection.java  Establish connection with the database
  Sale.java Contains the definition of class Sale
  SaleManager.java Manage transactions with the database like insert, update, delete
  DWSAXParser.java Read the XML file, parse the data and populate the database

 

Do you want to download the whole example and try it by yourself ??? Go ahead, but don't forget to send me a feedback with your opinion, ok??? Thanks !!! BTW, You must have your own database to work. The parameters I give in the DBConnection class will not work properly with its default values.

The following file contains all the codes :

DWSaxParserFromSiomara.zip (80KB) This zip file contains all the codes and screen shots of the results obtained.

  

Click here if you want to see the
RESULTS
of this example when executed correctly

 

1