(Java) JAXB _ Java Architecture for XML Binding

Java Architecture for XML Binding (JAXB) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects. In other words, JAXB allows storing and retrieving data in memory in any XML format, without the need to implement a specific set of XML loading and saving routines for the program’s class structure. It is similar to xsd.exe and XmlSerializer in the .NET Framework.
JAXB is particularly useful when the specification is complex and changing. In such a case, regularly changing the XML Schema definitions to keep them synchronised with the Java definitions can be time consuming and error prone.
JAXB is a part of the Java SE platform and one of the APIs in the Java EE platform, and is part of the Java Web Services Development Pack (JWSDP). It is also one of the foundations for WSIT. JAXB is part of SE version 1.6.
JAXB 1.0 was developed under the Java Community Process as JSR 31.[1] As of 2006, JAXB 2.0 is being developed under JSR 222.[2] Reference implementations for these specifications are available under the CDDL open source license at java.net. (wikipedia.com)

default data tyoe Binding

Kalau Menurut saya JAXB ( Java Architecture for XML Binding) Merupakan arsitektur xml yang di buat menggunakan java , kalau lebih sederhananya konversi java ke xml , bukan juga sih , kita membuat entitas menggunakan java dan akan di eksekusi oleh API JAXB dan akan di buat struktur xml nya hingga dapat di jalankan . gitu aja sih , karena kemaren saya menyinggung postingan sebelumnya antara XML dan JSON dan kedepannya saya akan selalu memposting antara kedua nya di teknologi masing masih , ini baru permulaan dan akan ada lagi yang lebih seru… tunggu hingga akhir nya XML dan JSON saling di satukan.

Download API JAXB di situs resminya http://jaxb.java.net/

Buat Project dengan nama JAXBExample

Buat Class Dengan Nama Customer

Disini kita akan membuat entitas menggunakan JAXB sebelumnya lihat table di bawah ini :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jaxb_helloworld;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author mrroot
 */

@XmlRootElement
public class Customer {
    String name;
    int age;
    int id;
    
    public String getName(){
        return name;
    }
    @XmlElement
            public void setName(String name){
            this.name= name;
    }
    public int getAge(){
        return age;
    }
    @XmlElement
            public void setAge(int age){
            this.age = age;
    }
    public int getId(){
        return id;
    }
    @XmlAttribute
            public void setId(int id){
            this.id = id;
    }
    
    
}

Buka File Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jaxb_helloworld;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author mrroot
 */
public class JAXB_HelloWorld {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        Customer cs = new Customer();
        cs.setId(100);
        cs.setName("Aerdy");
        cs.setAge(20);
        try {
            
            File file = new File ("/home/mrroot/file.xml");//alamat direktory file yang akan di simpan
            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            
            //output untuk melakukan konversi menggunakan marshaller
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(cs, file);
            jaxbMarshaller.marshal(cs, System.out);
            
            //output shell
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
            System.out.println(customer);
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Jalankan dan hasil nya ada di directori anda simpan sebelumnya ..

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
    <age>20</age>
    <name>Aerdy</name>
</customer>

Referensi :
http://www.mkyong.com/java/jaxb-hello-world-example/
http://www.vogella.com/articles/JAXB/article.html

Advertisements

Published by

jarcode

hello I am jarcode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s