Spring与Castor集成示例(将Java对象编组为XML)

通过CastorMarshaller类的帮助,我们可以使用Castor将Java对象编组为XML,反之亦然。它是Marshaller和Unmarshaller接口的实现类。它不需要任何进一步的默认配置。

Spring与Castor集成示例(将Java对象编组为XML)

要使用Spring和Castor将Java对象编组为XML,您需要创建以下文件:

Employee.java

这个类定义了三个属性:id、name和salary,以及对应的setter和getter方法。

package cn.javatiku;  
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="cn.javatiku.Employee"></property>  
        <property name="mappingLocation" value="mapping.xml"></property>  
    </bean>  
</beans> 

mapping.xml

这个文件定义了如何将Java类的属性映射到XML元素。

<?xml version="1.0"?>  
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"  
                         "http://castor.org/mapping.dtd">  
 <mapping>  
    <class name="cn.javatiku.Employee" auto-complete="true" >  
        <map-to xml="Employee" ns-uri="http://www.javatiku.cn" ns-prefix="dp"/>  
        <field name="id" type="integer">  
            <bind-xml name="id" node="attribute"></bind-xml>  
        </field>  
        <field name="name">  
            <bind-xml name="name"></bind-xml>  
        </field>  
        <field name="salary">  
            <bind-xml name="salary" type="float"></bind-xml>  
        </field>  
          
    </class>  
      
 </mapping>  

Client.java

这个类从applicationContext.xml文件中获取Marshaller的实例,并调用marshal方法。

package cn.javatiku;  
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");  
 }  
}  

Output of the example

生成的XML文件内容将类似于以下内容:

<?xml version="1.0" encoding="UTF-8"?>  
<dp:Employee xmlns:dp="http://www.javatiku.cn" id="101">  
<dp:name>Sonoo Jaiswal</dp:name>  
<dp:salary>100000.0</dp:salary>  
</dp:Employee>  

标签: spring, Spring教程, Spring技术, Spring语言学习, Spring学习教程, Spring下载, Spring框架, Spring框架入门, Spring框架教程, Spring框架高级教程, Spring面试题, Spring笔试题, Spring编程思想