- Javax.xml.bind classes
- Home
- Binder
- DatatypeConverter
- JAXB
- JAXBContext
- JAXBElement
- JAXBElement.GlobalScope
- JAXBIntrospector
- Marshaller.Listener
- SchemaOutputResolver
- Unmarshaller.Listener
- Javax.xml.bind.util classes
- JAXBResult
- JAXBSource
- ValidationEventCollector
- Javax.xml.parsers classes
- DocumentBuilder
- DocumentBuilderFactory
- SAXParser
- SAXParserFactory
- Javax.xml.soap classes
- AttachmentPart
- MessageFactory
- MimeHeader
- MimeHeaders
- SAAJMetaFactory
- SOAPConnection
- SOAPConnectionFactory
- SOAPFactory
- SOAPMessage
- SOAPPart
- Javax.xml.validation classes
- Schema
- SchemaFactory
- TypeInfoProvider
- Validator
- ValidatorHandler
- Javax.xml.xpath classes
- XPathConstants
- XPathFactory
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
Javax.xml.bind.JAXBContext.createUnmarshaller() Method
Description
The Javax.xml.bind.JAXBContext.createUnmarshaller() create an Unmarshaller object that can be used to convert XML data into a java content tree.
Declaration
Following is the declaration for javax.xml.bind.JAXBContext.createUnmarshaller() method −
public abstract Unmarshaller createUnmarshaller()
Parameters
NA
Return Value
This method returns an Unmarshaller object
Exception
JAXBException − if an error was encountered while creating the Unmarshaller object.
Example
The following example shows the usage of javax.xml.bind.JAXBContext.createUnmarshaller() 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 create Unmarshaller object for unmarshalling ie. convert Student.xml to JAXB object. This example unmarshals the Student.xml file to JAXB object and prints the XML node values.
package com.tutorialspoint; import java.io.FileInputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; public class JAXBContextDemo { public static void main(String[] args) { try { // create JAXBContext which will be used to create a Binder JAXBContext jc = JAXBContext.newInstance(Student.class); // create new file input stream FileInputStream fis = new FileInputStream("Student.xml"); // unmarshaller obj to convert xml data to java content tree Unmarshaller u = jc.createUnmarshaller(); // unmarshaller xml data to java content tree Student s = (Student)u.unmarshal(fis); // prints System.out.println("Age: "+s.getAge()); System.out.println("Name: "+s.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>14</age> <name>Soniya</name> </student>
Let us compile and run the above program, this will produce the following result −
Age: 14 Name: Soniya