- 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.JAXB.marshal() Method
Description
The Javax.xml.bind.JAXB.marshal(Object jaxbObject, URL xml) method writes a Java object tree to XML and store it to the specified location.
Declaration
Following is the declaration for javax.xml.bind.JAXB.marshal(Object jaxbObject, URL xml) method
public static void marshal(Object jaxbObject, URL xml)
Parameters
jaxbObject − The string is first interpreted as an absolute URI. If it's not a valid absolute URI, then it's interpreted as a File.
xml − The XML will be sent to the resource pointed by this URL. Note that not all URLs support such operation, and exact semantics depends on the URL implementations. In case of HTTP URLs, this will perform HTTP POST.
Return Value
This method does not return any value.
Exception
DataBindingException − If the operation fails, such as due to I/O error, unbindable classes.
Example
The following example shows the usage of javax.xml.bind.JAXB.marshal(Object jaxbObject, URL xml) 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 marshal ie. 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; import java.io.IOException; import java.io.InputStream; import java.net.URL; import javax.xml.bind.JAXB; public class JAXBDemo { public static void main(String[] args) throws IOException { // create student object Student st = new Student(); st.setAge(12); st.setName("Sania"); InputStream in = null; try { // create new URL String s = "http://www.tutorialspoint.com/result.xml"; URL url = new URL(s); // saves student object to the file JAXB.marshal(st, url); // create new input stram in = (InputStream) url.openStream(); int i=0; // read till the end of the xml file while((i=in.read())!=-1) { // convert integer to character char c = (char)i; // print System.out.print(c); } }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 = "yes"?> <student id = "10"> <age>12</age> <name>Sania</name> </student>