- 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
Java.io.BufferedInputStream.createBinder() Method
Description
The Javax.xml.bind.JAXBContext.createBinder(Class<T> domType) Creates a Binder object that can be used for associative/in-place unmarshalling/marshalling.
Declaration
Following is the declaration for java.io.BufferedInputStream.createBinder(Class<T> domType) method −
public <T> Binder<T> createBinder(Class<T> domType)
Parameters
domType − select the DOM API to use by passing in its DOM Node class.
Return Value
This method returns a new valid Binder object
Exception
domType − if an I/O error occurs.
Example
The following example shows the usage of Javax.xml.bind.JAXBContext.createBinder(Class<T> domType) method. To proceed, consider the following Student class which will be used to have objects for marshalling purpose −
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 binder object for marshalling ie. to 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;
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 JAXBContextDemo { public static void main(String[] args) { // create a Student object and set its properties Student student = new Student(); student.setId(10); student.setName("Zara Ali"); student.setAge(10); 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); // creates binder object for associative marshaling/unmarshaling Binder<Node> binder = jc.createBinder(Node.class); // set output as formatted one 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(); } } }
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>10</age> <name>Zara Ali</name> </student>
To Continue Learning Please Login
Login with Google