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

Spring JAXB

JAXB는 XML 바인딩을 위한 Java 아키텍처의 약자입니다. Java 개발자가 Java 클래스를 XML 표현 형식으로 매핑할 수 있도록 합니다. JAXB는 Java 객체를 XML로编组하고 반대로도 사용할 수 있습니다.

Sun에서 제공하는 OXM(객체 XML 매핑) 또는 O/M 프레임워크.


JAXB의 장점은 SAX나 DOM 파서를 생성하거나 사용하지 않고,回调 메서드를 작성하지 않아도 됩니다.


Spring과 JAXB 통합 예제(Java 객체를 XML로编组)

JAXB를 사용하여 다음 파일을 생성하려면 로드해야 합니다:

Employee.java applicationContext.xml Client.java


필수 Jar 파일

이 예제를 실행하려면 다음을 로드해야 합니다:

Spring Core jar 파일 Spring Web jar 파일

Spring의 모든 jar 파일을 다운로드하세요. core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm 등.


Employee.java

id, 이름, 급여와 같은 세 가지 속성이 정의되어 있다면. 이 클래스에서는 다음과 같은 주석을 사용합니다:

@XmlRootElement XML 파일의 루트 요소를 지정합니다。 @XmlAttribute 속성의 속성을 지정합니다。 @XmlElement 그래프를 지정합니다。

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

그는 jaxbMarshallerBean이라는 Bean을 정의하고, Employee 클래스가 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

그는 applicationContext.xml 파일에서 Marshaller의 인스턴스를 가져와 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 Created Sucessfully");
 }
}

예제의 출력

employee.xml

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