Showing posts with label JAX-B. Show all posts
Showing posts with label JAX-B. Show all posts

JAX-B UnMarshalling: XML document into Java Objects

JAX-B UnMarshalling: XML document into Java Objects
JAX-B API provides javax.xml.bind.Unmarshaller interface, It is used to un marshal(read) the xml document into Java Objects. In this example, we are going to discuss about how to read contents from xml into java objects using JAX-B.
Steps to convert java object into XML document.
  • Create POJO or bind the schema and generate the classes.
  • Create the JAXBContext object.
  • Create the UnMarshaller objects.
  • Call the unmarshal method
  • Use getter methods of POJO to access the data
XMLDocument:Employee.xml

    25
    IT
    Jhon klazck

Create Employee POJO class
SOURCE:Employee.java
package com.becbe.jaxb.ch1;

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

@XmlRootElement
public class Employee {
 private int employeeId;
 private String employeeName;
 private int age;
 private String dept;
 /**
  * @return the employeeId
  */
 @XmlAttribute
 public int getEmployeeId() {
  return employeeId;
 }
 /**
  * @param employeeId the employeeId to set
  */
 public void setEmployeeId(int employeeId) {
  this.employeeId = employeeId;
 }
 /**
  * @return the employeeName
  */
 @XmlElement
 public String getEmployeeName() {
  return employeeName;
 }
 /**
  * @param employeeName the employeeName to set
  */
 public void setEmployeeName(String employeeName) {
  this.employeeName = employeeName;
 }
 /**
  * @return the age
  */
 @XmlElement
 public int getAge() {
  return age;
 }
 /**
  * @param age the age to set
  */
 public void setAge(int age) {
  this.age = age;
 }
 /**
  * @return the dept
  */
 @XmlElement
 public String getDept() {
  return dept;
 }
 /**
  * @param dept the dept to set
  */
 public void setDept(String dept) {
  this.dept = dept;
 }

}

SOURCE:XmlToObject.java
/**
 * 
 */
package com.becbe.jaxb.ch1;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/**
 * @author BECBE
 *
 */
public class XmlToObject {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  try {
   File xmlDocFile = new File("d:\\Employee.xml");
   //Create JAX-B instance
   JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
   //Create UnMarshaller Instance
   Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
   //Un  marshalling the xml document to Employee pojo
   Employee employee=(Employee) unmarshaller.unmarshal(xmlDocFile);
   System.out.println("Name : "+employee.getEmployeeName());
   System.out.println("Id : "+employee.getEmployeeId());
   System.out.println("Age : "+employee.getAge());
   System.out.println("Department : "+employee.getDept());
   
  } catch (JAXBException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

}



OUTPUT:
Name : Jhon klazck
Id : 23456
Age : 25
Department : IT


JAX-B Marshalling: Java Object into XML document

JAX-B Marshalling: Java Object into XML document
JAX-B API provides Marshaller interface, It is used to marshal(write) the object into xml document. In this example, we are going to discuss about conversion of the object into xml using JAX-B.

Steps to convert java object into XML document.

  • Create POJO or bind the schema and generate the classes.
  • Create the JAXBContext object.
  • Create the Marshaller objects.
  • Create the content tree by using set methods.
  • Call the marshal method.

Before we looking in to example. First we need to know about annotations which requires to define the xml doc information in POJO.Some of the annotations are listed below

  • @XmlRootElement specifies the root element for the xml document.

  • @XmlAttribute specifies the attribute for the root element.

  • @XmlElement specifies the sub element for the root element.

Create Employee POJO class

SOURCE:Employee.java
SOURCE:ObjectToXML.java
OUTPUT:


    25
    IT
    Jhon klazck


JAX-B Introduction

JAX-B it is an acronym of Java API for XML Binding. It is a convenient framework to process XML documents.
JAX-B provides a mechanism to marshal java objects into XML document and un-marshals XML document in to Java Objects.
JAX-B mainly used for three operations.which are listed below
  • Marshalling a tree of objects into an XML document
  • Unmarshalling an XML document into a tree of objects.
    1. It includes validation of the XML against the schema
    2. It used to generate the classes of the objects
  • javascript:void(0);
  • Validation of object trees against the schema used to generate their classes
    1. some constraints are enforced while working with the objects
    2. others are only enforced when validation is requested

The following diagram describes JAX-B architecture.


New features in JAXB 2.0 over JAXB 1.0
  • Parameterized types:JAX-B supports parameterized types, The advantage of parameterized types is compile time type checking.

  • JAXB 2.0 supports all of XML Schema constructs, JAXB 1.0 doesn't.
  • With JAXB 2.0 fewer Java classes are generated from an XML Schema as compared to JAXB 1.0. For each top level complexType, a value class is generated, instead of an interface and an implementation class. For each top-level element, a factory class method is generated, instead of an interface and an implementation class. Fewer runtime libraries are required for JAXB 2.0.
  • Annotation: In JAXB 2.0, support for binding Java to XML has been added with javax.xml.bind.annotation package. Annotations provide a metadata facility with which code may be generated using annotation types and annotation declarations.
  • WebSerive support: JAXB 2.0 provides integration with the Java API for XML-based Web Services (JAX-WS) 2.0.

  • References