How do I convert a bean to XML persistence?

package org.kodejava.example.bean;

import java.beans.XMLEncoder;

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.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.
        } catch (FileNotFoundException e) {

    public Long getId() {
        return id;

    public void setId(Long 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"> 
  <void property="itemColour"> 
   <string>Dark Red</string> 
  <void property="itemName"> 
  <void property="itemQuantities"> 

Wayan Saryada

Programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply