I try to write blogs whenever I get spare time

Just to let you know what I am trying to learn and what I am doing now.

The idea of auto generation of boiler-plate codes of Java EE (part 2)

After defining your XML structure and writing the DTD file, now write your actual XML file that will contain your required info about the entity (like its name, attribute list etc). The XML file can be something like this:

After finishing this initial task, then you have to look for a XML API that would help you to parse and manipulate these XML data. There are many choices like SAX, JAX etc.
I will describe the use of a Borland tool that uses SAX (Simple API for XML). I am telling about this tool because I am familiar with it. You can use any other tool that serves this purpose.
The Borland tool that I am telling about, is provided with JBuilder. Now let us see, how can we combine all these to produce our desired generator.
1. Open JBuilder and choose "New" from the File menu.
2. Choose a databinding project from the XML options. And give the desired name and other info about the project.
3. Then when asked for the DTD file, choose the DTD file we've created before and show the root element of the XML.
4. When you finish the project creation, you will see Some classes attached with your project. The Borland XML tool creates a class for each XML tag it finds in the DTD file. The inter-relationship between classes are defined according to the relations defined in the DTD file. The getter, setter methods for each class and their children.
5. Now you are ready to make your run. You can make a java class named Generator in which you will write the code to generate your desired codes using the XML classes. It will use simple File I/O to write the generated classes.
6. All you need now is to call a unmarshal method of the root class. For example, if we call the method like this: entities.unmarshal("data.xml");
We'll get a tree structure from which, we can get the children list by calling like: entities.getentity().getentityList();
7. And then we are free to use this data anywhere in our code generator class.

0 comments:

Post a Comment