阅读(2832)
赞(6)
Spring教程 - 弹簧对象 - XML映射
2017-01-09 19:06:19 更新
Spring教程 - 弹簧对象 - XML映射
以下代码显示了如何使用Castor进行Java对象到XML映射。
项目依赖
要使用脚本,请将以下依赖项添加到pom.xml文件。
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring oxm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Uses Castor for XML -->
<dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>castor</artifactId>
<version>1.2</version>
</dependency>
<!-- Castor need this -->
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
Java Bean
这里是一个简单的Java bean做xml映射
package com.www.zijiebao.common;
public class Employee {
String name;
int age;
boolean trained;
String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isTrained() {
return trained;
}
public void setTrained(boolean trained) {
this.trained = trained;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Marshaller和Unmarshaller
以下代码处理映射通过Spring“的oxm接口:Marshaller和Unmarshaller。
package com.www.zijiebao.common;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
public class XMLConverter {
private Marshaller marshaller;
private Unmarshaller unmarshaller;
public Marshaller getMarshaller() {
return marshaller;
}
public void setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
}
public Unmarshaller getUnmarshaller() {
return unmarshaller;
}
public void setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
public void convertFromObjectToXML(Object object, String filepath)
throws IOException {
FileOutputStream os = null;
try {
os = new FileOutputStream(filepath);
getMarshaller().marshal(object, new StreamResult(os));
} finally {
if (os != null) {
os.close();
}
}
}
public Object convertFromXMLToObject(String xmlfile) throws IOException {
FileInputStream is = null;
try {
is = new FileInputStream(xmlfile);
return getUnmarshaller().unmarshal(new StreamSource(is));
} finally {
if (is != null) {
is.close();
}
}
}
}
弹簧配置
在Spring的bean配置文件中,注入CastorMarshaller作为XML绑定框架。
<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="XMLConverter" class="com.www.zijiebao.common.XMLConverter">
<property name="marshaller" ref="castorMarshaller" />
<property name="unmarshaller" ref="castorMarshaller" />
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" />
</beans>
例子
这里是运行应用程序的代码。
package com.www.zijiebao.common;
import java.io.IOException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
private static final String XML_FILE_NAME = "customer.xml";
public static void main(String[] args) throws IOException {
ApplicationContext appContext = new ClassPathXmlApplicationContext("SpringBeans.xml");
XMLConverter converter = (XMLConverter) appContext.getBean("XMLConverter");
Employee customer = new Employee();
customer.setName("java2s");
customer.setAge(99);
customer.setTrained(true);
customer.setAddress("This is address");
//from object to XML file
converter.convertFromObjectToXML(customer, XML_FILE_NAME);
//from XML to object
Employee customer2 = (Employee)converter.convertFromXMLToObject(XML_FILE_NAME);
System.out.println(customer2);
}
}
上面的代码生成以下结果。

在项目根文件夹中生成以下XML文件“customer.xml"。
文件:customer.xml
<?xml version="1.0" encoding="UTF-8"?> <customer trained="true" age="99"> <address>This is address</address> <name>java2s</name> </customer>
Download Castor_XML_Mapping.zip
例子...
要控制哪个字段应用作属性或元素,使用Castor XML映射文件来定义Object和XML之间的关系。
以下代码创建映射文件mapping.xml。把它放到我们的项目类路径中。
<mapping>
<class name="com.java2s.core.model.Employee">
<map-to xml="customer" />
<field name="age" type="integer">
<bind-xml name="age" node="attribute" />
</field>
<field name="trained" type="boolean">
<bind-xml name="trained" node="element" />
</field>
<field name="name" type="string">
<bind-xml name="name" node="element" />
</field>
<field name="address" type="string">
<bind-xml name="address" node="element" />
</field>
</class>
</mapping>
在Spring bean配置文件中,将上面的mapping.xml注入CastorMarshaller通过“mappingLocation"。
<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="XMLConverter" class="com.java2s.core.XMLConverter">
<property name="marshaller" ref="castorMarshaller" />
<property name="unmarshaller" ref="castorMarshaller" />
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" >
<property name="mappingLocation" value="classpath:mapping.xml" />
</bean>
</beans>

