java类转xml——内含3个实例可直接copy运行

本文详细介绍使用XStream库将Java对象转换为XML格式的方法,包括基本属性、嵌套类和列表类型的序列化技巧,通过具体示例展示了如何利用注解定制XML结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 核心代码

 XStream xstream = new XStream(new StaxDriver());
 xstream.autodetectAnnotations(true); //开启XStream注解
 xstream.toXML(xmlResultVO); // xmlResultVO可以替换成你要 转换的java类

——————————————————————以下3个实例均可直接copy运行

实例1. 普通属性

@XStreamAlias 为类 和 属性 起别名

@Data
@XStreamAlias("Goods")
public class ToXmlDemo1 {

    private Double goodsWeight = 1.22;

    private Double cube = 3.44;

//    @XStreamOmitField //隐藏字段  xml中不会显示
    private int totalNumber =20;
}



class Test{
    public static void main(String[] args) {
        String xml = classToXml();
        System.out.println(xml);
    }

    public static String classToXml(){
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true); //开启XStream注解
        ToXmlDemo1 toXmlDemo1 = new ToXmlDemo1();
        String toXML = xstream.toXML(toXmlDemo1);// xmlResultVO可以替换成你要 转换的java类
        return toXML;

    }
}

}
<Goods>
  <goodsWeight>1.22</goodsWeight>
  <cube>3.44</cube>
  <totalNumber>20</totalNumber>
</Goods>

加上 @XStreamOmitField 结果:

在这里插入图片描述

实例2:属性中有类:


import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import lombok.Data;

@Data
@XStreamAlias("Goods")
public class ToXmlDemo1 {

    private Double goodsWeight = 1.22;

    private Double cube = 3.44;

    @XStreamOmitField //隐藏字段  xml中不会显示
    private int totalNumber =20;

    @XStreamAlias("goodPrice")
    private Price price;
}


@Data
class Price{
    private Double lowPrice = 11.11;

    private Double midPrice = 55.55;

    private Double highPrice = 99.99;
}



class Test{
    public static void main(String[] args) {
        String xml = classToXml();
        System.out.println(xml);
    }

    public static String classToXml(){
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true); //开启XStream注解
        ToXmlDemo1 toXmlDemo1 = new ToXmlDemo1(); //要转化成xml的类
        Price price = new Price();
        toXmlDemo1.setPrice(price); // 属性是类的 进行 设置值
        String toXML = xstream.toXML(toXmlDemo1);
        return toXML;

    }
}

结果:

<Goods>
  <goodsWeight>1.22</goodsWeight>
  <cube>3.44</cube>
  <goodPrice>
    <lowPrice>11.11</lowPrice>
    <midPrice>55.55</midPrice>
    <highPrice>99.99</highPrice>
  </goodPrice>
</Goods>

实例3: 属性中有list

属性priceList 希望节点名称是:goodPriceList
里面的每一小项的 节点名称是:goodSingle

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
@XStreamAlias("Goods")
public class ToXmlDemo1 {

    private Double goodsWeight = 1.22;

    private Double cube = 3.44;

    @XStreamOmitField //隐藏字段  xml中不会显示
    private int totalNumber =20;

    @XStreamAlias("goodPriceList")
    private List<Price> priceList;
}


@Data
@XStreamAlias("goodSingle")
class Price{
    private Double lowPrice = 11.11;

    private Double midPrice = 55.55;

    private Double highPrice = 99.99;
}



class Test{
    public static void main(String[] args) {
        String xml = classToXml();
        System.out.println(xml);
    }

    public static String classToXml(){
        XStream xstream = new XStream();
        xstream.autodetectAnnotations(true); //开启XStream注解
        ToXmlDemo1 toXmlDemo1 = new ToXmlDemo1();
        List<Price> priceList = new ArrayList<>();
        Price price = new Price();
        priceList.add(price);
        toXmlDemo1.setPriceList(priceList);
        String toXML = xstream.toXML(toXmlDemo1);// xmlResultVO可以替换成你要 转换的java类
        return toXML;

    }
}

结果:

<Goods>
  <goodsWeight>1.22</goodsWeight>
  <cube>3.44</cube>
  <goodPriceList>
    <goodSingle>
      <lowPrice>11.11</lowPrice>
      <midPrice>55.55</midPrice>
      <highPrice>99.99</highPrice>
    </goodSingle>
  </goodPriceList>
</Goods>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值