操作xml有很多的工具,如果不想使用dom4j之类需要手写代码(而且效率也低)的工具,可以使用绑定工具,这类工具有Castor、Jibx、XStream、XmlBeans、Jaxb2等。
本文简单的介绍spring中的Jaxb2Marshaller的使用,通过进一步封装的类实现无需额外处理代码即可完成oxm(xml->object 与 object->xml),只需要把自定义的实体类在配置文件里注册(实体类上要加上注解)。
先写一个通用的映射类,把xml转换成对象时直接调用它的unmarshal方法即可,从对象转换成xml时只需调用它的marshal方法:
package com.lee.test.xml;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
public class Mapper {
private Marshaller marshaller;
private Unmarshaller unmarshaller;
public void setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
}
public void setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
//java object --> xml
public String marshal(Object obj) {
String xml;
StringWriter writer = null;
try {
writer = new StringWriter();
this.marshaller.marshal(obj, new StreamResult(writer));
xml = writer.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
return xml;
}
//xml --> java object
public Object unmarshal(String xml) {
Object obj = null;
try {
obj = this.unmarshaller.unmarshal(new StreamSource(new StringReader(xml)));
} catch (Exception e) {
throw new RuntimeException(e);
}
return obj;
}
}
spring的配置如下:
<bean id="mapper" class="com.lee.test.xml.Mapper">
<property name="marshaller" ref="marshaller" />
<property name="unmarshaller" ref="marshaller" />
</bean>
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.lee.test.obj.Order</value>
</list>
</property>
</bean>
这里只绑定了一个类Order,如需映射其他类,只需在<list></list>里加上<value>你定义的类</value>,其他代码无需改动。
当然你也可以定义转换后xml的格式、有无声明、编码等,如下:
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.lee.test.obj.Order</value>
</list>
</property>
<property name="marshallerProperties">
<map>
<!-- format : 默认 Boolean.false(Compact format) -->
<entry key="jaxb.formatted.output">
<value type="java.lang.Boolean">true</value>
</entry>
<!-- xml Declaration :默认 Boolean.false -->
<entry key="jaxb.fragment">
<value type="java.lang.Boolean">false</value>
</entry>
<!-- xml encoding declaration: 默认 UTF-8 -->
<entry key="jaxb.encoding" value="GBK"/>
</map>
</property>
</bean>
这里用的是Jaxb2Marshaller,如果要使用其他的绑定工具如Castor,需要把Jaxb2Marshaller改为CastorMarshaller,Mapper类无需改动:
<bean id="marshaller" class="org.springframework.oxm.castor.CastorMarshaller">
<property name="mappingLocation" value="classpath:mapping.xml"></property>
</bean>
Order类的完整代码如下:
package com.lee.test.obj;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Order")
public class Order extends BaseModel{
private String id;
private User user;
@XmlElement(name="Id")//不加时默认为@XmlElement(name="id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
package com.lee.test.obj;
public class User extends BaseModel{
private String name;
private String sex;
private String birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
package com.lee.test.obj;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
public class BaseModel {
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
对于jaxb2的各个注解的使用请查看相关资料,如
https://jaxb.java.net/tutorial/index.html,这里就不多说了。
假如有这么一个场景:客户端post一个xml报文,你经过处理后返回一个xml报文,代码如下:
package com.lee.test.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lee.test.obj.Order;
import com.lee.test.obj.User;
import com.lee.test.xml.Mapper;
@Controller
public class Home {
@Resource
private Mapper mapper;
@RequestMapping(value="postXml", method=RequestMethod.POST, produces={"application/xml;charset=GBK"})
@ResponseBody
public String testPostXml(@RequestBody String xml) {
System.out.println("请求xml报文:"+xml);
//把xml转换成java对象
Order order = (Order)mapper.unmarshal(xml);
//模拟其他操作
order.setId("123");
User user = order.getUser();
user.setName("zhangsan");
user.setBirthday("2013-12-13");
//把java对象转换成xml,你可以绑定其他类,这里为了简便用了Order
xml = mapper.marshal(order);
System.out.println("响应xml报文:"+xml);
return xml;
}
}
测试结果: