These are the steps to create java classes from xsd files from Eclipse using the JAXB2 Maven plugin:
1) Create a Maven project in Eclipse
2) Add the JAXB2 Maven plugin to the pom.xml file
In the pom.xml file:
- Add the JAXB2 plugin dependency.
- Configure the location of schema files.
- Configure the location of the global binding directory.
- Configure the directory for generated classes.
<plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.9.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${project.basedir}/src/main/xsd</schemaDirectory> <schemaIncludes> <include>Patient.xsd</include> </schemaIncludes> <bindingDirectory>${project.basedir}/src/main/xsd</bindingDirectory> <bindingIncludes> <include>global.xjb</include> </bindingIncludes> <generateDirectory>${project.basedir}/src/generated</generateDirectory> </configuration> </plugin>
3) Configure datatype binding rules in the xjb file
This is the content of the global.xjb file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema" jaxb:extensionBindingPrefixes="xjc"> <jaxb:globalBindings> <xjc:simple /> <xjc:serializable uid="-1" /> <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" printMethod="javax.xml.bind.DatatypeConverter.printDateTime" /> </jaxb:globalBindings> </jaxb:bindings>
4) Create Java Classes
Right-click on the project and select Run As > Maven Generate Sources. The classes should be created in the generate directory mentioned in step 2. The package name will match the target namespace in the xsd file.