Saturday 6 May 2017

Javax.xml.bind.JAXBElement Class

Javax.xml.bind.JAXBElement Class


The javax.xml.bind.JAXBElement class is the JAXB representation of an Xml Element.This class represents information about an Xml Element from both the element declaration within a schema and the element instance value within an xml document with the following properties:

1.   element's xml tag name
2.   value represents the element instance's atttribute(s) and content model
3.   element declaration's declaredType  (xs:element @type attribute)
4.   scope of element declaration
5.   boolean nil property. (element instance's xsi:nil attribute)

 

Class declaration:      


Following is the declaration for javax.xml.bind.JAXBElement class:

public class JAXBElement<T>
   extends Object
      implements Serializable


Field


Following are the fields for javax.xml.bind.JAXBElement class:



protected Class<T> declaredType − This is the Java datatype binding for xml element declaration's type

protected QName name − This is the xml element tag name.

protected boolean nil −   This is true if the xml element instance has          
                                   xsi:nil="true".

protected Class scope −  This is the scope of xml element declaration
                                  representing this xml element instance.

protected T value − This is the xml element value.



Methods inherited


This class inherits methods from the following classes −
javax.xml.Object

Example



The following example shows the usage of java.xml.bind.JAXBElement class. To proceed, consider the following Employee class which will be used to have objects for marshalling and unmarshalling purposes.


Employee.java



package com.jaxb.demo;

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 double employeeSal; 
       /**
        * @return the employeeId
        */
       public int getEmployeeId() {
             return employeeId;
       } 
       /**
        * @param employeeId the employeeId to set
        */
       @XmlAttribute
       public void setEmployeeId(int employeeId) {
             this.employeeId = employeeId;
       } 
       /**
        * @return the employeeName
        */
       public String getEmployeeName() {
             return employeeName;
       } 
       /**
        * @param employeeName the employeeName to set
        */
       @XmlElement
       public void setEmployeeName(String employeeName) {
             this.employeeName = employeeName;
       } 
       /**
        * @return the employeeSal
        */
       public double getEmployeeSal() {
             return employeeSal;
       } 
       /**
        * @param employeeSal the employeeSal to set
        */
       @XmlElement
       public void setEmployeeSal(double employeeSal) {
             this.employeeSal = employeeSal;
       }

}

Now let us create main class which will be used to marshal ie. convert Student object to XML. Here we will create a JAXBElement of a Student object and then update StringWriter object using JAXBContext. This example marshals the Student object and prints the XML.


JAXBDemoApp.java


package com.jaxb.demo;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;

public class JAXBDemoApp {

       public static void main(String[] args) {
             //Create a employee object
             Employee employee = new Employee();

             //fill details of the employee
             employee.setEmployeeName("Best employee");
             employee.setEmployeeId(1);
             employee.setEmployeeSal(12000.00);

             try {

                 /*
                                         *  create JAXBElement of type Student
                                             Pass it the employee object
                                          */
                JAXBElement<Employee> jaxbElement =  new JAXBElement(
                   new QName(Employee.class.getSimpleName()), Employee.class,
                                                       employee); 
                /*
                                      * Create a String writer object which will be
                                          used to write jaxbElment XML to string
                                         */
                StringWriter writer = new StringWriter();

                // create JAXBContext which will be used to update writer            
                JAXBContext context = JAXBContext.newInstance(Employee.class);

                // marshal or convert jaxbElement containing student to xml format
                context.createMarshaller().marshal(jaxbElement, writer);
            
                //print XML string representation of Student object             
                System.out.println( writer.toString() );

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




No comments:

Post a Comment