back to my homepage
Final Project for XML and Java
CPS616 Final Project
Author: Dawei Wang
Date: May 1 1999.
Copyright by Dawei Wang. 1999-2000. All Rights Reserved.
Index of my project
About the project
Short introduction to XML
What is XML
What is DOM

Overview

Step 1: Converting an XML document to a Java Object model

Step 2: Working with the Java object model Step 3: Generating an XML document form a Java object model

Working with Servlets

Source Code


go to topAbout the project
In this tutorial, we will create an XML document, the contents of which can be accessed using a Servlet and from a web browser. The XML document is a very simplistic representation of an address book which stores the name, email and company name of people. The XML document is written manually, and its structure is known by the the Servlet. This project shows how Java can be used to display information in XML documents using a graphical Swing interface and an HTML based interface. This project demonstrates the simple power of XML, the information is the most important element of the equation. Information rendering engines (user interfaces) can be swapped out as is appropriate for the display device. All these rendering engines all work on the same XML document. Also, XML Parsers from different vendors can be used without making any major changes to the source code, which is another feature of using a standards based information storage format.

My source code bundles use the IBM XML Parser for Java. If you want to know more about IBM XML Parser, you can go to the IBM XML web site and download it.

go to topShort Introduction to XML
What is XML?
XML is described very well in IBM's XML Tutorial for Programmers The following paragraph is a summary of what is said there. XML uses markup tags as well, but, unlike HTML, XML tags describe the content, rather than the presentation of that content. By avoiding formatting tags in the data, but marking the meaning of the data itself with custom user definable tags, we actually make it easier to search various documents for a tag and view documents tailored to the preferences of the user. Using XML tags to define what your data means using the natural vocabulary of your data's domain is the key motivation for XML's invention and the basis of its usefulness. XML data can be rendered differently, depending on the destination device. The XML processor can exist on the server, the client, or both.
What is DOM?
The Document Object Model (DOM) is a set of platform and language neutral interfaces that allow programs to access and modify the content and structure of XML documents. This specification defines a minimal set of interfaces for accessing and manipulating XML document objects. The Document Object Model is language neutral and platform independent. A DOM object is used to extract information from an XML document in a Java program (using a Java XML Parser).

go to topOverview
There are three main steps we have to go through when working with XML and Java. They are: converting an XML document to an Java object model, working with the Java object model, and generating (saving) an XML document from the Java object model.

These steps are shown below:

overview

Step 1: Converting an XML document to a Java object model This step involves using an XML parser and DOM. Using the DOM API we will create our own a Java object model. In this tutorial, an XML address book document is converted to a Java object model for an address book.

Step 2: Working with the Java object model The contents of the address book are stored in the object model and this model is used to add, edit, or delete information (about persons).

Step 3: Generating an XML document from the Java object model The Java object model for the address book is saved as an XML document.


go to top Step 1: Converting an XML document to a Java object model

The DOM API is used to read in information from an XML document. DOM can also be used to change this information, but using DOM is very tedious. DOM can also be used to generate an XML document. There is an easier way of getting around using DOM for modifiying and saving the XML data; by creating a Java object model for the information in the document, you can create this object model by giving it a DOM object that holds all the XML document information. That is, the XML document should be mapped to a Jave object model.

The ADDRESSBOOK element is mapped to the AdressBook class, the PERSON element is mapped to the Person class. The AddressBook class is simply a container for Person objects.


The Person class maps the PERSON element to a class and is a container for the LASTNAME, FIRSTNAME, COMPANY, AND EMAIL elements. The LASTNAME, FIRSTNAME, COMPANY, AND EMAIL elements are mapped to simple Strings.


Creating the Java object model
Converting AddressBook XML document to an AddressBook object is done by the IoUtils class. The getAddressBook()method returns an AddressBook object when it is called with an XML filename and path.


go to top Step 2: Working with the Java object model

Now that we can change the information in the AddressBook XML document into an AddressBook object, we don't need to use DOM anymore. It is very simple to modify the AddressBook object; it is also trivial to create and modify Person objects. The AddressBook contains an ArrayList object (which holds all the Person objects).

Adding new information to the AddressBook
The following code adds a Person object to an AddressBook:

AddressBook addressBook = new AddressBook();
Person person = new Person();
person.setLastName("Wang");
person.setFirstName("Dawei");
person.setCompany("Syracuse University");
person.setEmail("dawang@mailbox.syr.edu");
addressBook.add(person);


Editing the AddressBook
To edit the 2nd Person object in the AddressBook, we need to get a Person object in index 2 of the AddressBook object (which is a container of Person objects).

Person person = addressBook.get(2);
Then with the person object reference, you can change the contents of this person object. Once you have finished your modifications, you can put the person object back in the AddressBook object by using:
addressBook.set( 2, person);


Deleting a person from the AddressBook
To delete the 2nd Person object from the AddressBook object, we simple tell the addressbook to remove the 2nd object it contains:

addressBook.remove(2);


go to top Step 3: Generating an XML document from a Java object model

Generating an XML document is exactly the same as writing a text file. The object model has to be manually covered to text using all the appropriate tags. DOM can generate XML text based on the intformation contained in it, but since we didn't use DOM to allow modifications on the AddressBook, we have to manually genereate the XML ourselves. It is actually easier to do it this way, becuase DOM is very messy to manipulate.

We first need to create a new text file using FileOutputStream, we can write Strings to this file using a PrintWriter. Since we know how the Java object model is structured, we can simply add a tag before and after every element. The AddressBook object contains multiple Person objects, so we need to use loop to extract each Person object.

As you might already know, a DTD does not need to be included in XML file. In this example however the DTD is included in the XML file.

go to topWorking with Servlets
OpenServlet class
This servlet displays a text input field and button. When the button is pressed, a GET request is sent to AddressBookServlet with the String entered in text field.


AddressBookServlet class
This servlet does all the work displaying AddressBook and manipulating it.

The doGet()method is used to display the ocntents of the AddressBook.

Teh doPost()method does all the work for adding, editing, or deleting person objects from the AddressBook object. The POST requests made to the AddressBookServlet must be given anactioncommand. In the doPost()method of the AddressBookServlet calls the appropriate method based on the action's name.


Refreshing the page
In your browser, when you are looking at the AddressBookServlet HTML page, and you try to edit or delete and invalid row number, a message appears (in a new HMTL page) saying that you need to enter the correct row number for a short amount of time. This page then dissapears and the AddressBookServlet page is shown again.

go to topSource Code