Popular Posts

Wednesday, June 24, 2009

XML Data File 2 Java Bean Class

This time I am creating a J2EE application which requires back and forth retrieval of data values from a XML data file. I decide to write a separate class file which will only read the XML file and load its values into the memory. The usual way will be to use a XML parser but I decided to go with JOX Java Library.

JOX Introduction:

JOX matches an XML document to the fields of a java bean and will use a DTD when writing an XML document if one is available. Because JOX identifies the Java Bean at runtime, you can transmit XML into different classes.

JOX limitations:

  1. You must use Java Bean’s getter/setter methods because JOX uses introspection to understand the property names.
  2. XML tag names must match bean property names.
  3. JOX converts XML data to the type of the bean property.
  4. Without a DTD, JOX uses bean property names as XML tag names.

Note: you can also use JOX to write a bean to a DOM object so that it can be used for XSLT processor later on.

JOX example:

Now I will walk you through a live a example of How I used JOX for my party invitation generator application. First take a look at the XML data file which is being created by my application when a user created a party:

   1:  <?xml version="1.0" ?>
   2:  - <partyInvitation>
   3:    <date>2009.May.11 12:48 PM</date> 
   4:    <hostname>testtest</hostname> 
   5:    <password>testtest</password> 
   6:    <inviteMessage>test</inviteMessage> 
   7:    <guestname>test</guestname> 
   8:    <guestemail>test@gmu.edu</guestemail> 
   9:    <gueststatus>No Status</gueststatus> 
  10:    <guestid>0</guestid> 
  11:    <guestname>test2</guestname> 
  12:    <guestemail>test2@gmu.edu</guestemail> 
  13:    <gueststatus>No Status</gueststatus> 
  14:    <guestid>1</guestid> 
  15:    <RiazG00444898 /> 
  16:    </partyInvitation>


As you can see on line 5 the password field is not encrypted in the file which is not a good approach but because of the testing limitations of this application I have to save it in a simple text form. At least at the bear minimum you can generate the hash code of the field before saving it to the file.

For the purpose of this article I will retrieve host name, guest emails and id values as these three belongs to different data types. Host name will always be a string, guest emails will contain more then one value so ArrayList will be use here and id will be integer type. And also I handled ArrayList values differently so that's why I am discussing it here. So lets get started by creating a Java Bean class with respective property names.

   1:  public class PartyDataBean {
   2:   
   3:      private static String hostname;
   4:      private static int guestid;
   5:      private static ArrayList<String> guestemail = new ArrayList<String>();
   6:      
   7:      public void sethostname(String name) {
   8:          hostname = name;
   9:      }
  10:   
  11:      
  12:     public void setguestemail(String email) {
  13:   
  14:          guestemail.add(email);
  15:      }
  16:   
  17:     
  18:      public void setguestid(int n) {
  19:   
  20:          guestid = n;
  21:      }
  22:   
  23:      public String gethostname() {
  24:          return hostname;
  25:      }
  26:   
  27:      
  28:      public String getguestemail() {
  29:          return guestemail.toString();
  30:      }
  31:   
  32:      public int getguest() {
  33:   
  34:          return guestid;
  35:      }
  36:  }

As you can see from from line 29 I am returning the all the values of the array without any loop which will return the values in this format: [test@gmu.edu,test2@gmu.edu] so you need to tokenize this string if you want to display these two values separately. The other possible way can be with a loop but then you are making more one function call which can effect your applications respond time as in large applications you have more then one value to retrieve.

Packages Needed:

import com.wutka.jox.JOXBeanInputStream;
import java.io.FileInputStream;

You need the above two packages in order to use JOX. The necessary JOX library files can be downloaded from http://sourceforge.net/projects/jox and make sure you put jox116.jar file into classpath so that the java code can find corresponding classes at compile and run time.

JOXBeanInputStream is An InputStream filter that reads XML into a bean. When you read an XML document, you must supply either a class or an object instance. The input stream will attempt to match XML tags to bean attributes in the class/object you supply.If you supply a class, the input stream will automatically create a new object instance to hold the data.

The stream understands the basic Java data types and their object equivalents, plus strings and dates. Anything else must be a bean. It can also read arrays of any of the supported types or of beans if it tries to read a bean with an indexed property.

If there are XML fields that don't match the bean, it will ignore them. If the data types are not compatible, you will get an exception.

Useful functions:

You only need to provide the XML data file path to JOXBeanInputStream object and then just read the data bean at runtime by providing the class file of that data bean to JOXBeanInputStream object. But before that make sure that the XML file does exit otherwise you will get an exception. Here is how I did it with the help of these functions give below:

public Boolean FileExits(String filename) {
   File file = new File(username);
   boolean exists = file.exists();
   if (!exists) {
   // It returns false if File or directory does not exist
    return false;
   } else {
    // It returns true if File or directory exists
    return true;
   }
 }
public Boolean LoadFile(String filepath) {
  try {
   FileInputStream fIn = new FileInputStream(filepath);
   JOXBeanInputStream joIn = new JOXBeanInputStream(fIn);
   testBean = (PartyDataBean) joIn.readObject(PartyDataBean.class);
    return true;
    } catch (Exception ex) {
    ex.printStackTrace();
    }
      return false;
}
 

It is a good practice if you make sure and checks the resource does exit before pointing to it.

That’s all for today and thanks for reading. Let me know if anyone needs more details on JOX. Any comments or suggestions are always welcomed.

Post a Comment