Java.xml.bind.Binder.unmarshal() Method



Description

The Javax.xml.bind.Binder.unmarshal(XmlNode xmlNode, Class<T> declaredType) method unmarshals i.e. converts an XML document by provided declaredType to a JAXB object tree.

This method is similar to Unmarshaller.unmarshal(Node, Class) with the addition of maintaining the association between XML nodes and the produced JAXB objects, enabling future update operations, updateXML(Object, Object) or updateJAXB(Object).

Declaration

Following is the declaration for java.xml.bind.Binder.unmarshal(XmlNode xmlNode, Class<T> declaredType) method −

public abstract <T> JAXBElement<T> unmarshal(XmlNode xmlNode, Class<T> declaredType)

Parameters

  • xmlNode − the document/element to unmarshal XML data from.

  • declaredType − appropriate JAXB mapped class to hold node's XML data.

Return Value

The method returns JAXB Element representation of node

Exception

  • JAXBException − if any unexpected error occur while unmarshalling.

  • UnmarshalException − If the ValidationEventHandler returns false from its handleEvent method or the Binder is unable to perform the XML to Java binding.

  • IllegalArgumentException − If the node parameter is null.

Example

The following example shows the usage of java.xml.bind.Binder.unmarshal(XmlNode xmlNode, Class<T> declaredType) method.

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 to JAXB object. Here we will creat Binder object using JAXBContext because we can not create Binder object directly because it is an abstract class. This example unmarshals the Student object and prints.

package com.tutorialspoint;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Binder;
import javax.xml.bind.JAXBElement;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class BinderDemoUnmarshall {
   public static void main(String[] args) {
  
      // create a Student object and set its properties
      Student student = new Student();
      student.setId(10);
      student.setName("Zara Ali");
      student.setAge(10);

      try {
         // we need a document to store Student.xml as input
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
         Document document = docBuilder.parse("Student.xml");
          
         // create JAXBContext which will be used to create a Binder
         JAXBContext jc = JAXBContext.newInstance(Student.class);

         // create binder
         Binder<Node> binder = jc.createBinder();
         
         // get xml node from the document
         Node xmlNode = document.getDocumentElement();
         
         // unmarshal the xml
         JAXBElement<Student> jst = binder.unmarshal(xmlNode, Student.class);
         
         // get student object
         Student st = jst.getValue();
         
         // print
         System.out.println("Age: "+st.getAge());
         System.out.println("Name: "+st.getName());
    
      }catch(Exception ex) {
         ex.printStackTrace();
      }
   }
}

To create document, an XML file is needed as input. The XML file is named as Student.xml

<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
<student id = "10">
   <age>10</age>
   <name>Zara Ali</name>
</student>

Let us compile and run the above program, this will produce the following result −

Age: 10
Name: Zara Ali
javax_xml_bind_binder.htm
Advertisements