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

准备工作

需处理的数据

1
<cart_goods order_name="iPhone" order_type="3C" order_price="5000.00">2</cart_goods>

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

  • 使用Xstram自带的转换器
  • 自定义的转换器

使用Xstram自带的转换器

pom.xml依赖

1
2
3
4
5
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.11.1</version>
</dependency>

JavaBean实体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import me.yezhou.springboot.xml.converter.CartGoodsConverter;

@Data
@NoArgsConstructor
@AllArgsConstructor
@XStreamAlias("cart_goods")
// 自带的转换器
// @XStreamConverter(value= ToAttributedValueConverter.class, strings={"goodsQuantity"})
// 自定义的转换器
@XStreamConverter(CartGoodsConverter.class)
public class CartGoods {
@XStreamAsAttribute
@XStreamAlias("goods_name")
private String goodsName;
@XStreamAsAttribute
@XStreamAlias("goods_category")
private String goodsCategory;
@XStreamAsAttribute
@XStreamAlias("goods_price")
private String goodsPrice;
// @XStreamOmitField
private String goodsQuantity;
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootXmlApplicationTests {

@Test
public void parseCartGoodsWithXStreamConverter() {
CartGoods cartGoods = new CartGoods("iPhone", "3C", "5000.00", "2");
//XStream xStream = new XStream();
XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));//解决下划线问题
xStream.autodetectAnnotations(true); //自动检测注解
xStream.processAnnotations(CartGoods.class); //应用Bean类的注解
String toXML = xStream.toXML(cartGoods);
// 序列化
System.out.println("序列化: " + toXML);
// 反序列化
Object fromXML = xStream.fromXML(toXML);
System.out.println("反序列化: " + fromXML);
}

}
1
2
序列化: <cart_goods order_name="iPhone" order_type="3C" order_price="5000.00">2</cart_goods>
反序列化: CartGoods(goodsName=iPhone, goodsCategory=3C, goodsPrice=5000.00, goodsQuantity=2)

自定义转换器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
import me.yezhou.springboot.xml.model.CartGoods;

public class CartGoodsConverter implements Converter {
/**
* 将java对象转为xml时使用
* @param source
* @param writer
* @param context
*/
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
CartGoods cartGoods = (CartGoods) source;
writer.addAttribute("order_name", cartGoods.getGoodsName());
writer.addAttribute("order_type", cartGoods.getGoodsCategory());
writer.addAttribute("order_price", cartGoods.getGoodsPrice());
writer.setValue(cartGoods.getGoodsQuantity());
}

/**
* 将xml转为java对象使用
* @param reader
* @param context
* @return
*/
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
// 在解析order元素时,先创建一个Order对象
CartGoods cartGoods = new CartGoods();
// 将order元素的name属性设置为Order对象的name属性值
cartGoods.setGoodsName(reader.getAttribute("order_name"));
cartGoods.setGoodsCategory(reader.getAttribute("order_type"));
cartGoods.setGoodsPrice(reader.getAttribute("order_price"));
// 将order元素的txt值设置为Order对象的value值
cartGoods.setGoodsQuantity(reader.getValue());
return cartGoods;
}

/**
* 转换条件
* @param type
* @return
*/
@Override
public boolean canConvert(Class type) {
return type.equals(CartGoods.class);
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :