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



Description

The Javax.xml.bind.Binder.getJAXBNode(XmlNode xmlNode) method gets the JAXB object associated with the given XML element.

Once a JAXB object tree is associated with an XML fragment, this method enables navigation between the two trees.

Declaration

Following is the declaration for javax.xml.bind.Binder.getJAXBNode(XmlNode xmlNode) method

public abstract Object getJAXBNode(XmlNode xmlNode)

Parameters

xmlNodet − the node document to be passed as parameter.

Return Value

This method returns null if the specified XML node is not known to this Binder, or if it is not associated with a JAXB object.

Exception

IllegalArgumentException − If the node parameter is null.

Example

The following example shows the usage of javax.xml.bind.Binder.getJAXBNode(XmlNode xmlNode) method. To proceed, consider the following Student class which will be used to have objects for marshalling and unmarshalling purposes −

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. 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.xml file and prints Xml element values.

package com.tutorialspoint;

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 org.w3c.dom.Document;
import org.w3c.dom.Node;

public class BinderDemoUpdate {
   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");
          
         // create JAXBContext which will be used to create a Binder
         JAXBContext jc = JAXBContext.newInstance(Student.class);

         Binder<Node> binder = jc.createBinder();
         
         // get xml node from the document
         Node xmlNode = document.getDocumentElement();
         
         // unmarshal xml document to JAXB object
         Student st = (Student) binder.unmarshal(xmlNode);
         
         // get xml node associated with the binder
         st = (Student) binder.getJAXBNode(xmlNode);
         
         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>

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 −

Age: 10
Name: Zara Ali
javax_xml_bind_binder.htm
Advertisements