Javax.xml.bind.util.JAXBResult.getResult() Method



Description

The Javax.xml.bind.util.JAXBResult.getResult() method gets the unmarshalled object created by the transformation.

Declaration

Following is the declaration for javax.xml.bind.util.JAXBResult.getResult() method

public Object getResult()

Parameters

NA

Return Value

This method always return a non-null object.

Exception

  • IllegalStateException − if this method is called before an object is unmarshalled.

  • JAXBException − if there is any unmarshalling error. Note that the implementation is allowed to throw SAXException during the parsing when it finds an error.

Example

The following example shows the usage of javax.xml.bind.util.JAXBResult.getResult() 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 unmarshal ie. convert Student XML into an object.

package com.tutorialspoint;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.util.JAXBResult;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

public class JAXBResultDemo {
   public static void main(String[] args) {
      
      try {
         JAXBContext context = JAXBContext.newInstance(Student.class);
         JAXBResult result = new JAXBResult(context);
               
         // set up XSLT transformation
         TransformerFactory tf = TransformerFactory.newInstance();
         File xslFile = new File("transform.xsl");
         Transformer t = tf.newTransformer(new StreamSource(xslFile));

         // run transformation
         File f = new File("Student.xml");
         StreamSource s = new StreamSource(f);
         t.transform(s,result);

         // obtain the unmarshalled content tree
         Object o = result.getResult();

         // cast object to student
         Student st = (Student)o;
         
         // print
         System.out.println("Age: "+st.getAge());
         System.out.println("Name: "+st.getName());
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

The Student.xml is used for unmarshalling. Below are the content details of the file −

<?xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<student id = "10">
   <age>12</age>
   <name>Malik</name>
</student>

The following style sheet transform.xsl is implemented in the program to set schema in the binder object.

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
   <xsl:template match = "node() | @*">
      <xsl:copy>
         <xsl:apply-templates select = "node() | @*" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match = "name"> <xsl:value-of select = "descendents"/>
      <xsl:element name = "name">
         <xsl:value-of select = "node()"/>
      </xsl:element>
   </xsl:template>
   <xsl:template match = "age"> <xsl:value-of select = "descendents"/>
      <xsl:element name = "age">
         <xsl:value-of select = "node()"/>
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>

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: 12
Name: Malik
javax_xml_bind_util_jaxbresult.htm
Advertisements