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

JAXB Spring

JAXB est Architecture Java pour la liaison XMLle premierlettre. Il permet aux développeurs Java de mapper des classes Java en représentation XML. JAXB peut être utilisé pour sérialiser des objets Java en XML et vice versa.

C'est l'abréviation de Sun fournit OXM (Object XML Mapping) ou O/M framework.


Les avantages de JAXB n'impliquent pas de création ou d'utilisation d'un analyseur SAX ou DOM, ni d'écriture de méthodes de rappel.


Exemple d'intégration Spring et JAXB (sérialisation des objets Java en XML)

Vous devez utiliser JAXB avec Spring pour sérialiser des objets Java en XML pour créer les fichiers suivants:

Employee.java applicationContext.xml Client.java


Fichiers Jar nécessaires

Pour exécuter cet exemple, vous devez charger:

Fichier jar Spring Core Fichier jar Spring Web

Téléchargez tous les fichiers jar de Spring, y compris core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm, etc.


Employee.java

Si trois attributs sont définis: id, nom et salaire. Nous utilisons les commentaires suivants dans cette classe:

@XmlRootElement Cela spécifie l'élément racine du fichier xml. @XmlAttribute Cela spécifie les attributs de l'attribut. @XmlElement Cela spécifie l'élément.

package com.w;3codebox;
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

Il définit un Bean jaxbMarshallerBean, où la classe Employee est liée au cadre 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

Il s'agit de récupérer l'instance de Marshaller à partir du fichier applicationContext.xml et d'appeler la méthode marshal.

package com.w;3codebox;
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("XML créé avec succès");
 }
}

Sortie de l'exemple

employee.xml

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