Marshelling and Unmarshelling Document using JAXB

Marshelling is a process of transforming java objects into XML format. and Unmarshalling is the process of converting an XML document into a corresponding set of Java objects.

Concept 1 : Marshelling Document
Create JAXBContext object as per your project as
JAXBContext jaxbContext;
jaxbContext = JAXBContext.newInstance("com.anuj.jaxb");

Once your domain object is initialized, use the JAXB context to create a Marshaller object and a typed JAXBElement. Creating the marshaller is simple:
Marshaller marshaller = jaxbContext.createMarshaller();

You can Set a property so that the output will be formatted for human use and then write to standard output
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal( bookingElement, System.out );  Outout will be XML.


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

import com.anuj.entity.Customer;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author Anuj
 */
public class MarshellingExample {

    public static void main(String[] args) {
        try {
            JAXBContext jAXBContext = JAXBContext.newInstance(Customer.class);
            Marshaller marshaller = jAXBContext.createMarshaller();

            Customer customer = new Customer();
            customer.setCustomerId("1");
            customer.setCustomerName("anuj");
            customer.setAge(25);

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            marshaller.marshal(customer, new File("customer.xml"));
            marshaller.marshal(customer, System.out);

        } catch (JAXBException ex) {
            Logger.getLogger(MarshellingExample.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


Output :

    25
    1
    anuj

BUILD SUCCESSFUL (total time: 0 seconds)

Concept 2 : Unmarshelling Document
To unmarshall an XML document, you create an Unmarshaller from the context, as shown here:
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

package com.anuj.xml;

import com.anuj.entity.Customer;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/**
 * Convert XML to Object
 *
 * @author Anuj
 *
 */
public class UnMarshallingExample {

    /**
     * @param args
     */
    public static void main(String[] args) {

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
            File file = new File("customer.xml");

            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Customer customer = (Customer) unmarshaller.unmarshal(file);

            System.out.println("Customer Id : " + customer.getCustomerId());
            System.out.println("Customer Name : " + customer.getCustomerName());
            System.out.println("Customer age : " + customer.getAge());


        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }
}


Output :
run:
Customer Id : 1
Customer Name : anuj
Customer age : 25
BUILD SUCCESSFUL (total time: 0 seconds)

Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/

No comments:

Post a Comment