Beranda > Java Tutorial, XML > serialize object to XML is easy with xstream

serialize object to XML is easy with xstream


public bingung cara parsing dari object sebuah class ke XML?

sebenarnya gampang gunakan aja library yang namanya JAXB, tapi sayang penggunaannya agak ribet :D

tapi tenang, sekarang untuk parsing dari sebuah object ke file XML lebih gampang!!

gak percaya?

kalo gak percaya silahkan download aja framework yang namanya XStream dan coba rasakan kemudahannya seperti pada artikel ini :D

 

sebelumnya kita buat dulu 2 buah class yang bernama Address dan Person seperti dibawah ini :

Address :

package com.nohnit.xtream;

public class Address {

    private String street;

    private String city;

    private String country;

    private String postalCode;

    public 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;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Address other = (Address) obj;
        if ((this.street == null) ? (other.street != null) : !this.street.equals(other.street)) {
            return false;
        }
        if ((this.city == null) ? (other.city != null) : !this.city.equals(other.city)) {
            return false;
        }
        if ((this.country == null) ? (other.country != null) : !this.country.equals(other.country)) {
            return false;
        }
        if ((this.postalCode == null) ? (other.postalCode != null) : !this.postalCode.equals(other.postalCode)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 67 * hash + (this.street != null ? this.street.hashCode() : 0);
        hash = 67 * hash + (this.city != null ? this.city.hashCode() : 0);
        hash = 67 * hash + (this.country != null ? this.country.hashCode() : 0);
        hash = 67 * hash + (this.postalCode != null ? this.postalCode.hashCode() : 0);
        return hash;
    }
}

Person :

package com.nohnit.xtream;

public class Person {

    private int id;

    private String name;

    private Address address;

    public Person() {
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Person other = (Person) obj;
        if (this.id != other.id) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if (this.address != other.address && (this.address == null || !this.address.equals(other.address))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 19 * hash + this.id;
        hash = 19 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 19 * hash + (this.address != null ? this.address.hashCode() : 0);
        return hash;
    }
}

dan sekarang seperti inilah cara penggunaan XStream, misal kita akan mendapatkan String XML dari class Address dan Person :

package com.nohnit.xtream;

import com.thoughtworks.xstream.XStream;

public class Main {

    public static void main(String[] nohnit) {
        XStream xStream = new XStream();
        xStream.alias("address", Address.class);
        xStream.alias("person", Person.class);

        Person person = new Person();
        person.setId(1);
        person.setName("Eko Kurnaiwan Khannedy");

        Address address = new Address();
        address.setCity("Subang");
        address.setCountry("Indonesia");
        address.setPostalCode("41271");
        address.setStreet("Jl Raya Kalijati");
        person.setAddress(address);

        String hasil = xStream.toXML(person);
        System.out.println(hasil);
    }
}

dan setelah dicompile dan dijalankan, maka akan tertrace tulisan seperti dibawah ini :

  1
  Eko Kurnaiwan Khannedy
  <address>
    Jl Raya Kalijati
    Subang
    Indonesia
    41271
  </address>

bagaimana?

gampangkan? :D

Sumber: eecchhoo.wordpress.com

Kategori:Java Tutorial, XML
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: