- Javax.xml.bind classes
- Home
- Binder
- DatatypeConverter
- JAXB
- JAXBContext
- JAXBElement
- JAXBElement.GlobalScope
- JAXBIntrospector
- Marshaller.Listener
- SchemaOutputResolver
- Unmarshaller.Listener
- Javax.xml.bind.util classes
- JAXBResult
- JAXBSource
- ValidationEventCollector
- Javax.xml.parsers classes
- DocumentBuilder
- DocumentBuilderFactory
- SAXParser
- SAXParserFactory
- Javax.xml.soap classes
- AttachmentPart
- MessageFactory
- MimeHeader
- MimeHeaders
- SAAJMetaFactory
- SOAPConnection
- SOAPConnectionFactory
- SOAPFactory
- SOAPMessage
- SOAPPart
- Javax.xml.validation classes
- Schema
- SchemaFactory
- TypeInfoProvider
- Validator
- ValidatorHandler
- Javax.xml.xpath classes
- XPathConstants
- XPathFactory
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
Javax.xml.bind.Unmarshaller.Listener.afterUnmarshal() Method
Description
The Javax.xml.bind.Unmarshaller.Listener.afterUnmarshal(Object target, Object parent) callback method invoked after unmarshalling XML data into target.
This method is invoked after all the properties (except IDREF) are unmarshalled into target, but before target is set into its parent object. Note that if the class of target defines its own afterUnmarshal method, the class specific callback method is invoked before this method is invoked.
Declaration
Following is the declaration for javax.xml.bind.Unmarshaller.Listener.afterUnmarshal(Object target, Object parent) method
public void afterUnmarshal(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.afterUnmrarshal(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 afterUnmarshal 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 afterUnmarshal(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 = 5 Column number = 11 System Id = null Public Id = null Location Uri= null CharacterOffset = 127
To Continue Learning Please Login
Login with Google