XStream是一个开源项目,利用它可以方便的进行java对象和xml之间进行转换,从而在测试,服务器设置等方面有很好的应用。
先建两个java对象
1.Adress类
package xml_chb;
public class Adress {
private String provice;
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvice() {
return provice;
}
public void setProvice(String provice) {
this.provice = provice;
}
}
2.Person类
package xml_chb;
public class Person {
private String name="";
private String sex="";
private int age;
private String mobile;
private Adress adress;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Adress getAdress() {
return adress;
}
public void setAdress(Adress adress) {
this.adress = adress;
}
}
3.生成一个Person对象,并将其转化为xml
//生成一个java对象
Person person=new Person();
person.setName("chb");
person.setAge(22);
person.setSex("boy");
//生成一个Adress对象
Adress adr=new Adress();
adr.setProvice("Beijing");
adr.setCity("Beijing");
person.setAdress(adr);
//将java对象转化为xml
XStream xstream=new XStream(new DomDriver());
String strxml= xstream.toXML(person);
System.out.println(strxml);
输出:
<xml__chb.Person>
<name>chb</name>
<sex>boy</sex>
<age>22</age>
<adress>
<provice>Beijing</provice>
<city>Beijing</city>
</adress>
</xml__chb.Person>
我们可以看到,xml根结点是<xml_chb.Person>是类的全名,这样看起来很繁琐,我们可以使用别名来改进这一现象:
xstream.alias("person",Person.class);
strxml= xstream.toXML(person);
System.out.println(strxml);
输出结果为:
<person>
<name>chb</name>
<sex>boy</sex>
<age>22</age>
<adress>
<provice>Beijing</provice>
<city>Beijing</city>
</adress>
</person>
4.从xml转化为POJO
String str="<person><name>emily</name><sex>girl</sex><age>22</age><adress><provice>Beijing</provice><city>Beijing</city></adress></person>";
Person person1=(Person) xstream.fromXML(str);
System.out.println("姓名:"+person1.getName());
System.out.println("性别:"+person1.getSex());
输出结果为:
姓名:emily
性别:girl
5.修改元素值
Adress adr1=new Adress();
adr1.setProvice("Shanghai");
adr1.setCity("Shanghai");
person.setAdress(adr1);
strxml= xstream.toXML(person);
System.out.println(strxml);
输出结果:
<person>
<name>chb</name>
<sex>boy</sex>
<age>22</age>
<adress>
<provice>Shanghai</provice>
<city>Shanghai</city>
</adress>
</person>
XStream的jar包可以直接到http:// xstream.codehaus.org/下载,那里有一些简单例子
XStream是个很小的开源项目,所以它能实现的功能也就很有限,它只能包装简单类型的元素进行转换,复杂类型的,比如Calendar,Date等类型则需要用户自己实现Converter。
还有, XStream对xml的操作并不好,比如想把一个子节点变成属性就是一个很复杂的工作,但是大家平时用于测试的话,看看对象中到底存着什么数据,还是必要的。
先建两个java对象
1.Adress类
package xml_chb;
public class Adress {
private String provice;
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvice() {
return provice;
}
public void setProvice(String provice) {
this.provice = provice;
}
}
2.Person类
package xml_chb;
public class Person {
private String name="";
private String sex="";
private int age;
private String mobile;
private Adress adress;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Adress getAdress() {
return adress;
}
public void setAdress(Adress adress) {
this.adress = adress;
}
}
3.生成一个Person对象,并将其转化为xml
//生成一个java对象
Person person=new Person();
person.setName("chb");
person.setAge(22);
person.setSex("boy");
//生成一个Adress对象
Adress adr=new Adress();
adr.setProvice("Beijing");
adr.setCity("Beijing");
person.setAdress(adr);
//将java对象转化为xml
XStream xstream=new XStream(new DomDriver());
String strxml= xstream.toXML(person);
System.out.println(strxml);
输出:
<xml__chb.Person>
<name>chb</name>
<sex>boy</sex>
<age>22</age>
<adress>
<provice>Beijing</provice>
<city>Beijing</city>
</adress>
</xml__chb.Person>
我们可以看到,xml根结点是<xml_chb.Person>是类的全名,这样看起来很繁琐,我们可以使用别名来改进这一现象:
xstream.alias("person",Person.class);
strxml= xstream.toXML(person);
System.out.println(strxml);
输出结果为:
<person>
<name>chb</name>
<sex>boy</sex>
<age>22</age>
<adress>
<provice>Beijing</provice>
<city>Beijing</city>
</adress>
</person>
4.从xml转化为POJO
String str="<person><name>emily</name><sex>girl</sex><age>22</age><adress><provice>Beijing</provice><city>Beijing</city></adress></person>";
Person person1=(Person) xstream.fromXML(str);
System.out.println("姓名:"+person1.getName());
System.out.println("性别:"+person1.getSex());
输出结果为:
姓名:emily
性别:girl
5.修改元素值
Adress adr1=new Adress();
adr1.setProvice("Shanghai");
adr1.setCity("Shanghai");
person.setAdress(adr1);
strxml= xstream.toXML(person);
System.out.println(strxml);
输出结果:
<person>
<name>chb</name>
<sex>boy</sex>
<age>22</age>
<adress>
<provice>Shanghai</provice>
<city>Shanghai</city>
</adress>
</person>
XStream的jar包可以直接到http:// xstream.codehaus.org/下载,那里有一些简单例子
XStream是个很小的开源项目,所以它能实现的功能也就很有限,它只能包装简单类型的元素进行转换,复杂类型的,比如Calendar,Date等类型则需要用户自己实现Converter。
还有, XStream对xml的操作并不好,比如想把一个子节点变成属性就是一个很复杂的工作,但是大家平时用于测试的话,看看对象中到底存着什么数据,还是必要的。