English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JAXB Spring

JAXB è Architettura Java per la binding XMLSigla. Permette agli sviluppatori Java di mappare le classi Java alla rappresentazione XML. JAXB può essere utilizzato per serializzare gli oggetti Java in XML e viceversa.

È un framework OXM (Object XML Mapping) o O/M fornito da Sun.


I vantaggi di JAXB non richiedono la creazione o l'uso di parser SAX o DOM, né la scrittura di metodi di callback.


Esempio di integrazione Spring e JAXB (serializzazione degli oggetti Java in XML)

È necessario utilizzare Spring con JAXB per serializzare gli oggetti Java in XML per creare i seguenti file:

Employee.java applicationContext.xml Client.java


File jar necessari

Per eseguire questo esempio, è necessario caricare:

File jar Core Spring File jar Web Spring

Scarica tutti i file jar di spring, inclusi core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm, ecc.


Employee.java

Se sono definiti tre attributi id, nome e stipendio. Nella classe di questo tipo abbiamo utilizzato i seguenti commenti:

@XmlRootElement Specifica l'elemento radice del file xml. @XmlAttribute Specifica le proprietà dell'attributo. @XmlElement Specifica l'elemento.

package com.w3codebox;
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

Definisce un Bean jaxbMarshallerBean, in cui la classe Employee è associata al framework OXM.

<?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.w3codebox.Employee"/>
      </oxm:jaxb2-marshaller>
</beans>

Client.java

Essa preleva l'istanza di Marshaller dal file applicationContext.xml e chiama il metodo marshal.

package com.w3codebox;
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("Sonoo Jaiswal");
  employee.setSalary(100000);
    
  marshaller.marshal(employee, new StreamResult(new FileWriter("employee.xml")));
  
  System.out.println("Creazione XML avvenuta con successo");
 }
}

Output dell'esempio

employee.xml

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