XStream转换器: 处理xml节点中既有属性又有值
1.需处理的数据:orderInfo orderName="酸奶" orderType="奶制品" orderPrice="5.00">2orderInfo>2.处理xml节点中既有属性又有值,有两种方式使用Xstram自带的转换器自定义的转换器3.示例:3.1.JavaBean实体package com.ne
·
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:* @Copyright 2016-2017 - Powered By 研发中心* @author: 王延飞* @date:2017/12/27 8:05*/@XStreamAlias("orderInfo")// 自带的转换器//@XStreamConverter(value=ToAttributedValueConverter.class, strings={"orderNumber"})// 自定义的转换器@XStreamConverter(OrderConverter.class)public class Order {@XStreamAsAttributeprivate String orderName;@XStreamAsAttributeprivate String orderType;@XStreamAsAttributeprivate String orderPrice;// @XStreamOmitFieldprivate String orderNumber;public Order() {}public Order(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;}public void setOrderName(String orderName) {this.orderName = orderName;}public String getOrderType() {return orderType;}public void setOrderType(String orderType) {this.orderType = orderType;}public String getOrderPrice() {return orderPrice;}public void setOrderPrice(String orderPrice) {this.orderPrice = orderPrice;}public String getOrderNumber() {return orderNumber;}public void setOrderNumber(String orderNumber) {this.orderNumber = orderNumber;}@Overridepublic String toString() {return "Order{" +"orderName='" + orderName + '\'' +", orderType='" + orderType + '\'' +", orderPrice='" + orderPrice + '\'' +", orderNumber='" + orderNumber + '\'' +'}';}public static void main(String[] args) {Order order = new Order("酸奶", "奶制品", "5.00", "2");XStream xStream = new XStream();xStream.autodetectAnnotations(true);//自动检测注解xStream.processAnnotations(Order.class);//应用Person类的注解String toXML = xStream.toXML(order);// 序列化System.out.println("【序列化】:"+toXML);// 反序列化Object fromXML = 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:自定义转换器* @Copyright 2016-2017 - Powered By 研发中心* @author: 王延飞* @date:2017/12/27 8:42*/public class OrderConverter implements Converter {//转换条件@Overridepublic boolean canConvert(Class type) {return type.equals(Order.class);}/*** 将java对象转为xml时使用*/@Overridepublic void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {Order order = (Order) source;writer.addAttribute("orderName", order.getOrderName());writer.addAttribute("orderType", order.getOrderType());writer.addAttribute("orderPrice", order.getOrderPrice());writer.setValue(order.getOrderNumber());}/*** 将xml转为java对象使用*/@Overridepublic Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {// 在解析order元素时,先创建一个Order对象Order order = new Order();// 将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'}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)