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



Description

The Javax.xml.bind.JAXBIntrospector.isElement(Object object) method return true if object represents a JAXB element.

Parameter object is a JAXB element for following cases −

  • It is an instance of javax.xml.bind.JAXBElement.

  • The class of object is annotated with @XmlRootElement.

Declaration

Following is the declaration for javax.xml.bind.JAXBIntrospector.isElement(Object object) method

public abstract boolean isElement(Object object)

Parameters

object − object represents a JAXBElement.

Return Value

This method returns true if the object represents a JAXB element.

Exception

NA

Example

The following example shows the usage of javax.xml.bind.JAXBIntrospector.isElement(Object object) 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. Here we will creat Binder object using JAXBContext because we can not create Binder object directly because it is an abstract class. This example marshals the Student object. The JAXBIntrospector is created to verify if the unmarshalled object represents a JAXBElement.

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);      
               
         // returns true if object represents a JAXBElement
         b = introspector.isElement(element);
         
         System.out.println("1. Introspector created successfully");
         
         if(b) {
            System.out.print("2. Recognized root of unmarshalled");
            System.out.println(" object tree as element");
         } else {
            System.out.print("2. Could not recognize root of");
            System.out.println(" unmarshalled object tree as element");
         }
      }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 −

1. Introspector created successfully
2. Recognized root of unmarshalled object tree as element
javax_xml_bind_jaxbintrospector.htm
Advertisements