How do I convert an XML persistence to Java Bean?

In the previous example you can see how to convert a bean into an XML persistence. Now we’ll do the opposite, converting the XML back to a bean. For the BeanToXML class use in this example please refer to How do I convert a bean to XML persistence? example.

package org.kodejava.example.bean;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;

public class XmlToBean {
    public static void main(String[] args) {
        XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
                XmlToBean.class.getResourceAsStream("Bean.xml")));

        //
        // Reads the next object from the underlying input stream.
        //
        BeanToXML bean = (BeanToXML) decoder.readObject();
        decoder.close();

        System.out.println("ID              = " + bean.getId());
        System.out.println("Item Name       = " + bean.getItemName());
        System.out.println("Item Colour     = " + bean.getItemColour());
        System.out.println("Item Quantities = " + bean.getItemQuantities());
    }
}

Here is our Bean.xml persistence file:

<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.6.0_02" class="java.beans.XMLDecoder"> 
 <object class="org.kodejava.example.bean.BeanToXML"> 
  <void property="id"> 
   <long>1</long> 
  </void> 
  <void property="itemColour"> 
   <string>Dark Red</string> 
  </void> 
  <void property="itemName"> 
   <string>T-Shirt</string> 
  </void> 
  <void property="itemQuantities"> 
   <int>100</int> 
  </void> 
 </object> 
</java> 

The result are:

ID              = 1
Item Name       = T-Shirt
Item Colour     = Dark Red
Item Quantities = 100

How do I convert a bean to XML persistence?

package org.kodejava.example.bean;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;

public class BeanToXML {
    private Long id;
    private String itemName;
    private String itemColour;
    private Integer itemQuantities;

    public static void main(String[] args) {
        BeanToXML bean = new BeanToXML();
        bean.setId(new Long(1));
        bean.setItemName("T-Shirt");
        bean.setItemColour("Dark Red");
        bean.setItemQuantities(new Integer(100));
        
        try {
            XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
                    new FileOutputStream("Bean.xml")));

            //
            // Write an XML representation of the specified object to the output.
            //
            encoder.writeObject(bean);
            encoder.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemColour() {
        return itemColour;
    }

    public void setItemColour(String itemColour) {
        this.itemColour = itemColour;
    }

    public Integer getItemQuantities() {
        return itemQuantities;
    }

    public void setItemQuantities(Integer itemQuantities) {
        this.itemQuantities = itemQuantities;
    }
}

The XML persistence will be like:

<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.6.0_02" class="java.beans.XMLDecoder"> 
 <object class="org.kodejava.example.bean.BeanToXML"> 
  <void property="id"> 
   <long>1</long> 
  </void> 
  <void property="itemColour"> 
   <string>Dark Red</string> 
  </void> 
  <void property="itemName"> 
   <string>T-Shirt</string> 
  </void> 
  <void property="itemQuantities"> 
   <int>100</int> 
  </void> 
 </object> 
</java>