JAXB, stands for Java Architecture for XML Binding, using JAXB annotation to convert Java object to / from XML file. In this tutorial, we show you how to use JAXB to do following stuffs :
- Marshalling – Convert a Java object into a XML file.
- Unmarshalling – Convert XML content into a Java Object.
Technologies used in this article
- JDK 1.6
- JAXB 2.0
Working with JAXB is easy, just annotate object with JAXB annotation, later use
jaxbMarshaller.marshal() orjaxbMarshaller.unmarshal() to do the object / XML conversion.1. JAXB Dependency
No extra jaxb libraries are required if you are using JDK1.6 or above, because JAXB is bundled in JDK 1.6.
Note
For JDK < 1.6, download JAXB from here, and puts “jaxb-api.jar” and “jaxb-impl.jar” on your project classpath.
For JDK < 1.6, download JAXB from here, and puts “jaxb-api.jar” and “jaxb-impl.jar” on your project classpath.
2. JAXB Annotation
For object that need to convert to / from XML file, it have to annotate with JAXB annotation. The annotation are pretty self-explanatory, you can refer to this JAXB guide for detail explanation. import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
/**
* A message.
*/
@XmlRootElement
public class Message {
/**
* The sender.
*/
private String from;
/**
* The receiver.
*/
private String to;
/**
* A unique identification.
*/
private int id;
/**
* The actual message.
*/
private String text;
/**
* Default contstructor.
*/
public Message() {
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getId() {
return id;
}
@XmlAttribute
public void setId(int id) {
this.id = id;
}
public String toString() {
return String.format("Message[id=%s, from=%s, to=%s, text=%s]", id, from, to, text);
}
}
3. Convert Object to XML
JAXB marshalling example, convert customer object into a XML file. The
jaxbMarshaller.marshal() contains a lot of overloaded methods, find one that suit your output. import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class MessageImpl {
public static void main(String[] args) throws JAXBException{
Message message = new Message();
message.setFrom("Konda Reddy");
message.setTo("Prasad Reddy");
message.setText("Hello from JAXB!");
message.setId(123);
// Create a JAXB context and a marshaller
JAXBContext context = JAXBContext.newInstance(Message.class);
Marshaller handler = context.createMarshaller();
// Set the output in pretty format
handler.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Make the actual serialization
handler.marshal(message, new File("src/output.xml"));
// Here I am using eclipse project for that src/output.xml
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<message id="123">
<from>Konda Reddy</from>
<text>Hello from JAXB!</text>
<to>Prasad Reddy</to>
</message>
4. Convert XML to Object
JAXB unmarshalling example, convert a XML file content into a customer object. The
jaxbMarshaller.unmarshal() contains a lot of overloaded methods, find one that suit yours. import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
public class XmlToObject {
public static void main(String [] args){
try{// Create a JAXB context and an unmarshaller
JAXBContext context = JAXBContext.newInstance(Message.class);
Unmarshaller handler = context.createUnmarshaller();
// Get the object
Message msg = (Message)handler.unmarshal(new File("src/output.xml"));
System.out.println(msg);
System.out.println(msg.getId());
System.out.println(msg.getText());
System.out.println(msg.getFrom());
System.out.println(msg.getTo());
System.out.println(msg.getClass().getName());
}catch(Exception e){
e.printStackTrace();
}
}
}
Output
Message[id=123, from=Konda Reddy, to=Prasad Reddy, text=Hello from JAXB!]
123
Hello from JAXB!
Konda Reddy
Prasad Reddy
Message
Enjoy with this is example
please post all source files and related jar for download
ReplyDelete