-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
Object menu
Not to forget to use annotations to indicate the root and elements: @XmlRootElement and @XmlElement :
- Read XML with JAXB
-Create a JAXBContext
-Create object Unmarshaller
-Define inputStream object
-Read XML using the unmarshalling object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} |
- Write XML with JAXB
-Create a JAXBContext
-Create object Marshaller
-Define the output format
-Define outputStream object
-Write XML using the marshalling object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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); | |
} |
- Executing the main method - Results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
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