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:
- Employee.java
- applicationContext.xml
- Client.java
Required Jar files
To run this example, you need to load:
- Spring Core jar files
- Spring Web jar files
Employee.java
If defines three properties id, name and salary. We have used following annotations in this class:
- @XmlRootElement It specifies the root element for the xml file.
- @XmlAttribute It specifies attribute for the property.
- @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
download this example (developed using Eclipse IDE)