xStream转换XML、JSON

前端之家收集整理的这篇文章主要介绍了xStream转换XML、JSON前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一. 简介

xStream可以很容易实现Java对象和xml文档互相转换, 可以修改某个特定的属性和节点名称,xStream提供annotation注解,

可以在JavaBean中完成对xml节点和属性的描述,并支持Json的转换,只需要提供相关的JSONDriver就能完成转换

官方网站:http://xstream.codehaus.org/tutorial.html


二. 准备工作

1. 环境准备:

Jar文件下载地址:

https://nexus.codehaus.org/content/repositories/releases/com/thoughtworks/xstream/xstream-distribution/1.3.1/xstream-distribution-1.3.1-bin.zip

代码结构图:



2. junit测试代码

public class XStreamTest {
	private XStream xstream;
	private ObjectOutputStream out;
	private ObjectInputStream in;
	private Student student;

	/**
	 * 初始化资源准备
	 */
	@Before
	public void init() {
		try {
			xstream = new XStream(new DomDriver());
		} catch (Exception e) {
			e.printStackTrace();
		}
		student = new Student();
		student.setAddress("china");
		student.setEmail("jack@email.com");
		student.setId(1);
		student.setName("jack");
		Birthday birthday = new Birthday(); 
		birthday.setBirthday("2010-11-22");
		student.setBirthday(birthday);
	}

	/**
	 * 释放对象资源
	 */
	@After
	public void destory() {
		xstream = null;
		student = null;
		try {
			if (out != null) {
				out.flush();
				out.close();
			}
			if (in != null) {
				in.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.gc();
	}
	
	/**
	 * 打印字符串
	 */
	public final void print(String string) { 
		System.out.println(string);
	}
	
	/**
	 * 高亮字符串
	 */
	public final void highLight(String string) {
		System.err.println(string);
	}

}

3. 所需实体类:

(1)Student:

public class Student {
	private int id;
	private String name;
	private String email;
	private String address;
	private Birthday birthday;
	// getter and setter
	public String toString() {
		return this.name + "#" + this.id + "#" + this.address + "#" + this.birthday + "#" + this.email;
	}
}


(2)Birthday

public class Birthday {
	private String birthday;

	public Birthday() {
	}
	
	public Birthday(String birthday) {
		this.birthday = birthday;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
}

三 Java对象转为xml

1. 将JavaBean转成xml文档:

/** 
	 * Java对象转换成XML
	 */
	@Test
	public void writeBean2XML() {
		try {
			highLight("====== Bean -> XML ======");
			print("<!-- 没有重命名的XML -->");
			print(xstream.toXML(student));
			
			print("<!-- 重命名后的XML -->");
			// 类重命名
			xstream.alias("student",Student.class);
			xstream.alias("生日",Birthday.class);
			xstream.aliasField("生日",Student.class,"birthday");
			xstream.aliasField("生日",Birthday.class,"birthday");

			// 属性重命名
			xstream.aliasField("邮件","email");

			// 包重命名
			xstream.aliasPackage("zdp","com.zdp.domain");
			print(xstream.toXML(student));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

运行结果:

====== Bean -> XML ======
<!-- 没有重命名的XML -->
<com.zdp.domain.Student>
  <id>1</id>
  <name>jack</name>
  <email>jack@email.com</email>
  <address>china</address>
  <birthday>
    <birthday>2010-11-22</birthday>
  </birthday>
</com.zdp.domain.Student>
<!-- 重命名后的XML -->
<student>
  <id>1</id>
  <name>jack</name>
  <邮件>jack@email.com</邮件>
  <address>china</address>
  <生日>
    <生日>2010-11-22</生日>
  </生日>
</student>

第一份文档是没有经过修改重命名的文档, 按照原样输出

第二份文档的类、属性、包都经过了重命名


2. 将List集合转成xml文档:

/**
	 * 将List集合转换成XML对象
	 */
	@Test
	public void writeList2XML() {
		try {
			// 修改元素名称
			highLight("====== List --> XML ======");
			xstream.alias("beans",ListBean.class);
			xstream.alias("student",Student.class);
			ListBean listBean = new ListBean();
			listBean.setName("this is a List Collection");

			List<Object> list = new ArrayList<Object>();
			// 引用javabean
			list.add(student);
			list.add(student); 
			// list.add(listBean); 引用listBean,父元素

			student = new Student();
			student.setAddress("china");
			student.setEmail("tom@125.com");
			student.setId(2);
			student.setName("tom");
			Birthday birthday = new Birthday("2010-11-22"); 
			student.setBirthday(birthday);

			list.add(student);
			listBean.setList(list);
			
			// 将ListBean中的集合设置空元素,即不显示集合元素标签
	        // xstream.addImplicitCollection(ListBean.class,"list");
			
			// 设置reference模型
			xstream.setMode(XStream.ID_REFERENCES); // id引用
	        //xstream.setMode(XStream.NO_REFERENCES); // 不引用
	        //xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES); // 绝对路径引用
			
			// 将name设置为父类(Student)的元素的属性
	        xstream.useAttributeFor(Student.class,"name");
	        xstream.useAttributeFor(Birthday.class,"birthday");
			
			// 修改属性的name
			xstream.aliasAttribute("姓名","name");
			xstream.aliasField("生日","birthday");

			print(xstream.toXML(listBean));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

运行结果:

====== List --> XML ======
<beans id="1">
  <name>this is a List Collection</name>
  <list id="2">
    <student id="3" 姓名="jack">
      <id>1</id>
      <email>jack@email.com</email>
      <address>china</address>
      <birthday id="4" 生日="2010-11-22"/>
    </student>
    <student reference="3"/>
    <student id="5" 姓名="tom">
      <id>2</id>
      <email>tom@125.com</email>
      <address>china</address>
      <birthday id="6" 生日="2010-11-22"/>
    </student>
  </list>
</beans>

3. 在JavaBean中添加Annotation注解进行重命名设置

(1)JavaBean代码

@XStreamAlias("class")
public class Classes {

	@XStreamAsAttribute
	@XStreamAlias("名称")
	private String name;

	@XStreamOmitField
	private int number;

	@XStreamImplicit(itemFieldName = "Students")
	private List<Student> students;

	@XStreamConverter(SingleValueCalendarConverter.class)
	private Calendar created = new GregorianCalendar();

	public Classes() {
	}

	public Classes(String name,Student... stu) {
		this.name = name;
		this.students = Arrays.asList(stu);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}

	public Calendar getCreated() {
		return created;
	}

	public void setCreated(Calendar created) {
		this.created = created;
	}
	
}

(2)编写类型转换器:

public class SingleValueCalendarConverter implements Converter {
	public void marshal(Object source,HierarchicalStreamWriter writer,MarshallingContext context) {
		Calendar calendar = (Calendar) source;
		writer.setValue(String.valueOf(calendar.getTime().getTime()));
	}

	public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(new Date(Long.parseLong(reader.getValue())));
		return calendar;
	}

	public boolean canConvert(Class type) {
		return type.equals(GregorianCalendar.class);
	}
}

(3)测试代码

/**
	 * 使用注解将List转为XML文档
	 */
	@Test
	public void writeList2XML4Annotation() {
		try {
			highLight("====== annotation Bean --> XML ======");
			Student stu = new Student();
			stu.setName("jack");
			Classes c = new Classes("一班",student,stu);
			c.setNumber(2);
			xstream.alias("student",Student.class);
			print(xstream.toXML(c));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
运行结果:

====== annotation Bean --> XML ======
<com.zdp.domain.Classes>
  <name>一班</name>
  <number>2</number>
  <students class="java.util.Arrays$ArrayList">
    <a class="student-array">
      <student>
        <id>1</id>
        <name>jack</name>
        <email>jack@email.com</email>
        <address>china</address>
        <birthday>
          <birthday>2010-11-22</birthday>
        </birthday>
      </student>
      <student>
        <id>0</id>
        <name>jack</name>
      </student>
    </a>
  </students>
  <created>
    <time>1409821431920</time>
    <timezone>Asia/Shanghai</timezone>
  </created>
</com.zdp.domain.Classes>

4. 将Map集合转成xml文档:

@H_403_179@/** * 将Map集合转成XML文档 */ @Test public void writeMap2XML() { try { highLight("====== Map --> XML ======"); Map<String,Student> map = new HashMap<String,Student>(); map.put("No.1",student); student = new Student(); student.setAddress("china"); student.setEmail("tom@125.com"); student.setId(2); student.setName("tom"); Birthday day = new Birthday("2010-11-22"); student.setBirthday(day); map.put("No.2",student); student = new Student(); student.setName("jack"); map.put("No.3",student); xstream.alias("student",Student.class); xstream.alias("key",String.class); xstream.useAttributeFor(Student.class,"id"); xstream.useAttributeFor("birthday",String.class); print(xstream.toXML(map)); } catch (Exception e) { e.printStackTrace(); } }运行结果:

====== Map --> XML ======
<map>
  <entry>
    <key>No.3</key>
    <student id="0">
      <name>jack</name>
    </student>
  </entry>
  <entry>
    <key>No.1</key>
    <student id="1">
      <name>jack</name>
      <email>jack@email.com</email>
      <address>china</address>
      <birthday birthday="2010-11-22"/>
    </student>
  </entry>
  <entry>
    <key>No.2</key>
    <student id="2">
      <name>tom</name>
      <email>tom@125.com</email>
      <address>china</address>
      <birthday birthday="2010-11-22"/>
    </student>
  </entry>
</map>

5.用OutStream输出流写XML

/**
	 * 用OutStream输出流写XML
	 */
	@Test
	public void writeXML4OutStream() {
		try {
			out = xstream.createObjectOutputStream(System.out);
			Student stu = new Student();
			stu.setName("jack");
			Classes c = new Classes("一班",stu);
			c.setNumber(2);
			highLight("====== ObjectOutputStream ## JavaObject--> XML ======");
			out.writeObject(stu);
			out.writeObject(new Birthday("2010-05-33"));
			out.write(22);//byte
			out.writeBoolean(true);
			out.writeFloat(22.f);
			out.writeUTF("hello");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
运行结果:

====== ObjectOutputStream ## JavaObject--> XML ======
<object-stream>
  <com.zdp.domain.Student>
    <id>0</id>
    <name>jack</name>
  </com.zdp.domain.Student>
  <com.zdp.domain.Birthday>
    <birthday>2010-05-33</birthday>
  </com.zdp.domain.Birthday>
  <byte>22</byte>
  <boolean>true</boolean>
  <float>22.0</float>
  <string>hello</string>
</object-stream>


四. xml文档转为Java对象:

1. 用inputStream将XML文档转换为Java对象

/**
	 * 用InputStream将XML文档转换成java对象 
	 */
	@Test
	public void readXML4InputStream() {
	    try {
	        String s = "<object-stream><com.zdp.domain.Student><id>0</id><name>jack</name>" +
	          "</com.zdp.domain.Student><com.zdp.domain.Birthday><birthday>2010-05-33</birthday>" +
	          "</com.zdp.domain.Birthday><byte>22</byte><boolean>true</boolean><float>22.0</float>" +
	          "<string>hello</string></object-stream>";
	        highLight("====== ObjectInputStream## XML --> javaObject ======");
	        StringReader reader = new StringReader(s);
	        in = xstream.createObjectInputStream(reader);
	        Student stu = (Student) in.readObject();
	        Birthday b = (Birthday) in.readObject();
	        byte i = in.readByte();
	        boolean bo = in.readBoolean();
	        float f = in.readFloat();
	        String str = in.readUTF();
	        System.out.println(stu);
	        System.out.println(b);
	        System.out.println(i);
	        System.out.println(bo);
	        System.out.println(f);
	        System.out.println(str);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}
运行结果:

====== ObjectInputStream## XML --> javaObject ======
jack#0#null#null#null
com.zdp.domain.Birthday@27391d
22
true
22.0
hello

2. 将XML文档转为Java对象:

/**
	 * 将XML文档转换成Java对象
	 */
	@Test
	public void readXml2Object() {
	    try {
	        highLight("====== Xml >>> Bean ======");
	        Student stu = (Student) xstream.fromXML(xstream.toXML(student));
	        print(stu.toString());
	        
	        List<Student> list = new ArrayList<Student>();
	        list.add(student);//add
	        
	        Map<String,Student>();
	        map.put("No.1",student);//put
	        
	        student = new Student();
	        student.setAddress("china");
	        student.setEmail("tom@125.com");
	        student.setId(2);
	        student.setName("tom");
	        Birthday day = new Birthday("2010-11-22");
	        student.setBirthday(day);
	        list.add(student);//add
	        map.put("No.2",student);//put
	        
	        student = new Student();
	        student.setName("jack");
	        list.add(student);//add
	        map.put("No.3",student);//put
	        
	        highLight("====== XML >>> List ======"); 
	        List<Student> studetns = (List<Student>) xstream.fromXML(xstream.toXML(list));
	        print("size:" + studetns.size());//3
	        for (Student s : studetns) {
	            print(s.toString());
	        }
	        
	        highLight("====== XML >>> Map ======");
	        Map<String,Student> maps = (Map<String,Student>) xstream.fromXML(xstream.toXML(map));
	        print("size:" + maps.size());//3
	        Set<String> key = maps.keySet();
	        Iterator<String> iter = key.iterator();
	        while (iter.hasNext()) {
	            String k = iter.next();
	            print(k + ":" + map.get(k));
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}
运行结果:

====== Xml >>> Bean ======
jack#1#china#com.zdp.domain.Birthday@1125127#jack@email.com
====== XML >>> List ======
size:3
jack#1#china#com.zdp.domain.Birthday@134bed0#jack@email.com
tom#2#china#com.zdp.domain.Birthday@1db4f6f#tom@125.com
jack#0#null#null#null
====== XML >>> Map ======
size:3
No.3:jack#0#null#null#null
No.1:jack#1#china#com.zdp.domain.Birthday@1d520c4#jack@email.com
No.2:tom#2#china#com.zdp.domain.Birthday@2a5330#tom@125.com

五. xStream对JSON的支持

xStream对JSON也有非常好的支持,它提供了2个模型驱动。用这2个驱动可以完成Java对象到JSON的相互转换。使用JettisonMappedXmlDriver驱动,将Java对象转换成json,需要添加jettison.jar

1.用JettisonMappedXmlDriver完成Java对象到JSON的转换

/**
	 * XStream结合JettisonMappedXmlDriver驱动,转换Java对象到JSON
	 */
	@Test
	public void writeEntity2JETTSON() {
	    highLight("====== JettisonMappedXmlDriver === JavaObject >>>> JaonString ======");
	    xstream = new XStream(new JettisonMappedXmlDriver());
	    xstream.setMode(XStream.NO_REFERENCES);
	    xstream.alias("student",Student.class);
	    print(xstream.toXML(student));
	}
运行结果:

====== JettisonMappedXmlDriver === JavaObject >>>> JaonString ======
{"student":{"id":1,"name":"jack","email":"jack@email.com","address":"china","birthday":[{},"2010-11-22"]}}

2. 用JsonHierarchicalStreamDriver完成Java对象到JSON的转换

/**
	 * 转换java对象为JSON字符串
	 */
	@Test
	public void writeEntiry2JSON() {
	    highLight("====== JsonHierarchicalStreamDriver === JavaObject >>>> JaonString ======");
	    xstream = new XStream(new JsonHierarchicalStreamDriver());
	    xstream.alias("student",Student.class);
	    highLight("-------Object >>>> JSON---------");
	    print(xstream.toXML(student));
	    
	    //删除根节点
	    xstream = new XStream(new JsonHierarchicalStreamDriver() {
	        public HierarchicalStreamWriter createWriter(Writer out) {
	            return new JsonWriter(out,JsonWriter.DROP_ROOT_MODE);
	        }
	    });
	    
	    xstream.alias("student",Student.class);
	    print(xstream.toXML(student));
	}
运行结果:
====== JsonHierarchicalStreamDriver === JavaObject >>>> JaonString ======
-------Object >>>> JSON---------
{"student": {
  "id": 1,"name": "jack","email": "jack@email.com","address": "china","birthday": {
    "birthday": "2010-11-22"
  }
}}
{
  "id": 1,"birthday": {
    "birthday": "2010-11-22"
  }
}

使用JsonHierarchicalStreamDriver转换默认会给转换后的对象添加一个根节点,但是在构建JsonHierarchicalStreamDriver驱动的时候,

你可以重写createWriter方法,删掉根节点。


3.将List集合转换成JSON串

/**
	 * 将List集合转换成JSON字符串
	 */
	@Test
	public void writeList2JSON() {
	    highLight("===== JsonHierarchicalStreamDriver ==== JavaObject >>>> JaonString =====");
	    JsonHierarchicalStreamDriver driver = new JsonHierarchicalStreamDriver();
	    xstream = new XStream(driver);
	    // xstream = new XStream(new JettisonMappedXmlDriver());//转换错误
	    // xstream.setMode(XStream.NO_REFERENCES);
	    xstream.alias("student",Student.class);
	    
	    List<Student> list = new ArrayList<Student>();
	    list.add(student);
	    
	    student = new Student();
	    student.setAddress("china");
	    student.setEmail("tom@125.com");
	    student.setId(2);
	    student.setName("tom");
	    Birthday day = new Birthday("2010-11-22");
	    student.setBirthday(day);
	    list.add(student);
	    
	    student = new Student();
	    student.setName("jack");
	    list.add(student);
	    
	    print(xstream.toXML(list));
	    
	    //删除根节点
	    xstream = new XStream(new JsonHierarchicalStreamDriver() {
	        public HierarchicalStreamWriter createWriter(Writer out) {
	            return new JsonWriter(out,JsonWriter.DROP_ROOT_MODE);
	        }
	    });
	    xstream.alias("student",Student.class);
	    print(xstream.toXML(list));
	}
运行结果:

===== JsonHierarchicalStreamDriver ==== JavaObject >>>> JaonString =====
{"list": [
  {
    "id": 1,"birthday": {
      "birthday": "2010-11-22"
    }
  },{
    "id": 2,"name": "tom","email": "tom@125.com",{
    "id": 0,"name": "jack"
  }
]}
[
  {
    "id": 1,"name": "jack"
  }
]

4. 将Map转换成json串:

/**
	 * 将Map集合转换成JSON字符串
	 */
	@Test
	public void writeMap2JSON() {
	    highLight("==== JsonHierarchicalStreamDriver ==== Map >>>> JaonString =====");
	    xstream = new XStream(new JsonHierarchicalStreamDriver());
	    xstream.alias("student",Student.class);
	    
	    Map<String,Student>();
	    map.put("No.1",student);
	    
	    student = new Student();
	    student.setAddress("china");
	    student.setEmail("tom@125.com");
	    student.setId(2);
	    student.setName("tom");
	    student.setBirthday(new Birthday("2010-11-21"));
	    map.put("No.2",student);
	    
	    student = new Student();
	    student.setName("jack");
	    map.put("No.3",student);
	    
	    print(xstream.toXML(map));
	    
	    //删除根节点
	    xstream = new XStream(new JsonHierarchicalStreamDriver() {
	        public HierarchicalStreamWriter createWriter(Writer out) {
	            return new JsonWriter(out,JsonWriter.DROP_ROOT_MODE);
	        }
	    });
	    xstream
运行结果:

==== JsonHierarchicalStreamDriver ==== Map >>>> JaonString =====
{"map": [
  [
    "No.3",{
      "id": 0,"name": "jack"
    }
  ],[
    "No.1",{
      "id": 1,"birthday": {
        "birthday": "2010-11-22"
      }
    }
  ],[
    "No.2",{
      "id": 2,"birthday": {
        "birthday": "2010-11-21"
      }
    }
  ]
]}
[
  [
    "No.3","birthday": {
        "birthday": "2010-11-21"
      }
    }
  ]
]


5. 将JSON转换成Java对象:

/**
	 * 将JSON字符串转换成java对象
	 */
	@Test
	public void readJSON2Object() throws JSONException {
	    String json = "{student: {" +
	        "id: 1," +
	        "name: haha," +
	        "email: email," +
	        "address: address," +
	        "birthday: {" +
	           "birthday: 2010-11-22 " +
	        "}" +
	    "}}";
	    xstream = new XStream(new JettisonMappedXmlDriver());
	    xstream.alias("student",Student.class);
	    print(xstream.fromXML(json).toString());
	    
	    json = "{list: [{" +
		            "id: 1," +
		            "name: haha," +
		            "email: email," +
		            "address: address," +
		            "birthday: {" +
		              "birthday: 2010-11-22" +
		            "}" +
	           "},{" +
		            "id: 2," +
		            "name: tom," +
		            "email: tom@125.com," +
		            "address: china," +
		            "birthday: {" +
		              "birthday: 2010-11-22" +
		            "}" +
	             "}" +
	          "]}";
	    System.out.println(json); 
	    List list = (List) xstream.fromXML(json);
	    System.out.println(list.size());
	}
运行结果:

haha#1#address#com.zdp.domain.Birthday@137c60d#email
{list: [{id: 1,name: haha,email: email,address: address,birthday: {birthday: 2010-11-22}},{id: 2,name: tom,email: tom@125.com,address: china,birthday: {birthday: 2010-11-22}}]}
0


三. 遇到的问题

1. 如何加上xml头部?即<?xml version="1.0" encoding="UTF-8"?>

官方文档是这样解释的:

Why does XStream not write an XML declaration?
XStream is designed to write XML snippets,so you can embed its output into an existing stream or string.

You can write the XML declaration yourself into the Writer before using it to call XStream.toXML(writer).

我们可以自己添加:XmlDeclarationXStream

public class XmlDeclarationXStream extends XStream {
	private String version;

	private String ecoding;

	public XmlDeclarationXStream() {
		this("1.0","utf-8");
	}

	public XmlDeclarationXStream(String version,String ecoding) {
		this.version = version;
		this.ecoding = ecoding;
	}

	public String getDeclaration() {
		return "<?xml version=\"" + this.version + "\" encoding=\"" + this.ecoding + "\"?>";
	}

	@Override
	public void toXML(Object obj,OutputStream output) { 
		try {
			String dec = this.getDeclaration();
			byte[] bytesOfDec = dec.getBytes(this.ecoding);
			output.write(bytesOfDec);
		} catch (Exception e) {
			throw new RuntimeException("error happens",e);
		}
		super.toXML(obj,output);
	}

	@Override
	public void toXML(Object obj,Writer writer) {
		try {
			writer.write(getDeclaration()); 
		} catch (Exception e) {
			throw new RuntimeException("error happens",writer);
	}
}
测试的时候我们new这个类:XStream xstream = new XmlDeclarationXStream();

源码下载:http://download.csdn.net/detail/zdp072/7866129

原文:http://blog.csdn.net/IBM_hoojo/article/details/6342386

猜你在找的XML相关文章