Thursday 16 September 2010

XStream – One example

Here I'll talk about my last experience using the small, however very powerful marshalling/unmarshalling XML framework, the Xstream, you can check it out into http://xstream.codehaus.org/ .
First of all, this framework has a very small API, especially for persistence and it's very powerful and fast to export your objects for one XML file, though to read it you need use JAXP, JAXB, IO or ant other library that read XML code, even the NIO from Java 6 will work.
You can download the binaries from here.
After unzip the file in your choose destination, you will have into the folder this tree, here in my case I'm using the version 1.3.1 :

xstream-1.3.1    
  doc API's and tutorials
  libs .jars / libs
  LICENSE.txt Text files
  README.txt Text files


You will require xstream-[version].jar and xpp3-[version].jar in the classpath. XPP3 is a very fast XML pull-parser implementation. If you do not want to include this dependency, you can use a standard JAXP DOM parser instead.

And here our bind classes, pojos :


ShipTo.java :

public class ShipTo {
    private String name;
    private String address;
    private String city;
    private String country;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
} 



 


Item.java :


public class Item {
    private String title;
    private String note;
    private String quantity;
    private String price;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getNote() {
        return note;
    }
    public void setNote(String note) {
        this.note = note;
    }
    public String getQuantity() {
        return quantity;
    }
    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
} 



 

ShipOrder.java :

public class ShipOrder {
    private String orderid;
    private String orderperson;
    private ShipTo shipto;
    private List<Item> item;
    public String getOrderid() {
        return orderid;
    }
    public void setOrderid(String orderid) {
        this.orderid = orderid;
    }
    public String getOrderperson() {
        return orderperson;
    }
    public void setOrderperson(String orderperson) {
        this.orderperson = orderperson;
    }
    public ShipTo getShipto() {
        return shipto;
    }
    public void setShipto(ShipTo shipto) {
        this.shipto = shipto;
    }
    public List<Item> getItem() {
        return item;
    }
    public void setItem(List<Item> item) {
        this.item = item;
    }
} 



 


Now our class that will serialize to one String as XML and then de-serialize to Objects.


   1:  /*
   2:   * To change this template, choose Tools | Templates
   3:   * and open the template in the editor.
   4:   */
   5:  package xstreamtest;
   6:   
   7:  import com.thoughtworks.xstream.XStream;
   8:  import java.util.ArrayList;
   9:  import java.util.List;
  10:   
  11:  /**
  12:   *
  13:   * @author Klaus
  14:   */
  15:  public class Main {
  16:   
  17:     /**
  18:      * @param args the command line arguments
  19:      */
  20:     public static void main(String[] args) {
  21:        // Getting the Xstream
  22:        XStream xstream = Main.getStream();
  23:   
  24:        // Serializing the object to XML
  25:        ShipTo shipTo = Main.getShipTo();
  26:        List<Item> listOfItems = Main.getItems();
  27:        ShipOrder shipOrder = Main.getShipOrder(shipTo, listOfItems);
  28:   
  29:        // Serializing to the XML into one String, can create file if you want it.
  30:        String shipOrderXml = xstream.toXML(shipOrder);
  31:   
  32:        // Display the XML created
  33:        System.out.println("The XML :");
  34:        System.out.println(shipOrderXml);
  35:   
  36:        // Populating the obj again
  37:        ShipOrder shipOrder1 = (ShipOrder) xstream.fromXML(shipOrderXml);
  38:        ShipTo shipTo1 = shipOrder1.getShipto();
  39:        List<Item> listOfItems1 = shipOrder1.getItem();
  40:   
  41:        System.out.println("-------------------------------");
  42:        System.out.println("- Desirializing to the object -");
  43:        System.out.println("-------------------------------");
  44:        System.out.println("Order Id     : " + shipOrder1.getOrderid());
  45:        System.out.println("Order Person : " + shipOrder1.getOrderperson());
  46:        System.out.println("Ship To      : " + shipOrder1.getShipto().getName());
  47:        System.out.println("Items : ");
  48:        for (Item item : listOfItems1) {
  49:           System.out.println("Title : " + item.getTitle());
  50:           System.out.println("Qtde. : " + item.getQuantity());
  51:           System.out.println("Price : " + item.getPrice());
  52:           System.out.println("Note  : " + item.getNote());
  53:           System.out.println("-----------------------------------------");
  54:        }
  55:     }
  56:   
  57:     public static XStream getStream() {
  58:        XStream xstream = new XStream();
  59:        xstream.alias("shipTo", ShipTo.class);
  60:        xstream.alias("item", Item.class);
  61:        xstream.alias("shipOrder", ShipOrder.class);
  62:        return xstream;
  63:     }
  64:   
  65:     public static List<Item> getItems() {
  66:        List<Item> items = new ArrayList<Item>();
  67:        Item item = new Item();
  68:        item.setTitle("Item 01");
  69:        item.setPrice("10.00");
  70:        item.setQuantity("5");
  71:        item.setNote("This is the first item");
  72:        items.add(item);
  73:   
  74:        item = new Item();
  75:        item.setTitle("Item 02");
  76:        item.setPrice("15.00");
  77:        item.setQuantity("7.50");
  78:        item.setNote("This is the second item");
  79:        items.add(item);
  80:   
  81:        return items;
  82:     }
  83:   
  84:     public static ShipTo getShipTo() {
  85:        ShipTo shipTo = new ShipTo();
  86:        shipTo.setName("Customer 01");
  87:        shipTo.setCity("City 01");
  88:        shipTo.setCountry("country 01");
  89:        shipTo.setAddress("Address 01");
  90:        return shipTo;
  91:     }
  92:   
  93:     public static ShipOrder getShipOrder(ShipTo shipTo, List<Item> items) {
  94:        ShipOrder so = new ShipOrder();
  95:        so.setOrderid("Order number 01");
  96:        so.setOrderperson("Order to Person 01");
  97:        so.setShipto(shipTo);
  98:        so.setItem(items);
  99:        return so;
 100:     }
 101:  }




 


As you can see, is quite easy, serialize as de-serialize the XML with Xstream.


The framework is quite small though powerful and easy to use, make it as one excellent option.

No comments:

Post a Comment