Relying our Model from XML is always considered a better approach as it simplify the relationship and maintenance.
We can use Jaxb2 plugin for generating model driven Java classes.
We can use Jaxb2 plugin for generating model driven Java classes.
Apart from this the other benefit is that if you are going to use them in webservices communication you will never encounter of issue which comes during marshaling and de-marshaling by the Webservice API vendors say CXF/ Restlet.
In this post we are going to see conversion from XSD to Java with some utility methods.
JAXB2 Basics provides a package of plugins which can generate such utility code
Below is schema.xsd file
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="1.0">
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings choiceContentProperty="true" generateIsSetMethod="true">
<!--xjc:noValidator />
<xjc:noValidatingUnmarshaller /-->
</jaxb:globalBindings>
<jaxb:schemaBindings>
<jaxb:package name="com.techiehouse.pojo"/>
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>
<xs:element name="heteroSequence" type="heteroSequenceType"/>
<xs:complexType name="heteroSequenceType">
<xs:sequence>
<xs:element name="sa" type="xs:string"/>
<xs:element name="sb" type="xs:string"/>
<xs:sequence>
<xs:element name="sc" type="xs:string"/>
<xs:element name="sd" type="xs:string"/>
</xs:sequence>
<xs:sequence maxOccurs="unbounded">
<xs:element name="se" type="xs:string"/>
<xs:element name="sf" type="sequenceType"/>
</xs:sequence>
</xs:sequence>
</xs:complexType>
<xs:element name="simpleTypes" type="simpleTypesType"/>
<xs:complexType name="simpleTypesType">
<xs:sequence>
<xs:element name="base64Binary" type="xs:base64Binary" minOccurs="0"/>
<xs:element name="hexBinary" type="xs:hexBinary" minOccurs="0"/>
<xs:element name="NMTOKEN" type="xs:NMTOKEN" minOccurs="0"/>
<xs:element name="NMTOKENS" type="xs:NMTOKENS" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="elementWithListAttribute1">
<xs:attribute name="list1" type="listType1" use="optional"/>
</xs:complexType>
<xs:simpleType name="listType1">
<xs:list itemType="patternType1"/>
</xs:simpleType>
<xs:simpleType name="patternType1">
<xs:restriction base="xs:string">
<xs:length value="9"/>
<xs:pattern value="[A-Z]{2}([0-9]|[A-Z]){7}"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
wheras POM.xml should look like
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-sample-basic-maven</artifactId>
<version>0.6.4</version>
<packaging>jar</packaging>
<name>JAXB2 Basics - Sample [basic-maven]</name>
<dependencies>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.5-2</version>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
<version>0.6.4</version>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-testing</artifactId>
<version>0.6.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<args>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-Xinheritance</arg>
<arg>-Xcopyable</arg>
<arg>-XenumValue</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>
</plugin>
</plugins>
</configuration>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Maven will generate the Java source file at the mentioned package location.
References: http://confluence.highsource.org/display/J2B/Home