借助 CastorMarshaller 類,我們可以使用Castor將Java對(duì)象編組為xml,反之亦然。它是Marshaller和Unmarshaller接口的實(shí)現(xiàn)類。默認(rèn)情況下,不需要任何其他配置。
您需要?jiǎng)?chuàng)建以下文件使用帶有Castor的Spring將Java對(duì)象編組為XML:
Employee.java applicationContext.xml mapping.xml Client.java
要運(yùn)行此示例,您需要加載:
Spring Core jar文件 Spring Web jar文件 castor-1.3.jar castor-1.3-core.jar
下載spring的所有jar文件,包括core,web,aop,mvc,j2ee,remoting ,oxm,jdbc,orm等。
下載castor-1.3.jar
下載castor -1.3-core.jar
Employee.java
如果使用設(shè)置器和獲取器定義了三個(gè)屬性id,名稱和薪水。
package com.nhooo; 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
它定義了一個(gè)Bean castorMarshallerBean,其中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.nhooo.Employee"></property> <property name="mappingLocation" value="mapping.xml"></property> </bean> </beans>
mapping.xml
<?xml version="1.0"?> <!DOCTYPE mapping public "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" "http://castor.org/mapping.dtd"> <mapping> <class name="com.nhooo.Employee" auto-complete="true" > <map-to xml="Employee" ns-uri="http://www.soo66.com" 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的實(shí)例并調(diào)用marshal方法。
package com.nhooo; 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://www.soo66.com" id="101"> <dp:name>Sonoo Jaiswal</dp:name> <dp:salary>100000.0</dp:salary> </dp:Employee>