- 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.Binder.updateJAXB() Method
Description
The Javax.xml.bind.Binder.updateJAXB(XmlNode xmlNode) method takes an XML node and updates its associated JAXB object and its descendants.
This operation can be thought of as an "in-place" unmarshalling. The difference is that instead of creating a whole new JAXB tree, this operation updates an existing tree, reusing as much JAXB objects as possible.
Declaration
Following is the declaration for javax.xml.bind.Binder.updateJAXB(XmlNode xmlNode) method
public abstract Object updateJAXB(XmlNode xmlNode)
Parameters
xmlNode − the document/element to unmarshal XML data from.
Return Value
Returns the updated JAXB object.
Exception
JAXBException − if any unexpected error occur while unmarshalling.
IllegalArgumentException − If the node parameter is null.
Example
The following example shows the usage of javax.xml.bind.Binder.updateJAXB(XmlNode xmlNode) method. To proceed, consider the following Student class which will be used to have objects for "in-place" 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 unmarshal ie. convert Student XML file to JAXB 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 to JAXB object.
package com.tutorialspoint; import javax.xml.bind.JAXBContext; import javax.xml.bind.Binder; import javax.xml.bind.Marshaller; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; public class BinderDemoUnmarshall { public static void main(String[] args) { try { // we need a document to store Student.xml as input 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); // create binder Binder<Node> binder = jc.createBinder(); // get xml node from the document Node xmlNode = document.getDocumentElement(); // Returns the updated JAXB object Student st = (Student)binder.updateJAXB(xmlNode); // print 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
To Continue Learning Please Login
Login with Google