Using XStream to convert between JavaBean and Xml/json

Source: Internet
Author: User
Tags json

XML and JSON are two commonly used formats for data description and transmission today, especially when it comes to JS using JSON quite frequently. Naturally, in the Java world, it is the XStream and json-lib that are the components that JavaBean and the two formats convert to each other. Here I jot down the usage of xstream.

In fact, similar tools have already existed. If you have used DWR comrades, there must be a print, DWR remote method calls can also be completed for you JavaBean and JSON format two-way conversion, depends on its various Converter. If you pay attention to the details of the Struts1, Struts1 's Actionservlet the Commons-digester to complete the conversion of XML to JavaBean when initializing Struts-config.xml. The corresponding Apache also has a commons-betwixt implementation of JavaBean to XML generation.

And I am here to say XStream (http://xstream.codehaus.org) JavaBean and Xml/json between the two-way conversion all realized, and Json-lib as its name, the function is too shabby. To use XStream, download to the XStream package, the current version is 1.3.1. The Xstream-1.x.x.jar is then added to the project's Classpath and is not dependent on other packages. Other packages in the Lib directory are required at certain times, as mentioned below.

Briefly explain the use of XStream, divided into JavaBean-> XML, JavaBean-> JSON, XML-> JavaBean, JSON-> JavaBean several parts of the content. Before starting the example, define three classes (all in the Com.unmi.model package):

01.public class Customer {
02.    private int custId;
03.    private String custName;
04.    private List<Order> orders;
05.    //setter/getter 和构造方法略
06.}
07.
08.public class Order {
09.    private int orderId;
10.    private String orderName;
11.    private Product[] products;
12.    //setter/getter 和构造方法略
13.}
14.
15.public class Product {
16.    private int prodId;
17.    private String prodName;
18.    private double prodPrice;
19.    //setter/getter 和构造方法略
20.}

Customer/order/product, the relationship between them is One-to-many, One-to-many, and for demonstration purposes, the List and array are used as aggregation attributes respectively.

1. JavaBean-> XML

01.public static void main(String[] args) {
02.
03.    //构造接近实际的 Customer 对象
04.    Product p1 = new Product(1001,"电脑",4000);
05.    Product p2 = new Product(1002,"空调",2000);
06.    Product[] prods1 = new Product[]{p1,p2};
07.
08.    Order order1 = new Order(101,"电器类",prods1);
09.
10.    List<Order> orders = new ArrayList<Order>();
11.    orders.add(order1); 
12.    Customer customer = new Customer(1,"Unmi",orders);
13.
14.    //XStream xstream = new XStream();
15.    XStream xstream = new XStream(new DomDriver());
16.
17.    String xml = xstream.toXML(customer);//转换成 xml 格式
18.
19.    System.out.println(xml); //输出 xml 字符串
20.}

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.