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

准备工作

需处理的数据

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

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

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

使用Xstram自带的转换器

pom.xml依赖

<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.11.1</version>
</dependency>

JavaBean实体

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;
}

测试

@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);
    }

}
序列化: <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)

自定义转换器

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);
    }
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/09/spring-boot-integrate-xstream-converter-handling-both-attributes-and-values-in-xml-nodes/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Spring Boot集成XStream转换器:处理xml节点中既有属性又有值
准备工作 需处理的数据 <cart_goods order_name="iPhone" order_type="3C" order_price="5000.00">2</cart_goods>……
<<上一篇
下一篇>>
文章目录
关闭
目 录