The xml file under consideration is employee.xml which is as below.
<?xml version="1.0" encoding="UTF-8"?>
<employee xmlns="http://ts.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:e:/karthik/employee.xsd">
<id>1001</id>
<name>Karthikeyan C</name>
<age>29</age>
</employee>
To unmarshal (map) this XML into Employee.java instance, we need to generate the related Java files. To know how to generate JAXB annotated Java files please read this entry How to generate Java files from XSD using JAXB and Maven .
As having an xsd eases the generation of the required files [like ObjectFactory.java], let us create the employee.xsd file which is as below.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://ts.com" xmlns="http://ts.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="1">
<xsd:element name="employee">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="id"/>
<xsd:element ref="name"/>
<xsd:element ref="age"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="id" type="xsd:string"/>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="xsd:integer"/>
</xsd:schema>
Now after running mvn jaxb2:xjc command 3 java files are generated namely Employee.java, ObjectFactory.java and package-info.java. Let us create Test.java which is as below.
package xmltojaxb;
import java.io.FileInputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
public class Test {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("xmltojaxb");
Unmarshaller u = jc.createUnmarshaller();
Employee emp = (Employee) u.unmarshal(new FileInputStream("E:\\karthik\\employee.xml"));
System.out.println("The employee name is :" + emp.getName());
}
}
As we can infer from the code the xml is placed under e:\karthik. Running Test.java we get the output as Karthikeyan C
To try this example please use the pom.xml below
<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>com.thea</groupId>
<artifactId>xsdtojavagenerator</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>xsdtojavagenerator Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies/>
<build>
<finalName>xsjgen</finalName>
<plugins>
<!-- xsd to java generator plugin-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>xmltojaxb</packageName>
<schemaFiles>employee.xsd</schemaFiles>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
