Javax.xml.bind.JAXBIntrospector.getValue() Method



Description

The Javax.xml.bind.JAXBIntrospector.getValue(Object jaxbElement) method get the element value of a JAXB element.

Convenience method to abstract whether working with either a javax.xml.bind.JAXBElement instance or an instance of @XmlRootElement annotated Java class.

Declaration

Following is the declaration for javax.xml.bind.JAXBIntrospector.getValue(Object jaxbElement) method

public static Object getValue(Object jaxbElement)

Parameters

jaxbElement − is an object that isElement(Object) returned true.

Return Value

This method returns the element value of the jaxbElement.

Exception

NA

Example

The following example shows the usage of javax.xml.bind.JAXBIntrospector.getValue(Object jaxbElement) method. To proceed, consider the following Student class which will be used to have objects for marshalling purpose −

package com.tutorialspoint.po;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
public class Student{
 
   String name;
   int age;
   public 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;
   }
}

Now let us create main class which will be used to unmarshal ie. convert Student.xml into a JAXBObject.

package com.tutorialspoint;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBIntrospector;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class JAXBIntrospectorDemo {
   public static void main(String[] args) {
      
      StreamSource s = null;
      boolean b = false;
      
      try{
         // Create JAXBContext
         JAXBContext jc = JAXBContext.newInstance(Student.class);
         JAXBIntrospector introspector = jc.createJAXBIntrospector();
         
         // create unmarshaller
         Unmarshaller u = jc.createUnmarshaller();

            // create stream source
         File f = new File ("Student.xml");
         s = new StreamSource(f);
         
         // create element from unmarshalling
         JAXBElement<Student> element= u.unmarshal(s, Student.class);      
               
         // get value
         Student student = (Student) introspector.getValue(element);
         
         // print 
         System.out.println("Name: "+student.getName());
         System.out.print("Age: "+student.getAge());
         
      }catch(Exception e){
         e.printStackTrace();
      }
   }
}

Following is the Student.xml as input to the main class.

<?xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<student id = "10">
   <age>12</age>
   <name>Malik</name>
</student>

Before we proceed for compilation, we need to make sure that that we download JAXB2.xxx.jar and put it in our CLASSPATH. Once setup is ready, let us compile and run the above program, this will produce the following result −

Name: Malik
Age: 12
javax_xml_bind_jaxbintrospector.htm
Advertisements