01: JAXB Tutorial for XML processing in Java step by step – unmarshall & marshall

Step 1: Create a Java Maven project.

Import the “simple-jaxb” folder that has the pom.xml it into eclipse as an “existing maven project”.

Step 2: Modify the “pom.xml” file to look like as shown below. The “maven-jaxb2-plugin” generates JAXB annotated Java objects from the XML schema “books.xsd”. These objects JAXB annotated objects are used for unmarshalling (i.e. String or stream to Java objects) & marshalling (i.e. Java objects to String or stream).

Step 4: Create a new source folder src/main/resources and create a folder named “schema”. Under this folder create a XSD file named “books.xsd

The above XSD is for XML file Books.xml under src/main/resources/xml

Step 5: Generate the JAXB objects by running “mvn generate-sources” on the command line, or within eclipse by right clicking on the project, and then “Run As” -> “Maven generate-sources”

Step 6: Add the “${project.build.directory}/generated-sources/xjc1” to the the eclipse “build path” by right mouse clicking on the folder and then “Build Path” -> “Use as Source Folder“.

Step 7: Create the following “App.java” class (using Java 8) to marshall & unmarshall using the generated JAXB objects and JAXB API.

Output

Run App.java to get the following output.

Project Structure

JAXB for marshalling & unmarshalling XML files

JAXB for marshalling & unmarshalling XML files


Print Friendly
The following two tabs change content below.
Arulkumaran Kumaraswamipillai
Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.
Arulkumaran Kumaraswamipillai

Mechanical Engineering to Java freelancer since 2003. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.

Posted in JAXB Tutorial

600+ Java Interview Q&As ♥Free|♦FAQ

open all | close all

200+ Java Interview FAQs – Memory Joggers

open all | close all

16 Java Key Areas to be a top-notch

open all | close all

80+ Java Tutorials – Step by step

open all | close all

100+ Java Coding Exercises

open all | close all

How good are your "Career Skills"?

open all | close all