JAXB Maven Example

There might be a couple of Jaxb Maven plugins around causing some confusion but we will settle to the maven-jaxb2-plugin because of this stack overflow answer.

The POM will look like this:

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.dimitrisli.jaxb</groupId>
	<artifactId>JaxbShowcase</artifactId>
	<version>1.0</version>
	<build>
		<plugins>
			<plugin>
				<!-- jaxb plugin -->
				<groupId>org.jvnet.jaxb2.maven2</groupId>
				<artifactId>maven-jaxb2-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<!-- the package for the generated java classes -->
							<generatePackage>com.dimitrisli.jaxb.producedClasses</generatePackage>
							<!-- If the following not specified all xsd in resources are included -->
							<schemaIncludes>
								<include>sampleJaxb/CustomersOrders.xsd</include>
							</schemaIncludes>
							<!-- if you don't want old output -->
							<removeOldOutput>true</removeOldOutput>
							<!-- if you want verbosity -->
							<!-- verbose>true</verbose -->
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>${project.build.sourceEncoding}</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>javax.xml.bind</groupId>
			<artifactId>jaxb-api</artifactId>
			<version>2.2.6</version>
		</dependency>
		<dependency>
			<groupId>javax.xml</groupId>
			<artifactId>jaxb-impl</artifactId>
			<version>2.1</version>
		</dependency>
	</dependencies>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
</project>

Note how we explicitly specify the package directory structure of the produced java classes, the location of the XSD file, the level of verbosity. Also by default the outputted java classes are generated in /target/generated-sources/xjc.

The XSD file along with the sample XML data file were taken from this Microsoft tutorial and were placed in resources/sampleJaxb/ and resources/sampleXml/ respectively.

After placing the produced java classes from the target directory to the src/main/java location, the MainExample test class is showcasing the unmarshalling capabilities and the looping through the data coming from the xml now residing in the java classes:

package com.dimitrisli.jaxb;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.dimitrisli.jaxb.producedClasses.OrderType;
import com.dimitrisli.jaxb.producedClasses.Root;
import com.dimitrisli.jaxb.producedClasses.CustomerType;

public class MainExample {

	public static void main(String[] args) throws JAXBException,
			FileNotFoundException {
		JAXBContext context = JAXBContext
				.newInstance("com.dimitrisli.jaxb.producedClasses");
		Unmarshaller unmarshaller = context.createUnmarshaller();
		Root root = (Root) unmarshaller.unmarshal(new FileInputStream(
				"src/main/resources/sampleXml/CustomersOrders.xml"));
		
		System.out.println("first get customers:");
		System.out.println("====================");
		for (CustomerType currentCustomer : root.getCustomers().getCustomer()) {
			System.out.println(currentCustomer.getCompanyName());
			System.out.println(currentCustomer.getContactName());
			System.out.println(currentCustomer.getContactTitle());
			System.out.println(currentCustomer.getCustomerID());
			System.out.println(currentCustomer.getFullAddress());
			System.out.println(currentCustomer.getPhone());
			System.out.println("-----");
		}
		System.out.println("then get orders:");
		System.out.println("====================");
		for (OrderType currentOrder : root.getOrders().getOrder()) {
			System.out.println(currentOrder.getCustomerID());
			System.out.println(currentOrder.getEmployeeID());
			System.out.println(currentOrder.getOrderDate());
			System.out.println(currentOrder.getShipInfo().getShipAddress());
			System.out.println(currentOrder.getShipInfo().getShipName());
			System.out.println(currentOrder.getShipInfo().getShipName());
			System.out.println("-----");
		}
	}
}

The code can be found in this Github repository.

Advertisements