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|
|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:
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:
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:
public ArrayList getMessage_resourcess()
|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();|
|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 firstname.lastname@example.org|