Home » Spring and JAXB Integration example | Tutorial

Spring and JAXB Integration example | Tutorial

by Online Tutorials Library

Spring and JAXB Integration Example

JAXB is an acronym for Java Architecture for XML Binding. It allows java developers to map Java class to XML representation. JAXB can be used to marshal java objects into XML and vice-versa.

It is an OXM (Object XML Mapping) or O/M framework provided by Sun.


Advantage of JAXB

No need to create or use a SAX or DOM parser and write callback methods.


Example of Spring and JAXB Integration (Marshalling Java Object into XML)

You need to create following files for marshalling java object into XML using Spring with JAXB:

  1. Employee.java
  2. applicationContext.xml
  3. Client.java

Required Jar files

To run this example, you need to load:

  • Spring Core jar files
  • Spring Web jar files

download all the jar files for spring including core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm etc.


Employee.java

If defines three properties id, name and salary. We have used following annotations in this class:

  1. @XmlRootElement It specifies the root element for the xml file.
  2. @XmlAttribute It specifies attribute for the property.
  3. @XmlElement It specifies the element.

applicationContext.xml

It defines a bean jaxbMarshallerBean where Employee class is bound with the OXM framework.


Client.java

It gets the instance of Marshaller from the applicationContext.xml file and calls the marshal method.

Output of the example

employee.xml

You may also like