Java XML Helper

Download it here.    

SHIJIE MA would appreciate any suggestions from you.



I developed this tool to help me on two aspects. First is to generate corresponding Java classes that represent elements used in the XML; Second is to generate intellegent method to populate XML data to the corresponding java classes. Then just a method call, a skeleton will be charged with energy and become live to work. It's much more friendly than DOM. It's actually using Digester, but the advantage is all the coding work has already been done.

Modeling from DTD

Giving it a DTD, it will generate a bunch of Java classes that is the modeling of the XML. And after then, all the effort to parse an XML is to invoke a method with the XML file name as the parameter, all the data access methods are then become to a naturally java object method call.

For a Java programmer, if you are parsing XML using DOM or SAX or Digester, you can have a try on this tool. I developed this tool to help meself to get rid of those verbose, and monotonous XML parsing stuff, and I really feel happy about the result.

There's absolutely no learning curve to use this utility and to use the generated codes. The developer also need not do any further work to write script to help doing the modeling.

Generating the Code

In my work, there's a task to parse the content from the struts configure file and find the configuration information of the data sources. That is from out side of a web application, so I can not get those information as in a web container with struts support.

The following screens show an example on how to model the struts-config_1_1.dtd to Java classes using this utility.


  Step 1: Choose the DTD file using button of "Select DTD", or you can type the path manually to the text box  
  Step 2: Choose the target path to contain the generated Java classes using button of "Target Path". You can always type it alternately. If a target path is not described, the current path will be used. A message box will pop out to note the user  
  Step 3: Define the package name of the generated Java classes.  
  Step 4: define the generation options  

Add Variable Prefix: some of the XML tag is the same name as the Java reserved name. To make it simple, adding a prefix will avoid the conflict. For example, if you describe "the" as the prefix of the variable, then all the variables contained in the generated classes will have a name like "theXxxXxx".

  Generate Digester Processing Code: this is suggested to use. Since I spend most of my effort on this feature to let the final objects can automatically populate the data from corresponding XML that is derived from the modeled DTD. Without this option, this tool maybe worth nothing. I believe you will find it helpful.
  Generate XML Serialization Code: this is to serialize the Java objects to XML content. It's not included in this version.
  Generate Directories According to Package: this is to generate the hierarchy directories according to the package definition. For instance, if the package is named "msj.test", then under the target path, a new directory tree of "msj/test" will be generated. The whole path will like $TARGETPATH/msj/test
  Step 5: Generate. This is the most exciting step. Go and you will get all the codes necessary to  model the given DTD|Schame  

How to use the modeling classes

  Name of class:

It's from the element name from the DTD. For example, in the DTD of struts-config_1_1.dtd, a define is like this:
<!ELEMENT struts-config (data-sources?, form-beans?, global-exceptions?, global-forwards?, action-mappings?, controller?, message-resources*, plug-in*)>
<!ATTLIST struts-config id ID #IMPLIED>


From the above illustrated content of result files,you can find a class named Struts_config, the name is from the element name of struts-config, the first character is capital and the hyphen "-" is changed to underscore "_". If there's no any underscore in the element name, then it just capitallize the first letter.

  Name of the attribute: the name of the attribute is unimportant since all the attributes will be defined as private variable. The method to access these variables follows standard java naming convention.For the attribute of "id", there will be access methods lile "String getId()" and "void setId(String _id)".
  Name of the Access method:

For single occurence element, the convention is "xxx getXxx()" and "void setXxx(xxx _x)". For instance, the data-sources element in the struts-config, the code is like below:
Data_sources data_sources;
public Data_sources getData_sources()
return this.data_sources;
public void setData_sources(Data_sources _data_sources)
this.data_sources = _data_sources;

If an element will possibly repeat more than onece, the situation becomes a bit more complex, it will use an ArrayList to model this phenomenon. For the element of "message-resources", the access code is like this:
ArrayList message_resourcess = new ArrayList();

public ArrayList getMessage_resourcess()
return this.message_resourcess;
public void setMessage_resourcess(ArrayList _message_resourcess)
this.message_resourcess = _message_resourcess;
public void addMessage_resources(Message_resources _message_resources)
public void removeMessage_resources(Message_resources _message_resources)
public Message_resources getMessage_resources( int i)
return (Message_resources)this.message_resourcess.get(i);
There maybe need more flexible methods to access this kind of element, any advice, please tell me.

  Root element: Each root element will has a method called "populateDataFromXML(String xmlFile)" if "Generate Digester Processing Code" is selected, this is the method that parse the XML file and populate the data from XML to each java objects. More complex method will be offered to parse from any InputStream.
Example to use it  
  start from root: Struts_config cfg = new Struts_config();
  Parsing XML: cfg.populateDataFromXML("someDir/theDTD.dtd")
  get the id : String theId = cfg.getId();
  get Action mappings: Action_mappings am = cfg.getAction_mappings();
  ... ...
Note: If you can not generate the code by running from the Jar file, please change the running command as this: javaw -classpath "C:\javaXMLHelper\javaXMLHelper.jar;D:\download\dtdparser\dtdparser.jar" com.mars.xmlhelper.JavaXMLHelper
Please give me you advice to make this tool much more convinient, write to me at