Sunday, 1 March 2015

JAXB : XML Read & Write in Java

JAXB is "Java Architecture for xml binding" and consists in:
-Schema compiler
-Schema generator
-Binding runtime framework

The classes to read, write and binding XML in Java are included in JavaSE and JavaEE. These classes are contained in package: javax.xml.bind.*

We will analyze two examples of read and write XML using marshalling and unmarshalling as in the following diagram:


Read/Write operations:

XML --> Unmarshalling --> Object
XML <-- Marshalling    <--  Object

By default the encoding in the marshalling process is UTF-8

Example:
In the following example the readXML method will read an XML file and it will be converted into an Object. Then the writeXML method transform the object into XML again.

XML menu
<?xml version="1.0" encoding="UTF-8"?>
<menu>
<type>cheeseburguer</type>
<drink>coke</drink>
<side>chips</side>
<side>salad</side>
</menu>
view raw menu.xml hosted with ❤ by GitHub

Object menu
Not to forget to use annotations to indicate the root and elements: @XmlRootElement and @XmlElement :


import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Menu{
private String type;
private String drink;
private String[] side;
public String getType() {
return type;
}
@XmlElement
public void setType(String type) {
this.type = type;
}
public String getDrink() {
return drink;
}
@XmlElement
public void setDrink(String drink) {
this.drink = drink;
}
@XmlElement
public String[] getSide() {
return side;
}
public void setSide(String[] side) {
this.side = side;
}
}
view raw menu.java hosted with ❤ by GitHub

  • Read XML with JAXB
The steps are:
-Create a JAXBContext
-Create object Unmarshaller
-Define inputStream object
-Read XML using the unmarshalling object

/*
* Read XML file obtaining an object
*/
public Menu readXML() throws JAXBException, FileNotFoundException
{
JAXBContext jaxbContext = JAXBContext.newInstance(Menu.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
InputStream input = new FileInputStream("menu.xml");
Menu menu = (Menu)unmarshaller.unmarshal(input);
return menu;
}
view raw readXML.java hosted with ❤ by GitHub


  • Write XML with JAXB 
The steps are:
-Create a JAXBContext
-Create object Marshaller
-Define the output format
-Define outputStream object
-Write XML using the marshalling object

/*
* Write object into XML file
*/
public void writeXML(Menu menu) throws JAXBException, IOException
{
JAXBContext jaxbContext = JAXBContext.newInstance(Menu.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
OutputStream output = new FileOutputStream("output.xml");
marshaller.marshal(menu, output);
}
view raw writeXML.java hosted with ❤ by GitHub


  • Executing the main method - Results

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class ProcessXML {
public static void main(String args[]) throws JAXBException, IOException
{
ProcessXML processor = new ProcessXML();
//read
Menu menu = processor.readXML();
System.out.println(menu.getType());
System.out.println(menu.getDrink());
for(String sideString : menu.getSide()){
System.out.println(sideString);
}
//write
processor.writeXML(menu);
}
view raw mainJaxb.java hosted with ❤ by GitHub

The output in console will be:
cheeseburguer
coke
chips
salad

Also as a result of writing method a new output.xml file will be created:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<menu>
    <drink>coke</drink>
    <side>chips</side>
    <side>salad</side>
    <type>cheeseburguer</type>
</menu>


No comments:

Post a Comment