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



Description

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

Declaration

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

public abstract QName getElementName(Object jaxbElement)

Parameters

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

Return Value

This method returns xml element qname associated with jaxbElement; null if jaxbElement is not a JAXB Element.

Exception

NA

Example

The following example shows the usage of javax.xml.bind.JAXBIntrospector.getElementName(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. 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 return the QName for the unmarshalled object.

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.namespace.QName;
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 element name
         QName qname = introspector.getElementName(element);
         
         // print local part
         System.out.print("Local Part: "+qname.getLocalPart());
         
      }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 −

Local Part: student
javax_xml_bind_jaxbintrospector.htm
Advertisements