XStream Quick Guide to converting XML to Java objects

Source: Internet
Author: User
Tags object serialization

XStream is a simple and practical Java class Library that is used to convert between a serialized object and an XML object. Because the XStream configuration is simple and flexible to use, there is a lot of space in the XML object and the Java object serialization market. This article does not elaborate on the features of XStream, but shows how easy it is to demonstrate xstream as an example.


Download the latest version of the jar package on the 1.XStream website and upload it to the project path.


2. Suppose we want to convert the following XML object to a Java object.

<?xml version= "1.0" encoding= "UTF-8"?><order> <orderId>201612150001</orderId> < orderprice>40.23</orderprice> <createDate>20161215180000</createDate> <product> &L    T;productname>lvrouhuoshao</productname> <productPrice>23</productPrice> </product>     <product> <productName> Egg Filling </productName> <productPrice>17.23</productPrice> </product></order>


3. Build order objects and product objects based on XML structure


Hiorder.java

package com.favccxx.favsoft.pojo;import java.util.list;public class hiorder {     private string orderid;    private double orderprice ;    private list


Product.java

package com.favccxx.favsoft.pojo;public class hiproduct  {    private String productName;    private  Double productprice;    public string getproductname ()  {         return productName;    }     Public void setproductname (String productname)  {         this.productName = productName;    }    public  Double getproductprice ()  {        return productPrice;     }    public void setproductprice (double  Productprice)  {        this.productprice = productprice ;     }} 


4. Test code

package com.favccxx.favsoft.main;import com.favccxx.favsoft.pojo.hiorder;import  com.favccxx.favsoft.pojo.hiproduct;import com.thoughtworks.xstream.xstream;public class  Mainorder {    public static void main (String[] args)  {         String xmlbody =  "<?xml version= ' 1.0 '  encoding= ' UTF-8 '?><order><orderid>201612150001</orderid><orderprice>40.23< /orderprice><createdate>2016-12-12 15:16:04</createdate><product><productname> lvrouhuoshao</productname><productprice>23</productprice></product><product>< Productname> Egg-filling cake </productname><productprice>17.23</productprice></product></ Order> ";         xstream xstream = new xstream () ;        xstream.alias ("Order",  hiorder.class);         xstream.alias ("Product",  hiproduct.class);         Xstream.addimplicitcollection (hiorder.class,  "Product");         hiorder order =  (Hiorder)  xstream.fromxml (xmlbody);         system.out.println (Order.getorderid ());     }}


5. Run the above test class to output the details of the order. You can convert an XML object to a Java object in just a few simple steps, and you see this, are you itching to try it out?



This article is from the "This person's IT World" blog, be sure to keep this source http://favccxx.blog.51cto.com/2890523/1884453

XStream Quick Guide to converting XML to Java objects

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.