- 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.Binder.getEventHandler() Method
Description
The Javax.xml.bind.Binder.getEventHandler() method returns the current event handler or the default event handler if one hasn't been set.
Declaration
Following is the declaration for javax.xml.bind.Binder.getEventHandler() method
public abstract ValidationEventHandler getEventHandler()
Parameters
NA
Return Value
This method returns the current ValidationEventHandler or the default event handler if it hasn't been set.
Exception
JAXBException − if an error was encountered while getting the current event handler.
Example
The following example shows the usage of javax.xml.bind.Binder.getEventHandler() method. To proceed, consider the following Student class which will be used to have objects for marshalling 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 Student object. This example unmarshals the Student.xml file and prints it at STDOUT.
package com.tutorialspoint; import java.io.File; import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Binder; import javax.xml.bind.ValidationEventHandler; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; public class BinderDemo { public static void main(String[] args) { try { // we need a blank document to store final xml output DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); Document document = docBuilder.parse("Student.xml"); // xml constant String schemaLanguage = XMLConstants.W3C_XML_SCHEMA_NS_URI; SchemaFactory sf = SchemaFactory.newInstance(schemaLanguage); File f = new File("Student.xsd"); // create new schema Schema schema = sf.newSchema(f); // create JAXBContext which will be used to create a Binder JAXBContext jc = JAXBContext.newInstance(Student.class); // create binder object Binder<Node> binder = jc.createBinder(); // set the property binder.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // get xml node Node xmlNode = document.getDocumentElement(); // set schema for binder binder.setSchema(schema); // set validation event handler binder.setEventHandler(new StudentValidationEventHandler()); ValidationEventHandler veh = binder.getEventHandler(); System.out.println("Class : "+veh.getClass().getName()); // unmarshaling xml to JAXB object Student st = (Student)binder.unmarshal(xmlNode); // finally print the marshalled object on stdout System.out.println("Name : "+st.getName()); System.out.println("Age : "+st.getAge()); }catch(Exception ex) { ex.printStackTrace(); } } }
StudentValidationEventHandler class is created to set new event handler to the binder class.
package com.tutorialspoint; import javax.xml.bind.ValidationEvent; import javax.xml.bind.ValidationEventHandler; public class StudentValidationEventHandler implements ValidationEventHandler { public boolean handleEvent(ValidationEvent event) { System.out.println("\nEvent"); System.out.println("Severity: " + event.getSeverity()); System.out.println("Message: " + event.getMessage()); System.out.println("Linked Exception: " + event.getLinkedException()); System.out.println("LOCATOR"); System.out.println(" Line Number: " + event.getLocator().getLineNumber()); System.out.println(" Column Number: " + event.getLocator().getColumnNumber()); System.out.println(" Offset: " + event.getLocator().getOffset()); System.out.println(" Object: " + event.getLocator().getObject()); System.out.println(" Node: " + event.getLocator().getNode()); System.out.println(" Url: " + event.getLocator().getURL()); return true; } }
The Student.xml is used for unmarshalling. Below are the content details of the file −
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes"?> <student id = "10"> <age>12</age> <name>Malik</name> </student>
The following Student.xsd is implemented in the program to set schema in the binder object.
<?xml version = "1.0" encoding = "utf-8"?> <xs:schema attributeFormDefault = "unqualified" elementFormDefault = "qualified" xmlns:xs = "http://www.w3.org/2001/XMLSchema"> <xs:element name = "student"> <xs:complexType> <xs:sequence> <xs:element name = "age" type = "xs:unsignedByte" /> <xs:element name = "name" type = "xs:string" /> </xs:sequence> <xs:attribute name = "id" type = "xs:unsignedByte" use = "required" /> </xs:complexType> </xs:element> </xs:schema>
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 −
Class : com.tutorialspoint.StudentValidationEventHandler Name : Malik Age : 12
To Continue Learning Please Login
Login with Google