Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Spring with JAXB


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.
  1. package com.Kreationnext;
  2. import javax.xml.bind.annotation.XmlAttribute;
  3. import javax.xml.bind.annotation.XmlElement;
  4. import javax.xml.bind.annotation.XmlRootElement;
  5. @XmlRootElement(name=“employee”)
  6. public class Employee {
  7. private int id;
  8. private String name;
  9. private float salary;
  10. @XmlAttribute(name=“id”)
  11. public int getId() {
  12.     return id;
  13. }
  14. public void setId(int id) {
  15.     this.id = id;
  16. }
  17. @XmlElement(name=“name”)
  18. public String getName() {
  19.     return name;
  20. }
  21. public void setName(String name) {
  22.     this.name = name;
  23. }
  24. @XmlElement(name=“salary”)
  25. public float getSalary() {
  26.     return salary;
  27. }
  28. public void setSalary(float salary) {
  29.     this.salary = salary;
  30. }
  31. }

applicationContext.xml

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

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xmlns:oxm=“http://www.springframework.org/schema/oxm”
  5.     xsi:schemaLocation=”http://www.springframework.org/schema/beans 
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  7.         http://www.springframework.org/schema/oxm
  8.         http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd”>
  9.         <oxm:jaxb2-marshaller id=“jaxbMarshallerBean”>
  10.             <oxm:class-to-be-bound name=“com.Kreationnext.Employee”/>
  11.         </oxm:jaxb2-marshaller>
  12. </beans>

Client.java

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

  1. package com.Kreationnext;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import javax.xml.transform.stream.StreamResult;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. import org.springframework.oxm.Marshaller;
  8. public class Client{
  9.  public static void main(String[] args)throws IOException{
  10.   ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
  11.   Marshaller marshaller = (Marshaller)context.getBean(“jaxbMarshallerBean”);
  12.   Employee employee=new Employee();
  13.   employee.setId(101);
  14.   employee.setName(“Sonoo Jaiswal”);
  15.   employee.setSalary(100000);
  16.   marshaller.marshal(employee, new StreamResult(new FileWriter(“employee.xml”)));
  17.   System.out.println(“XML Created Sucessfully”);
  18.  }
  19. }

Output of the example

employee.xml

  1. <?xml version=“1.0” encoding=“UTF-8” standalone=“yes”?>
  2. <employee id=“101”>
  3. <name>Sonoo Jaiswal</name>
  4. <salary>100000.0</salary>
  5. </employee>

Comments

comments