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

스프링과 Castor 예제

때문에 CastorMarshaller 클래스를 사용하면 Castor를 통해 Java 객체를 xml로 구조화하고 반대로도 할 수 있습니다. 이는 Marshaller와 Unmarshaller 인터페이스의 구현 클래스입니다. 기본적으로, 추가적인 설정이 필요하지 않습니다.

스프링과 Castor 통합 예제(Java 객체를 XML로 구조화)

Castor를 사용하여 Java 객체를 XML로 구조화하기 위해 다음과 같은 파일을 생성해야 합니다:

Employee.java applicationContext.xml mapping.xml Client.java

필수 Jar 파일

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

스프링 코어 jar 파일 스프링 웹 jar 파일 castor-1.3.jar castor-1.3-core.jar

스프링의 모든 jar 파일을 다운로드하세요. core, web, aop, mvc, j를 포함합니다.2ee, remoting, oxm, jdbc, orm 등.

castor 다운로드-1.3.jar

castor 다운로드 -1.3-core.jar


Employee.java

세 가지 속성을 정의했습니다. id, 이름, 급여.

package com.w;3codebox;
public class Employee {
private int id;
private String name;
private float salary;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public float getSalary() {
    return salary;
}
public void setSalary(float salary) {
    this.salary = salary;
}
}

applicationContext.xml

그것은 castorMarshallerBean이라는 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="castorMarshallerBean" class="org.springframework.oxm.castor.CastorMarshaller">
        <property name="targetClass" value="com.w3codebox.Employee"></property>
        <property name="mappingLocation" value="mapping.xml"></property>
    </bean>
</beans>

mapping.xml

<?xml version="1.0"?>
<!DOCTYPE mapping public "-//EXOLAB/EXOLAB 1Castor Mapping DTD Version//.0
                         EN"//"http:/castor.org
 mapping.dtd">
    <mapping>3<class name="com.w-codebox.Employee" auto
        complete="true" >-<map-to xml="Employee" ns//www.w3uri="http:-codebox.com" ns/prefix="dp"
        >
            <bind-<field name="id" type="integer">/bind-xml>
        </field>
        <field name="id" node="attribute"><
            <bind-<field name="name">/bind-xml>
        </field>
        <field name="salary">
            <bind-<bind xml name="salary" type="float"></bind-xml>
        </field>
        
    </class>
    
 </mapping>

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("castorMarshallerBean");
        
  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"?>
<dp:Employee xmlns:dp="http://ko.oldtoolbag.com" id="101>
<dp:name>Sonoo Jaiswal</dp:name>
<dp:salary>100000.0</dp:salary>
</dp:Employee>