Wednesday 11 December 2013

JAXB

JAXB 


Hello Friends.what you will do if you want to marshal /unmarshal java objects ? well,there are 2-3 techniques which comes into your mind that is .(1)Sax Parser,(2)Dom Parser ,(3)JAXB



JAXB is binding api which is used to marshal & unmarshal java objects into/from xml.well the class which is annotated with javax.xml.binding annotations can only be processed by jaxb runtime .so whatever  field you will declare into java class ,that everything is processed by jaxb engine. i am going to show you simple annotations which will be used in code:


@XmlRootElement=> Root Element of xml File

@XmlType(propOrder={"countryname","CountryPopulation","listofStates"}) => Used to specify ordering of xml elements.

@XmlElement=> used to specify xml element .

 JAXB Architecture:

Sample Code :
 package org.demo;

import java.io.File;
import org.apache.commons.logging.log4j.*;
import java.util.*;
import java.util.logging.Logger;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class JavatoXml
{
    public static void main(String args[])
    {
      
         //Logger log = Logger.getLogger(
                //JavatoXml.class.getName());
      
            Country india=new Country();
          
            india.setCountryName("India");
            india.setCountryPopulation(5000000);
          
            List<State> states= new ArrayList<State>();
   
            State gujarat=new State();
          
            gujarat.setStatePopluation(6000000);
          
            states.add(gujarat);
          
            //india.setListofstates(states);
            try
            {
                JAXBContext jaxbcontext = JAXBContext.newInstance(Country.class);
              
                    Marshaller  jaxbmarsheller= jaxbcontext.createMarshaller();
                  
                    File f = new File("d:\\ocjp\\CountryRecord.xml");
                  
                    jaxbmarsheller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
                  
                jaxbmarsheller.marshal(india, f);
              
                System.out.println("Xml file created Successfully...!!!");
                  
            }
            catch(JAXBException  e)
            {
              
            }
          
          
    }

}

How Marshelling is done:
(1)
JAXBContext-> It provides an abstractinon for managing infomration necessary to 
implement the jaxb.it informs JAXB runtime that this object or class will be bind into
xml
 
(2) 
Marsheller->it tells JAXB runtime to marshell given instance of class.
 
 
Sample Code:
 

package org.demo;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.util.*;


@XmlRootElement

@XmlType(propOrder={"countryname","CountryPopulation","listofStates"})




public class Country
{
 
 
 @XmlElement
 private String countryName;
 @XmlElement
 private double countryPopulation;
 @XmlElement
 private ArrayList<State> listofstates;
 
 public Country() { }
 
 public String getCountryName() {
  return countryName;
 }

 @XmlElement
 public void setCountryName(String countryName) {
  this.countryName = countryName;
 }

 public double getCountryPopulation() {
  return countryPopulation;
 }

 @XmlElement
 public void setCountryPopulation(double countryPopulation) {
  this.countryPopulation = countryPopulation;
 }

 public ArrayList<State> getListofstates() 
 {
  return listofstates;
 }

 @XmlElementWrapper(name="stateList")
 @XmlElement(name="state")
 public void setListofstates(ArrayList<State> listofstates) {
  this.listofstates = listofstates;
 }

 
 
 
 
 

}
package org.demo;

import javax.xml.bind.annotation.XmlRootElement;



@XmlRootElement(namespace="org.demo.Country")
public class State
{
 
 private String statename;
 private long statePopluation;
 
 
 public State() { }
 public String getStatename() {
  return statename;
 }
 public void setStatename(String statename) {
  this.statename = statename;
 }
 public long getStatePopluation() {
  return statePopluation;
 }
 public void setStatePopluation(long statePopluation) {
  this.statePopluation = statePopluation;
 }

 
 

}


package org.demo;

import java.io.File;
import org.apache.commons.logging.log4j.*;
import java.util.*;
import java.util.logging.Logger;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class JavatoXml 
{
 public static void main(String args[])
 {
  
   Country india=new Country();
   
   india.setCountryName("India");
   india.setCountryPopulation(5000000);
   
   List<State> states= new ArrayList<State>();
 
   State gujarat=new State();
   
   gujarat.setStatePopluation(6000000);
   
   states.add(gujarat);
   
   //india.setListofstates(states);
   try
   {
    JAXBContext jaxbcontext = JAXBContext.newInstance(Country.class);
    
     Marshaller  jaxbmarsheller= jaxbcontext.createMarshaller();
     
     File f = new File("d:\\ocjp\\CountryRecord.xml");
     
     jaxbmarsheller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
     
    jaxbmarsheller.marshal(india, f);
    
    System.out.println("Xml file created Successfully...!!!");
     
   }
   catch(JAXBException  e)
   {
    
   }
   
   
 }

}

this is how java object is marshaled into xml .

thanks
for any query ping me on pathak.nisarg@yahoo.com
 
 
 
 
 
 
 
 
 

No comments:

Post a Comment

Spring Boot SSL configuration -Tomcat Server

Hi Friends hope you all are doing well. Today I am going to demonstrate about how to configure SSL in Spring boot web Application. Need o...