import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XppDomDriver;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.junit.Test;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.*;
public class XMLEncoderAndXMLDecoder {
@Test
public void xmlEncoder(){
try {
/**
* 把对象转成XML文件存储
*/
//定义输出流对象
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("test.xml"));
XMLEncoder xmle = new XMLEncoder(out);
Person p = new Person();
p.setPersonid("1212");
p.setName("Jerry");
p.setAddress("shenzhen");
p.setTel("123456");
p.setFax("987654");
p.setEmail("com.vince");
xmle.writeObject(p);
xmle.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void xmlDecoder(){
try {
/**
* 把XML文件解析成对象
*/
BufferedInputStream in = new BufferedInputStream(new FileInputStream("test.xml"));
XMLDecoder xmld = new XMLDecoder(in);
Person person = (Person)xmld.readObject();
System.out.println(person);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* xstream解析和生成XML文件{需要下载jar包:xstream-1.2.2.jar 和 xpp3-1.1.4-min.jar}
*/
@Test
public void xstreamTOxml(){
Person p = new Person();
p.setPersonid("1212");
p.setName("Jerry");
p.setAddress("shenzhen");
p.setTel("123456");
p.setFax("987654");
p.setEmail("com.vince");
XStream xs = new XStream(new XppDriver()); //或者:XStream xs = new XStream(new XppDomDriver());
//为Person这个类名设置别名:person在XML文件中即这个:<person>
//如果不设置别名,生成的xml是带包名的:<com.vince.XMLAndJSONDemo.Person>
xs.alias("person", Person.class);
//为Person类名设置属性值:"personid"
xs.useAttributeFor(Person.class,"personid");
String xml = xs.toXML(p);
System.out.println(xml);
/**
* xstream解析xml文件生成对象
*/
Person person = (Person)xs.fromXML(xml);
System.out.println(person);
}
}
Person.java
package com.vince.XMLAndJSONDemo;
public class Person {
private String personid;
private String name;
private String address;
private String tel;
private String fax;
private String email;
public Person() {
}
public Person(String personid, String name, String address, String tel, String fax, String email) {
this.personid = personid;
this.name = name;
this.address = address;
this.tel = tel;
this.fax = fax;
this.email = email;
}
public String getPersonid() {
return personid;
}
public void setPersonid(String personid) {
this.personid = personid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getFax() {
return fax;
}
public void setFax(String fax) {
this.fax = fax;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Person{" +
"personid='" + personid + '\'' +
", name='" + name + '\'' +
", address='" + address + '\'' +
", tel='" + tel + '\'' +
", fax='" + fax + '\'' +
", email='" + email + '\'' +
'}';
}
}
XStream 详细解析XML和写入XML(供其它类方法调用此方法的)
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.vince.bean.Clothes;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
//解析XML的工具类 {这里用到XStream ,XPP3 工具包}
public class ProductsXmlUtils {
//解析XML文件生成对象集合List<Clothes>
public static List<Clothes> parseProductsFromXml(){
List<Clothes> products = new ArrayList<>();
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream("products.xml"));
XStream xStream = new XStream(new XppDriver());
//为xml文件里的根节点标签名即<list> 设置一个别名"list",如果不设置,生成的xml文件中,根节点会带上包名<.包名.list>
//所以这里products就可以看作是这个List<Clothes>集合的一个对象:要得到类的class就可以使用:对象.getClass()
xStream.alias("list", products.getClass());
//为xml的根节点的子节点clothes设置别名“clothes”
xStream.alias("clothes", Clothes.class);
//为某个类设置属性:所要执行类的Class{Clothes.class},然后取这个类里的哪个属性值{id}
xStream.useAttributeFor(Clothes.class, "id");
products=((List<Clothes>) xStream.fromXML(in));
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return products;
}
//向xml文件中写对象数据
public static void writeProductsToXml(List<Clothes> products){
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("products.xml"));
//因为一个xml文件头部是固定的,所以可以单独写上如下一句
out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>".getBytes());
XStream xStream = new XStream(new XppDriver());
//为xml文件里的根节点标签名即<list> 设置一个别名"list",如果不设置,生成的xml文件中,根节点会带上包名<.包名.list>
//所以这里products就可以看作是这个List<Clothes>集合的一个对象:要得到类的class就可以使用:对象.getClass()
xStream.alias("list", products.getClass());
//为xml的根节点的子节点clothes设置别名“clothes”
xStream.alias("clothes", Clothes.class);
//为某个类设置属性:所要执行类的Class{Clothes.class},然后取这个类里的哪个属性值{id}
xStream.useAttributeFor(Clothes.class, "id");
xStream.toXML(products,out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<list>
<clothes id="A0001">
<brand>VEROMODA</brand>
<style>连衣裙</style>
<color>白色</color>
<size>S</size>
<num>4</num>
<price>274.0</price>
<description>夏季五分袖连衣裙</description>
</clothes>
<clothes id="A0002">
<brand>GAP</brand>
<style>连衣裙</style>
<color>黑白色</color>
<size>S</size>
<num>19</num>
<price>164.0</price>
<description>系带无袖宽松夏季柔软碎花背心裙</description>
</clothes>
</list>
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。