Step 1: Create a Java Maven project.
|
1 2 3 |
mvn archetype:generate -B -DgroupId=com.mytutorial -DartifactId=simple-jaxb |
Import the “simple-jaxb” folder that has the pom.xml it into eclipse as an “existing maven project”.
Step 2: Modify the “pom.xml” file to look like as shown below. The “maven-jaxb2-plugin” generates JAXB annotated Java objects from the XML schema “books.xsd”. These objects JAXB annotated objects are used for unmarshalling (i.e. String or stream to Java objects) & marshalling (i.e. Java objects to String or stream).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mytutorial</groupId> <artifactId>simple-jaxb</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>simple-jaxb</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.13.1</version> <executions> <execution> <id>schema1-generate</id> <goals> <goal>generate</goal> </goals> <configuration> <schemaDirectory>src/main/resources/schema</schemaDirectory> <schemaIncludes> <include>books.xsd</include> </schemaIncludes> <generatePackage>com.mytutorial</generatePackage> <generateDirectory>${project.build.directory}/generated-sources/xjc1</generateDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> |
Step 4: Create a new source folder src/main/resources and create a folder named “schema”. Under this folder create a XSD file named “books.xsd”
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="books"> <xsd:complexType> <xsd:sequence> <xsd:element name="header" type="header" minOccurs="1"/> <xsd:element name="book" type="book" minOccurs="0" maxOccurs="unbounded" /> <xsd:element name="footer" type="footer" minOccurs="1" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="header"> <xsd:sequence> <xsd:element name="name" type="xsd:string" minOccurs="1" > </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:complexType name="book"> <xsd:sequence> <xsd:element name="author" type="xsd:string" /> <xsd:element name="title" type="xsd:string" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="footer"> <xsd:sequence> <xsd:element name="records" type="xsd:integer" minOccurs="1"> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:schema> |
The above XSD is for XML file Books.xml under src/main/resources/xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <books> <header> <name>Programming Books</name> </header> <book> <author>John Darcy</author> <title>Learning Java</title> </book> <book> <author>Peter Smith</author> <title>Learning Scala</title> </book> <footer> <records>2</records> </footer> </books> |
Step 5: Generate the JAXB objects by running “mvn generate-sources” on the command line, or within eclipse by right clicking on the project, and then “Run As” -> “Maven generate-sources”
Step 6: Add the “${project.build.directory}/generated-sources/xjc1” to the the eclipse “build path” by right mouse clicking on the folder and then “Build Path” -> “Use as Source Folder“.
Step 7: Create the following “App.java” class (using Java 8) to marshall & unmarshall using the generated JAXB objects and JAXB API.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.mytutorial; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class App { public static void main(String[] args) throws JAXBException, IOException, URISyntaxException { //Java 1.6 onwards JAXBContext jaxbContext = JAXBContext.newInstance(Books.class); //unmarshall: String "source" to Java object Unmarshaller um = jaxbContext.createUnmarshaller(); String fileContents = readFileAsString(); StringReader source = new StringReader(fileContents); Books books = (Books)um.unmarshal(source); //Let's change "Learning Scala" to "Learning JAXB" books.getBook().get(1).setTitle("Learning JAXB"); //marshall: Java object to String "result" Marshaller m = jaxbContext.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter result = new StringWriter(); m.marshal (books, result); System.out.println(result.toString()); } /** * * @return * @throws IOException * @throws URISyntaxException */ private static final String readFileAsString() throws IOException, URISyntaxException { Path path = Paths.get(ClassLoader.getSystemResource("xml/books.xml").toURI()); byte[] readAllBytes = Files.readAllBytes(path); return new String(readAllBytes); } } |
Output
Run App.java to get the following output.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <books> <header> <name>Programming Books</name> </header> <book> <author>John Darcy</author> <title>Learning Java</title> </book> <book> <author>Peter Smith</author> <title>Learning JAXB</title> </book> <footer> <records>2</records> </footer> </books> |
