XStream转换器: 处理xml节点中既有属性又有值
简介:
XStream转换器: 处理xml节点中既有属性又有值
1.需处理的数据:
<orderInfo orderName="酸奶" orderType="奶制品" orderPrice="5.00">2</orderInfo>
2.处理xml节点中既有属性又有值,有两种方式
使用Xstram自带的转换器
自定义的转换器
3.示例:
3.1.JavaBean实体
package com.newcapec.dao.domain;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
/**
* @version V1.0
* @Title:
JavaBean实体
* @ClassName: com.newcapec.dao.domain.enums
* @Description:
* @Copyright2016-2017 - Powered By 研发中心
* @author: 王延飞
* @date:2017/12/278:05
*/
@XStreamAlias("orderInfo")
// 自带的转换器
//@XStreamConverter(value=ToAttributedValueConverter.class, strings={"orderNumber"})
// 自定义的转换器
@XStreamConverter(OrderConverter.class)
publicclassOrder {
@XStreamAsAttribute
private String orderName;
@XStreamAsAttribute
private String orderType;
@XStreamAsAttribute
private String orderPrice;
// @XStreamOmitField
private String orderNumber;
publicOrder() {
}
publicOrder(String orderName, String orderType, String orderPrice, String orderNumber) {
this.orderName = orderName;
this.orderType = orderType;
this.orderPrice = orderPrice;
this.orderNumber = orderNumber;
}
public String getOrderName() {
return orderName;
}
publicvoidsetOrderName(String orderName) {
this.orderName = orderName;
}
public String getOrderType() {
return orderType;
}
publicvoidsetOrderType(String orderType) {
this.orderType = orderType;
}
public String getOrderPrice() {
return orderPrice;
}
publicvoidsetOrderPrice(String orderPrice) {
this.orderPrice = orderPrice;
}
public String getOrderNumber() {
return orderNumber;
}
publicvoidsetOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
@Override
public String toString() {
return"Order{" +
"orderName='" + orderName + '\'' +
", orderType='" + orderType + '\'' +
", orderPrice='" + orderPrice + '\'' +
", orderNumber='" + orderNumber + '\'' +
'}';
}
publicstaticvoidmain(String[] args) {
Orderorder=newOrder("酸奶", "奶制品", "5.00", "2");
XStreamxStream=newXStream();
xStream.autodetectAnnotations(true);//自动检测注解
xStream.processAnnotations(Order.class);//应用Person类的注解
StringtoXML= xStream.toXML(order);
// 序列化
System.out.println("【序列化】:"+toXML);
// 反序列化
ObjectfromXML= xStream.fromXML("【反序列化】:"+toXML);
System.out.println(fromXML);
}
}
3.2.自定义转换器:
package com.newcapec.dao.domain;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
/**
* @version V1.0
* @Title:
* @ClassName: com.newcapec.dao.domain
* @Description:
自定义转换器
* @Copyright2016-2017 - Powered By 研发中心
* @author: 王延飞
* @date:2017/12/278:42
*/
publicclassOrderConverterimplementsConverter {
//转换条件
@Override
publicbooleancanConvert(Class type) {
return type.equals(Order.class);
}
/**
* 将java对象转为xml时使用
*/
@Override
publicvoidmarshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
Orderorder= (Order) source;
writer.addAttribute("orderName", order.getOrderName());
writer.addAttribute("orderType", order.getOrderType());
writer.addAttribute("orderPrice", order.getOrderPrice());
writer.setValue(order.getOrderNumber());
}
/**
* 将xml转为java对象使用
*/
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
// 在解析order元素时,先创建一个Order对象
Orderorder=newOrder();
// 将order元素的name属性设置为Order对象的name属性值
order.setOrderName(reader.getAttribute("orderName"));
order.setOrderType(reader.getAttribute("orderType"));
order.setOrderPrice(reader.getAttribute("orderPrice"));
// 将order元素的txt值设置为Order对象的value值
order.setOrderNumber(reader.getValue());
return order;
}
}
3.3.输出结果:
【序列化】:<orderInfo orderName="酸奶" orderType="奶制品" orderPrice="5.00">2</orderInfo>
【反序列化】:Order{orderName='酸奶', orderType='奶制品', orderPrice='5.00', orderNumber='2'}