在本教程中,我們將介紹如何使用 Spring 的 OXM 來(lái)做轉(zhuǎn)換, Object <--- Spring oxm ---> XML.
package com.yiibai.core.model; public class Customer { String name; int age; boolean flag; String address; //standard getter, setter and toString() methods. }
這個(gè)類(lèi)將處理通過(guò) Spring 的 OXM 接口的轉(zhuǎn)換: Marshaller 和 Unmarshaller.
package com.yiibai.core; 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(); } } } }
<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.yiibai.core.XMLConverter"> <property name="marshaller" ref="castorMarshaller" /> <property name="unmarshaller" ref="castorMarshaller" /> </bean> <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" /> </beans>
運(yùn)行它
package com.yiibai.core; import java.io.IOException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yiibai.core.model.Customer; public class App { private static final String XML_FILE_NAME = "customer.xml"; public static void main(String[] args) throws IOException { ApplicationContext appContext = new ClassPathXmlApplicationContext("App.xml"); XMLConverter converter = (XMLConverter) appContext.getBean("XMLConverter"); Customer customer = new Customer(); customer.setName("yiibai"); customer.setAge(28); customer.setFlag(true); customer.setAddress("Haikou haidiandao"); System.out.println("Convert Object to XML!"); //from object to XML file converter.convertFromObjectToXML(customer, XML_FILE_NAME); System.out.println("Done \n"); System.out.println("Convert XML back to Object!"); //from XML to object Customer customer2 = (Customer)converter.convertFromXMLToObject(XML_FILE_NAME); System.out.println(customer2); System.out.println("Done"); } }
輸出結(jié)果
Convert Object to XML! Done Convert XML back to Object! Customer [name=yiibai, age=28, flag=true, address=Haikou Haidiandao] Done
File : customer.xml
<?xml version="1.0" encoding="UTF-8"?> <customer flag="true" age="28"> <address>Haikou Haidiandao</address> <name>yiibai</name> </customer>
等等,為什么flag和age可轉(zhuǎn)換為屬性?這是一種來(lái)控制哪些字段應(yīng)為屬性或元素的使用的方式? 當(dāng)然,您可以使用 Castor XML映射定義對(duì)象 和XML之間的關(guān)系。
File : mapping.xml
<mapping> <class name="com.yiibai.core.model.Customer"> <map-to xml="customer" /> <field name="age" type="integer"> <bind-xml name="age" node="attribute" /> </field> <field name="flag" type="boolean"> <bind-xml name="flag" 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>
<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.yiibai.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>
File : customer.xml
<?xml version="1.0" encoding="UTF-8"?> <customer age="28"> <flag>true</flag> <name>yiibai</name> <address>Haikou Haidiandao</address> </customer>