XStream Framework Uses

Source: Internet
Author: User
Tags json

Original address: http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html

XStream Framework

XStream can easily convert Java objects and XML documents to each other, and can modify a particular attribute and node name, and also support JSON conversion;

Previous Json-lib this framework, online blog: http://www.cnblogs.com/hoojo/archive/2011/04/21/2023805.html

And Jackson this 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. To some extent, the description of the Java object is limited, and the XML is not fully embodied in the description of the Java object. Here's how XStream's best support for JSON and XML is covered. XStream is not only very friendly to the transformation of XML, but also provides annotation annotations, which can be used to describe XML nodes and attributes in JavaBean. and also support for JSON, you can complete the conversion simply by providing the relevant jsondriver.

First, preparatory work

1, download jar package, and official resources

XStream 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 and you can start the work below; 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;
/**
* <b>function:</b>java between objects and XML strings
* jar-lib-version:xstream-1.3.1
* @author Hoojo
* @createDate Nov 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 Resource Preparation
     * @author Hoojo
     * @createDate Nov 12:16:28 PM
     */
    @Before
    public void init () {
        try {
            XStream = new XStream ();
            XStream = new XStream (new Domdriver ()); Need XPP3 jar
        catch (Exception e) {
            E.printstacktrace ();
        }
        Bean = new Student ();
        Bean.setaddress ("the");
        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> Releasing Object resources
     * @author Hoojo
     * @createDate Nov 12:16:38 PM
     */
    @After
    public void Destory () {
        XStream = null;
        Bean = null;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.