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>