Friday, January 28, 2011

XML and JAVA

Covert XML to XSD using trang

I am sure there are better ways to covert an XML to xsd than using java with that said, I like the thought of using ant and java to generate xsd and automate it as part of my build.

I found trang while I was looking for way to create an xsd for my jaxb/web service projects, If you have worked with xsd's you would know how tedious it is to write an XSD by hand.

I think trang generates good xsd based of an xml (provided you enter all the data in the xml that is the input to trang), The tweaking is mostly to do with data types on the generated xsd.

I have listed the ant file that you would need to generate an xsd based on the xml file, If you look at the build xml, It is rather simple just provide the path to trang jars and the simple ant java task and you will have an xsd generated

<project name="jaxbMarshall" default="generateXSDFromXML">

 <property name="MAVEN_HOME" value="${user.home}"></property>

 <path id="project.classpath">
  <fileset dir="${MAVEN_HOME}/.m2/repository/com/thaiopensource/trang/20091111" includes="**/*.jar"/>
 </path>

 <target name="generateXSDFromXML">
  <java classname="com.thaiopensource.relaxng.translate.Driver">
   <!-- input xml file -->
   <arg id="-i" value="xxxx.xml"/>
   <!-- out put xsd file -->
   <arg id="-o" value="xxxxx.xsd"/>
   <classpath refid="project.classpath"/>
  </java>
 </target>

</project>

JAXB (XML Marshalling)

If you type java xml marshalling in google you will find a myriad results on marshalling and unmarshalling of xml data, I have used my share of  oxm tools castor, xstream  and JAXB to name a few. I have finally settled on jaxb as my primary tool for marshalling xml data. My reasons are rather simple easy to generate binding objects (maven plugin) and seems to provide decent performance (No memory or cpu usage issues).

I did find an excellent article that delves into performance side of these tools and JAXB seems do a decent job in their test

Let's get to the part of maven plugin (there is an ant task too, if you prefer ant over maven) to do the JAXB, I think JAXB project page does an excellent job in every aspect of documentation but I do find their maven plugin documentation part very spotty. I found this site that has more useful documentiation for jaxb maven plugin.

I could finally put together a maven plugin configuration that I could use in most of my projects to generate JAXB binder objects

I have listed the plugin config here, If you look at the configuration for the phase you will see that I am triggering xsd to java when you run generate-sources (i.e  mvn generate-sources command) that will generate the java binder objects using the XSD's listed in  schemadirectory (any file with *.xsd). I find this configuration to be very simple and reusable in any project where you would be using jaxb for xml marshalling

<project name="jaxbMarshall" default="generateXSDFromXML">

 <property name="MAVEN_HOME" value="${user.home}"></property>

 <path id="project.classpath">
  <fileset dir="${MAVEN_HOME}/.m2/repository/com/thaiopensource/trang/20091111" includes="**/*.jar"/>
 </path>

 <target name="generateXSDFromXML">
  <java classname="com.thaiopensource.relaxng.translate.Driver">
   <!-- input xml file -->
   <arg id="-i" value="xxxx.xml"/>
   <!-- out put xsd file -->
   <arg id="-o" value="xxxxx.xsd"/>
   <classpath refid="project.classpath"/>
  </java>
 </target>

</project>
You can download the complete source code for trang and jaxb project example from here


No comments:

Post a Comment