XStream转换器: 处理xml节点中既有属性又有值

1.需处理的数据:

   
   
  1. <orderInfo orderName="酸奶" orderType="奶制品" orderPrice="5.00">2</orderInfo>

2.处理xml节点中既有属性又有值,有两种方式

   
   
  1. 使用Xstram自带的转换器
  2. 自定义的转换器

3.示例:

3.1.JavaBean实体

   
   
  1. package com.newcapec.dao.domain;
  2. import com.thoughtworks.xstream.XStream;
  3. import com.thoughtworks.xstream.annotations.XStreamAlias;
  4. import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
  5. import com.thoughtworks.xstream.annotations.XStreamConverter;
  6. /**
  7. * @version V1.0
  8. * @Title: JavaBean实体
  9. * @ClassName: com.newcapec.dao.domain.enums
  10. * @Description:
  11. * @Copyright 2016-2017 - Powered By 研发中心
  12. * @author: 王延飞
  13. * @date:2017/12/27 8:05
  14. */
  15. @XStreamAlias("orderInfo")
  16. // 自带的转换器
  17. //@XStreamConverter(value=ToAttributedValueConverter.class, strings={"orderNumber"})
  18. // 自定义的转换器
  19. @XStreamConverter(OrderConverter.class)
  20. public class Order {
  21. @XStreamAsAttribute
  22. private String orderName;
  23. @XStreamAsAttribute
  24. private String orderType;
  25. @XStreamAsAttribute
  26. private String orderPrice;
  27. // @XStreamOmitField
  28. private String orderNumber;
  29. public Order() {
  30. }
  31. public Order(String orderName, String orderType, String orderPrice, String orderNumber) {
  32. this.orderName = orderName;
  33. this.orderType = orderType;
  34. this.orderPrice = orderPrice;
  35. this.orderNumber = orderNumber;
  36. }
  37. public String getOrderName() {
  38. return orderName;
  39. }
  40. public void setOrderName(String orderName) {
  41. this.orderName = orderName;
  42. }
  43. public String getOrderType() {
  44. return orderType;
  45. }
  46. public void setOrderType(String orderType) {
  47. this.orderType = orderType;
  48. }
  49. public String getOrderPrice() {
  50. return orderPrice;
  51. }
  52. public void setOrderPrice(String orderPrice) {
  53. this.orderPrice = orderPrice;
  54. }
  55. public String getOrderNumber() {
  56. return orderNumber;
  57. }
  58. public void setOrderNumber(String orderNumber) {
  59. this.orderNumber = orderNumber;
  60. }
  61. @Override
  62. public String toString() {
  63. return "Order{" +
  64. "orderName='" + orderName + '\'' +
  65. ", orderType='" + orderType + '\'' +
  66. ", orderPrice='" + orderPrice + '\'' +
  67. ", orderNumber='" + orderNumber + '\'' +
  68. '}';
  69. }
  70. public static void main(String[] args) {
  71. Order order = new Order("酸奶", "奶制品", "5.00", "2");
  72. XStream xStream = new XStream();
  73. xStream.autodetectAnnotations(true);//自动检测注解
  74. xStream.processAnnotations(Order.class);//应用Person类的注解
  75. String toXML = xStream.toXML(order);
  76. // 序列化
  77. System.out.println("【序列化】:"+toXML);
  78. // 反序列化
  79. Object fromXML = xStream.fromXML("【反序列化】:"+toXML);
  80. System.out.println(fromXML);
  81. }
  82. }

3.2.自定义转换器:

    
    
  1. package com.newcapec.dao.domain;
  2. import com.thoughtworks.xstream.converters.Converter;
  3. import com.thoughtworks.xstream.converters.MarshallingContext;
  4. import com.thoughtworks.xstream.converters.UnmarshallingContext;
  5. import com.thoughtworks.xstream.io.HierarchicalStreamReader;
  6. import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
  7. /**
  8. * @version V1.0
  9. * @Title:
  10. * @ClassName: com.newcapec.dao.domain
  11. * @Description: 自定义转换器
  12. * @Copyright 2016-2017 - Powered By 研发中心
  13. * @author: 王延飞
  14. * @date:2017/12/27 8:42
  15. */
  16. public class OrderConverter implements Converter {
  17. //转换条件
  18. @Override
  19. public boolean canConvert(Class type) {
  20. return type.equals(Order.class);
  21. }
  22. /**
  23. * 将java对象转为xml时使用
  24. */
  25. @Override
  26. public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
  27. Order order = (Order) source;
  28. writer.addAttribute("orderName", order.getOrderName());
  29. writer.addAttribute("orderType", order.getOrderType());
  30. writer.addAttribute("orderPrice", order.getOrderPrice());
  31. writer.setValue(order.getOrderNumber());
  32. }
  33. /**
  34. * 将xml转为java对象使用
  35. */
  36. @Override
  37. public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
  38. // 在解析order元素时,先创建一个Order对象
  39. Order order = new Order();
  40. // 将order元素的name属性设置为Order对象的name属性值
  41. order.setOrderName(reader.getAttribute("orderName"));
  42. order.setOrderType(reader.getAttribute("orderType"));
  43. order.setOrderPrice(reader.getAttribute("orderPrice"));
  44. // 将order元素的txt值设置为Order对象的value值
  45. order.setOrderNumber(reader.getValue());
  46. return order;
  47. }
  48. }

3.3.输出结果:

     
     
  1. 【序列化】:<orderInfo orderName="酸奶" orderType="奶制品" orderPrice="5.00">2</orderInfo>
  2. 【反序列化】:Order{orderName='酸奶', orderType='奶制品', orderPrice='5.00', orderNumber='2'}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

琦彦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值