Javax.xml.bind.Unmarshaller.Listener.beforeUnmarshal() Method



Description

The Javax.xml.bind.Unmarshaller.Listener.beforeUnmarshal(Object target, Object parent) callback method invoked before unmarshalling into target.

This method is invoked immediately after target was created and before the unmarshalling of this object begins. Note that if the class of target defines its own beforeUnmarshal method, the class specific callback method is invoked before this method is invoked.

Declaration

Following is the declaration for javax.xml.bind.Unmarshaller.Listener.beforeUnmarshal(Object target, Object parent) method

public void beforeUnmarshal(Object target, Object parent)

Parameters

  • target − non-null instance of JAXB mapped class prior to unmarshalling into it.

  • parent − instance of JAXB mapped class that will reference target. null when target is root element.

Return Value

This method does not return any value.

Exception

NA

Example

The following example shows the usage of javax.xml.bind.Unmarshaller.Listener.beforeUnmarshal(Object target, Object parent) method. To proceed, consider the following Student class which will be used to have objects for unmarshalling purpose −

package com.tutorialspoint;

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;
   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 an JAXB context object. This example unmarshals the Student.xml and prints the location of the unmarshalled objects.

package com.tutorialspoint;
 
import java.io.FileInputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
 
public class UnmarshallerListenerDemo {
   public static void main(String[] args) throws Exception {
      
      try {
         // create JAXBContext
         JAXBContext jc = JAXBContext.newInstance(Student.class);
         
         // create xml input factory
         XMLInputFactory xif = XMLInputFactory.newFactory();
         
         // create xml stream reader from Student.xml
         FileInputStream xml = new FileInputStream("Student.xml");
         XMLStreamReader xsr = xif.createXMLStreamReader(xml);
         
         // create unmarshaller
         Unmarshaller unmarshaller = jc.createUnmarshaller();
         
         // create location listener
         LocationListener ll = new LocationListener(xsr);
         unmarshaller.setListener(ll);
         
         // unmarshal Student.xml to student object
         Student student = (Student)unmarshaller.unmarshal(xsr);
         System.out.println(ll.getLocation(student));
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

Through Location.Listener class we will call the beforeUnmarshal event to record the location from the XMLStreamReader while the object is instantiated.

package com.tutorialspoint;

import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.Unmarshaller.Listener;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamReader;

public class LocationListener extends Listener {
   private XMLStreamReader xsr;
   private Map<Object, Location> locations;
   
   public LocationListener(XMLStreamReader xsr) {
      this.xsr = xsr;
      this.locations = new HashMap<Object, Location>();
   }
   
   @Override
   public void beforeUnmarshal(Object target, Object parent) {
      locations.put(target, xsr.getLocation());
   }

   public Location getLocation(Object o) {
      return locations.get(o);
   }
}

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 −

Line number = 2
Column number = 18
System Id = null
Public Id = null
Location Uri= null
CharacterOffset = 74
javax_xml_bind_unmarshaller.listener.htm
Advertisements