Original address: http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html
XStream Frame
XStream can easily convert Java objects and XML documents to each other, and can modify a particular attribute and node name, and also support the conversion of JSON;
There is an introduction to json-lib this framework, online blog: http://www.cnblogs.com/hoojo/archive/2011/04/21/2023805.html
And the Jackson Framework, online blog: http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html
They all support JSON perfectly, but support for XML is not very good. The description of the Java object is limited to a certain extent, and the XML is not fully represented in the description of the Java object. This will introduce XStream's perfect support for JSON and XML. XStream is not only very friendly to the conversion of XML, but also provides annotation annotations, which can be used to describe XML nodes and attributes in JavaBean. As well as JSON support, the conversion can be done with only the relevant jsondriver.
First, the preparatory work
1, download the jar package, and official resources
XStream's jar download address:
Https://nexus.codehaus.org/content/repositories/releases/com/thoughtworks/xstream/xstream-distribution/1.3.1/ Xstream-distribution-1.3.1-bin.zip
The official example is full, official reference example: http://xstream.codehaus.org/tutorial.html
Add the Xstream-1.3.1.jar file to the project, you can start the following work; the required jar is as follows:
2. Test Case Code
Package com.hoo.test;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import Java.io.StringReader;
Import Java.io.Writer;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;
Import org.codehaus.jettison.json.JSONException;
Import Org.junit.After;
Import Org.junit.Before;
Import Org.junit.Test;
Import Com.hoo.entity.Birthday;
Import com.hoo.entity.Classes;
Import Com.hoo.entity.ListBean;
Import com.hoo.entity.Student;
Import Com.thoughtworks.xstream.XStream;
Import Com.thoughtworks.xstream.io.HierarchicalStreamWriter;
Import Com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
Import Com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
Import Com.thoughtworks.xstream.io.json.JsonWriter;
/**
* Conversion of <b>function:</b>java objects and XML strings to each other
* jar-lib-version:xstream-1.3.1
* @author Hoojo
* @createDate 12:15:15 PM
* @file Xstreamtest.java
* @package Com.hoo.test
* @project Webhttputils
* @blog Http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
@SuppressWarnings ("Unchecked")
public class Xstreamtest {
Private XStream XStream = null;
Private ObjectOutputStream out = null;
Private ObjectInputStream in = null;
Private Student bean = null;
/**
* <b>function:</b> Initialization of resource preparation
* @author Hoojo
* @createDate 12:16:28 PM
*/
@Before
public void init () {
try {
XStream = new XStream ();
XStream = new XStream (new Domdriver ()); Requires XPP3 jar
} catch (Exception e) {
E.printstacktrace ();
}
Bean = new Student ();
Bean.setaddress ("China");
Bean.setemail ("jack@email.com");
Bean.setid (1);
Bean.setname ("Jack");
Birthday day = new Birthday ();
Day.setbirthday ("2010-11-22");
Bean.setbirthday (day);
}
/**
* <b>function:</b> Release Object resources
* @author Hoojo
* @createDate 12:16:38 PM
*/
@After
public void Destory () {
XStream = null;
Bean = null;