Spring with JAXB

Spring and JAXB Integration Example

1. Spring and JAXB Integration
2. Example of Spring and JAXB Integration

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

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.

package com.smgc;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="employee")
public class Employee {
private int id;
private String name;
private float salary;
@XmlAttribute(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

@XmlElement(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@XmlElement(name="salary")
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}

applicationContext.xml

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">

<oxm:jaxb2-marshaller id="jaxbMarshallerBean">
<oxm:class-to-be-bound name="com.smgc.Employee"/>
</oxm:jaxb2-marshaller>

</beans>

Client.java

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

package com.smgc;

import java.io.FileWriter;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;

public class Client{
public static void main(String[] args)throws IOException{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Marshaller marshaller = (Marshaller)context.getBean("jaxbMarshallerBean");

Employee employee=new Employee();
employee.setId(101);
employee.setName("Piyush Patel");
employee.setSalary(100000);

marshaller.marshal(employee, new StreamResult(new FileWriter("employee.xml")));

System.out.println("XML Created Sucessfully");
}
}

Output of the example
employee.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee id="101">
<name>Piyush Patel</name>
<salary>100000.0</salary>

</employee>                        

Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes