javax.xml.bind.Binder.setSchema() Method



Description

The Javax.xml.bind.Binder.setSchema(Schema schema) method specifies whether marshal, unmarshal and update methods performs validation on their XML content.

Declaration

Following is the declaration for javax.xml.bind.Binder.setSchema(Schema schema) method

public abstract void setSchema(Schema schema)

Parameters

schema − set to null to disable validation.

Return Value

This method does not return any value.

Exception

NA

Example

The following example shows the usage of javax.xml.bind.Binder.setSchema(Schema schema) 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 marshal ie. convert Student object into an XML file. 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 and prints it at STDOUT, but in practical scenario you can store the object in any file as an XML node.

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.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");
          
         SchemaFactory sf = null;
         sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
         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());
         
         // 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();
      }
   }
}

The following is the xml input to the program.

<?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 −

Name : Malik
Age : 12
javax_xml_bind_binder.htm
Advertisements