Javax.xml.bind.Binder.getProperty() Method



Description

The Javax.xml.bind.Binder.getProperty(String name) method gets the particular property in the underlying implementation of Binder. This method can only be used to get one of the standard JAXB defined unmarshal/marshal properties or a provider specific property for binder, unmarshal or marshal. Attempting to get an undefined property will result in a PropertyException being thrown.

Declaration

Following is the declaration for Javax.xml.bind.Binder.getProperty(String name) method

public abstract Object getProperty(String name)

Parameters

name − the name of the property to retrieve.

Return Value

The method returns the value of the requested property.

Exception

  • PropertyException − when there is an error processing the given property or value

  • IllegalArgumentException − If the name parameter is null

Example

The following example shows the usage of javax.xml.bind.Binder.getProperty(String name) 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.util.Properties;

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.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

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


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

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

         Binder<Node> binder = jc.createBinder();

         // set the property
         binder.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

         // get the property for Marshaller.JAXBFORMATTED_OUTPUT
         Object obj = binder.getProperty(Marshaller.JAXB_FORMATTED_OUTPUT);
         System.out.print("Property for Marshaller.JAXB_FORMATTED_OUTPUT: ");
         System.out.print(obj.toString());
  
      }catch(Exception ex) {
         ex.printStackTrace();
      }
   }
}

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 −

Property for Marshaller.JAXB_FORMATTED_OUTPUT: true
javax_xml_bind_binder.htm
Advertisements