Tutorial

jaxb2-maven-plugin XJC example to Generate Java Classes from XSD

Published on August 3, 2022
author

Pankaj

jaxb2-maven-plugin XJC example to Generate Java Classes from XSD

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Today we will look into jaxb2-maven-plugin XJC example to generate java classes from XSD. JAXB is the middleware technology between Java Classes and XML. We can use JAXB to generate XML from Java Object and vice versa.

jaxb2-maven-plugin

We use XSD to define the contract data structure, so it’s not common to generate java classes that represent the XML schema. jaxb2-maven-plugin XJC is JAXB Binding compiler tool that can be used to generate Java Classes from XSD files. Here we will learn how to use jaxb2-maven-plugin XJC in a maven project to generate java classes from XSD. First of all we have to create a maven project, then we need to use jaxb2-maven-plugin plugin to generate java classes from XSD. pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>jd</groupId>
  <artifactId>jd</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <name>JD Example XSD to Java</name>


	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<build>
		<plugins>
			<!-- Plugin required to build java classes from XSD using XJC -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>jaxb2-maven-plugin</artifactId>
				<version>1.5</version>
				<executions>
					<execution>
						<goals>
							<goal>xjc</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
                   <!-- The name of your generated source package -->
                	<arguments>-extension -npa -b ${project.basedir}/src/main/xsd/global.xjb</arguments> 
                </configuration>
			</plugin>
			
		</plugins>
	</build>
	
</project>

Notice the global.xjb file in arguments, this is where we specify java binding rules. global.xjb

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0"
  xmlns:jaxb="https://java.sun.com/xml/ns/jaxb"
  xmlns:xjc="https://java.sun.com/xml/ns/jaxb/xjc"
  xmlns:xs="https://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>

Here is the XSD that will be used to generate java classes. Employee.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="https://www.w3.org/2001/XMLSchema"
	targetNamespace="https://www.journaldev.com/com/journaldev/employee/data"
	xmlns:empns="https://www.journaldev.com/com/journaldev/employee/data"
	elementFormDefault="qualified">

	<element name="empRequest" type="empns:EmpRequest"></element>
	<element name="empResponse" type="empns:EmpResponse"></element>

	<complexType name="EmpRequest">
		<sequence>
			<element name="id" type="int" minOccurs="0" maxOccurs="1" />
			<element name="name" type="string" minOccurs="0" maxOccurs="1" />
		</sequence>
	</complexType>
	
	<complexType name="EmpResponse">
		<sequence>
			<element name="id" type="int" minOccurs="1" maxOccurs="1" />
			<element name="name" type="string" minOccurs="1" maxOccurs="1" />
			<element name="role" type="string" minOccurs="1" maxOccurs="unbounded" />
			<element name="gender" type="string" minOccurs="1" maxOccurs="1" />
			<element name="salary" type="string" minOccurs="1" maxOccurs="1" />
		</sequence>
	</complexType>

</schema>

Just build the maven project using mvn clean install and you will see java classes generated in target/generated-sources/jaxb directory. Finally the project would look something like below image. jaxb2-maven-plugin XJC Example Further Read: JAXB Tutorial Reference: jaxb2 maven plugin official page, java xjc

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
February 11, 2021

Just FYI the schema namespaces are NOT https:// … etc, they must be http://

- RM

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 15, 2019

    Proper config for JAXB2 for version below 2.0 is : ${project.basedir}/src/main/xsd/ Employee.xsd

    - Azizasm

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      August 17, 2017

      How to add rootelement into java class using XJB

      - Mageswran

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        August 17, 2017

        How to add Root element into Java class using XJB using Maven

        - Mageswran

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          March 9, 2016

          Hi Pankaj, I have one question, what is purpose of -extention and -npa? where you have use this? -extension -npa -b ${project.basedir}/src/main/xsd/global.xjb Thanks

          - Jalaram

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            September 11, 2015

            You forgot to tell mvn where Employee.xsd is. You need a in your element.

            - DeanS

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              May 12, 2015

              change the goal from xjc to generate if you getting Errors when trying to install with maven.

              - A S

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                December 1, 2014

                Thanks Pankaj ! :)

                - Bhawani Singh Shekhawat

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  March 26, 2014

                  Thanks Pankaj. I was looking for an example of the xjc maven plugin usage and yours was exactly what I needed.

                  - Ricardo

                    Try DigitalOcean for free

                    Click below to sign up and get $200 of credit to try our products over 60 days!

                    Sign up

                    Join the Tech Talk
                    Success! Thank you! Please check your email for further details.

                    Please complete your information!

                    Featured on Community

                    Get our biweekly newsletter

                    Sign up for Infrastructure as a Newsletter.

                    Hollie's Hub for Good

                    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                    Become a contributor

                    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                    Welcome to the developer cloud

                    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                    Learn more
                    DigitalOcean Cloud Control Panel