本文主要是介绍JavaWeb之XStream使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
XStream的作用
XStream可以把JavaBean对象转换成xml文件。通常服务器给客户端响应的数据是来自数据库的一组对象,这时通过把对象转化成xml再响应给客户端,可以使用XStream。
XSteam具体使用
XSteam的jar包
核心JAR包:xstream-1.4.7.jar;
必须依赖包:xpp3_min-1.1.4c(XML Pull Parser,一款速度很快的XML解析器);
使用步骤
XStream xstream = new XStream();
String xmlStr = xstream.toXML(javabean);
使用细节:
设置别名:xstream.alias(别名, 类)
将子元素设置成属性:xstream.useAttributeFor(类名, 子元素名)
去除Collection类型的标签名: xstream.addImplicitCollection(类名, 元素名)
去除指定类名,不生成对应的xml元素:xstream.omitField(类名, 元素名)
具体代码示例
建立两个javabean:City.java和Province.java
public class City {String name;//城市名String description;//城市描述public City(){}public City(String name,String description){this.name=name;this.description=description;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}
}
Province.java
public class Province {String name;//省份名List<City> arrayList=new ArrayList<>();public String getName() {return name;}public void setName(String name) {this.name = name;}public List<City> getArrayList() {return arrayList;}public void setArrayList(List<City> arrayList) {this.arrayList = arrayList;}public void addCity(City city){arrayList.add(city);}
}
测试类:XStreamTest.java
public class XStreamTest {public List<Province> getProvince(){List<Province> provinceList=new ArrayList<>();Province province1=new Province();province1.setName("北京");City city1=new City("海淀区","haidianQu");City city2=new City("西土城区","xituchengQu");province1.addCity(city1);province1.addCity(city2);Province province2=new Province();province2.setName("安徽");City city3=new City("合肥市","hefei");City city4=new City("蚌埠市","bengbu");province2.addCity(city3);province2.addCity(city4);provinceList.add(province1);provinceList.add(province2);return provinceList;}/*** 无任何处理,得到的xml* <list>* <cn.edu.stu.web.servlet.Province>* <name>北京</name>* <arrayList>* <cn.edu.stu.web.servlet.City>* <name>海淀区</name>* <description>haidianQu</description>* </cn.edu.stu.web.servlet.City>* <cn.edu.stu.web.servlet.City>* <name>西土城区</name>* <description>xituchengQu</description>* </cn.edu.stu.web.servlet.City>* </arrayList>* </cn.edu.stu.web.servlet.Province>* <cn.edu.stu.web.servlet.Province>* <name>安徽</name>* <arrayList>* <cn.edu.stu.web.servlet.City>* <name>合肥市</name>* <description>hefei</description>* </cn.edu.stu.web.servlet.City>* <cn.edu.stu.web.servlet.City>* <name>蚌埠市</name>* <description>bengbu</description>* </cn.edu.stu.web.servlet.City>* </arrayList>* </cn.edu.stu.web.servlet.Province>* </list>*/@Testpublic void func1(){//得到省份listList<Province>provinceList=getProvince();//创建XSTream对象XStream xStream=new XStream();//得到省份对象的xmlString s=xStream.toXML(provinceList);System.out.println(s);}/**** 将<list> 替换成<china>* <cn.edu.stu.web.servlet.Province>替换成<province>* <cn.edu.stu.web.servlet.City>替换成<city>** <china>* <province>* <name>北京</name>* <arrayList>* <city>* <name>海淀区</name>* <description>haidianQu</description>* </city>* <city>* <name>西土城区</name>* <description>xituchengQu</description>* </city>* </arrayList>* </province>* <province>* <name>安徽</name>* <arrayList>* <city>* <name>合肥市</name>* <description>hefei</description>* </city>* <city>* <name>蚌埠市</name>* <description>bengbu</description>* </city>* </arrayList>* </province>* </china>*/@Testpublic void func2(){//得到省份listList<Province>provinceList=getProvince();//创建XSTream对象XStream xStream=new XStream();//将生成的xml内容对应的标签替换别名xStream.alias("china",List.class);xStream.alias("province",Province.class);xStream.alias("city",City.class);//得到省份对象的xmlString s=xStream.toXML(provinceList);System.out.println(s);}/*** 隐藏掉省份中的城市集合属性** <china>* <province>* <name>北京</name>* <city>* <name>海淀区</name>* <description>haidianQu</description>* </city>* <city>* <name>西土城区</name>* <description>xituchengQu</description>* </city>* </province>* <province>* <name>安徽</name>* <city>* <name>合肥市</name>* <description>hefei</description>* </city>* <city>* <name>蚌埠市</name>* <description>bengbu</description>* </city>* </province>* </china>*/@Testpublic void func3(){//得到省份listList<Province>provinceList=getProvince();//创建XSTream对象XStream xStream=new XStream();//将生成的xml内容对应的标签替换别名xStream.alias("china",List.class);xStream.alias("province",Province.class);xStream.alias("city",City.class);//隐藏掉省份中的城市集合属性xStream.addImplicitCollection(Province.class,"arrayList");//得到省份对象的xmlString s=xStream.toXML(provinceList);System.out.println(s);}/*** 把province类的成员,变成province标签的属性* <china>* <province name="北京">* <city>* <name>海淀区</name>* <description>haidianQu</description>* </city>* <city>* <name>西土城区</name>* <description>xituchengQu</description>* </city>* </province>* <province name="安徽">* <city>* <name>合肥市</name>* <description>hefei</description>* </city>* <city>* <name>蚌埠市</name>* <description>bengbu</description>* </city>* </province>* </china>*/@Testpublic void func4(){//得到省份listList<Province>provinceList=getProvince();//创建XSTream对象XStream xStream=new XStream();//将生成的xml内容对应的标签替换别名xStream.alias("china",List.class);xStream.alias("province",Province.class);xStream.alias("city",City.class);//隐藏掉省份中的城市集合属性xStream.addImplicitCollection(Province.class,"arrayList");//把province类的成员,变成province标签的属性xStream.useAttributeFor(Province.class,"name");//得到省份对象的xmlString s=xStream.toXML(provinceList);System.out.println(s);}/**** 去掉city中的description* <china>* <province name="北京">* <city>* <name>海淀区</name>* </city>* <city>* <name>西土城区</name>* </city>* </province>* <province name="安徽">* <city>* <name>合肥市</name>* </city>* <city>* <name>蚌埠市</name>* </city>* </province>* </china>*/@Testpublic void func5(){//得到省份listList<Province>provinceList=getProvince();//创建XSTream对象XStream xStream=new XStream();//将生成的xml内容对应的标签替换别名xStream.alias("china",List.class);xStream.alias("province",Province.class);xStream.alias("city",City.class);//隐藏掉省份中的城市集合属性xStream.addImplicitCollection(Province.class,"arrayList");//把province类的成员,变成province标签的属性xStream.useAttributeFor(Province.class,"name");//去掉city中的descriptionxStream.omitField(City.class,"description");//得到省份对象的xmlString s=xStream.toXML(provinceList);System.out.println(s);}
}
这篇关于JavaWeb之XStream使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!