Javax.xml.bind.JAXBContext.newInstance() Method



Description

The Javax.xml.bind.JAXBContext.newInstance(Class... classesToBeBound) obtains a new instance of JAXBContext class.

The client application must supply a list of classes that the new context object needs to recognize. Not only the new context will recognize all the classes specified, but it will also recognize any classes that are directly/indirectly referenced statically from the specified classes. Subclasses of referenced classes nor @XmlTransient referenced classes are not registered with JAXBContext.

Declaration

Following is the declaration for javax.xml.bind.JAXBContext.newInstance(Class... classesToBeBound) method

public static JAXBContext newInstance(Class... classesToBeBound)

Parameters

classesToBeBound − list of java classes to be recognized by the new JAXBContext. Can be empty, in which case a JAXBContext that only knows about spec-defined classes will be returned.

Return Value

The method returns a new instance of a JAXBContext. Always non-null valid object.

Exception

  • JAXBException − if an error was encountered while creating the JAXBContext

  • IllegalArgumentException − If the name parameter is null

Example

The following example shows the usage of javax.xml.bind.JAXBContext.newInstance(Class... classesToBeBound) method. To proceed, consider the following Student class which will be used to have objects for marshalling or unmarshalling 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 create new instance of JAXBContext object for unmarshalling or marshalling ie. convert Student.xml file to an object or a JAXB object into an XML file. Here we will creat Binder object using JAXBContext because we can not create JAXB object directly because it is an abstract class.

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 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);

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

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

         // let's marshal the object and store in document
         binder.marshal(student, document);
         
         // finally print the marshalled object on stdout
         TransformerFactory tf = TransformerFactory.newInstance();
         Transformer t = tf.newTransformer();
         t.transform(new DOMSource(document), new StreamResult(System.out));
  
      }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 −

<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
<student id = "10">
   <age>11</age>
   <name>Malik</name>
</student>
javax_xml_bind_jaxbcontext.htm
Advertisements