ValidationEventCollector.handleEvent() Method



Description

The Javax.xml.bind.util.ValidationEventCollector.getEvents() method receives notification of a validation warning or error. The ValidationEvent will have a ValidationEventLocator embedded in it that indicates where the error or warning occurred.

Declaration

Following is the declaration for javax.xml.bind.util.ValidationEventCollector.handleEvent(ValidationEvent event) method

public boolean handleEvent(ValidationEvent event)

Parameters

event − the encapsulated validation event information. It is a provider error if this parameter is null.

Return Value

This method returns true if the JAXB Provider should attempt to continue the current unmarshal, validate, or marshal operation after handling this warning/error, false if the provider should terminate the current operation with the appropriate UnmarshalException, ValidationException, or MarshalException.

Exception

NA

Example

The following example shows the usage of javax.xml.bind.util.ValidationEventCollector.handleEvent(ValidationEvent event) 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 file into an Student object. This example unmarshals the Student.xml file and prints exception details if any error occurs inside the XML

package com.tutorialspoint;

import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventLocator;
import javax.xml.bind.util.ValidationEventCollector;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

public class ValidationEventCollectorDemo {
   public static void main(String[] args) {
      Schema mySchema;
      String SCHEMA_NS_URI = XMLConstants.W3C_XML_SCHEMA_NS_URI;
      
      // create SchemaFactory
      SchemaFactory sf = SchemaFactory.newInstance(SCHEMA_NS_URI);
      
      // validation Event collector
      ValidationEventCollector vec = new ValidationEventCollector();
      try{
         
         // parse schema file and return as schema object
         mySchema=sf.newSchema(new File("Student.xsd"));
         
         // create JAXBContext object
         JAXBContext jc = JAXBContext.newInstance(Student.class);
         
         // create umarshaller to unmarshal xml to object
         Unmarshaller u = jc.createUnmarshaller();
         
         // set schema object
         u.setSchema(mySchema);
         
         // set event handler
         u.setEventHandler( vec );
         
         // create student object
         Student s = (Student)u.unmarshal(new File("Student.xml"));
         
      }catch(Exception e){
         
      }finally{
         if( vec != null && vec.hasEvents() ){
            
            // get validation events generated if any error occurs.
            for( ValidationEvent ve: vec.getEvents() ){
               
               // returns false if provider terminates the current operation
               System.out.println("Should continue: "+vec.handleEvent(ve));
               String msg = ve.getMessage();
               ValidationEventLocator vel = ve.getLocator();
               int line = vel.getLineNumber();
               int column = vel.getColumnNumber();
               System.out.println(line + "." +column + ": " + msg ); 
            }
         }
      }
   }
}

For unmarshalling we must provide XML as input to the main program −

<?xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<student id = "10">
   <age1>12</age1>
   <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 −

Should continue: false
3.10: cvc-complex-type.2.4.a: Invalid content was found starting with element 'age1'. 
One of '{age}' is expected.
javax_xml_bind_util_validationeventcollector.htm
Advertisements