Skip to content

Commit 19eb8bd

Browse files
An introduction to JAXB
1 parent 108adf8 commit 19eb8bd

7 files changed

Lines changed: 349 additions & 0 deletions

File tree

‎jaxb/pom.xml‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>jaxb</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<name>jaxb</name>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.glassfish.jaxb</groupId>
12+
<artifactId>jaxb-runtime</artifactId>
13+
<version>${jaxb.version}</version>
14+
</dependency>
15+
16+
<dependency>
17+
<groupId>org.glassfish.jaxb</groupId>
18+
<artifactId>jaxb-core</artifactId>
19+
<version>${jaxb.version}</version>
20+
</dependency>
21+
22+
<!-- utils -->
23+
<dependency>
24+
<groupId>com.sun.istack</groupId>
25+
<artifactId>istack-commons-runtime</artifactId>
26+
<version>3.0.2</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.slf4j</groupId>
31+
<artifactId>slf4j-api</artifactId>
32+
<version>${org.slf4j.version}</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>ch.qos.logback</groupId>
37+
<artifactId>logback-classic</artifactId>
38+
<version>${logback.version}</version>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>ch.qos.logback</groupId>
43+
<artifactId>logback-core</artifactId>
44+
<version>${logback.version}</version>
45+
</dependency>
46+
47+
</dependencies>
48+
49+
<build>
50+
<finalName>jaxb</finalName>
51+
<resources>
52+
<resource>
53+
<directory>src/main/resources</directory>
54+
<filtering>true</filtering>
55+
</resource>
56+
</resources>
57+
58+
<pluginManagement>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.eclipse.m2e</groupId>
62+
<artifactId>lifecycle-mapping</artifactId>
63+
<version>1.0.0</version>
64+
<configuration>
65+
<lifecycleMappingMetadata>
66+
<pluginExecutions>
67+
<pluginExecution>
68+
<pluginExecutionFilter>
69+
<groupId>org.codehaus.mojo</groupId>
70+
<artifactId>jaxb2-maven-plugin</artifactId>
71+
<versionRange>[${jaxb2-maven-plugin.version},)</versionRange>
72+
<goals>
73+
<goal>schemagen</goal>
74+
<goal>xjc</goal>
75+
</goals>
76+
</pluginExecutionFilter>
77+
<action>
78+
<execute />
79+
</action>
80+
</pluginExecution>
81+
</pluginExecutions>
82+
</lifecycleMappingMetadata>
83+
</configuration>
84+
</plugin>
85+
</plugins>
86+
</pluginManagement>
87+
88+
<plugins>
89+
<plugin>
90+
<groupId>org.apache.maven.plugins</groupId>
91+
<artifactId>maven-compiler-plugin</artifactId>
92+
<version>${maven-compiler-plugin.version}</version>
93+
<configuration>
94+
<source>1.8</source>
95+
<target>1.8</target>
96+
</configuration>
97+
</plugin>
98+
99+
<!-- xjc -->
100+
<plugin>
101+
<groupId>org.codehaus.mojo</groupId>
102+
<artifactId>jaxb2-maven-plugin</artifactId>
103+
<version>${jaxb2-maven-plugin.version}</version>
104+
<executions>
105+
<execution>
106+
<id>xjc</id>
107+
<goals>
108+
<goal>xjc</goal>
109+
</goals>
110+
</execution>
111+
</executions>
112+
<configuration>
113+
<xjbSources>
114+
<xjbSource>src/main/resources/global.xjb</xjbSource>
115+
</xjbSources>
116+
<sources>
117+
<source>src/main/resources/user.xsd</source>
118+
</sources>
119+
<outputDirectory>${basedir}/src/main/java</outputDirectory>
120+
<clearOutputDir>false</clearOutputDir>
121+
</configuration>
122+
</plugin>
123+
124+
<!-- schemagen -->
125+
<!--
126+
<plugin>
127+
<groupId>org.codehaus.mojo</groupId>
128+
<artifactId>jaxb2-maven-plugin</artifactId>
129+
<version>${jaxb2-maven-plugin.version}</version>
130+
<executions>
131+
<execution>
132+
<id>schemagen</id>
133+
<goals>
134+
<goal>schemagen</goal>
135+
</goals>
136+
</execution>
137+
</executions>
138+
<configuration>
139+
<sources>
140+
<source>src/main/java/com/baeldung/jaxb/gen</source>
141+
</sources>
142+
<outputDirectory>src/main/resources</outputDirectory>
143+
<clearOutputDir>false</clearOutputDir>
144+
<transformSchemas>
145+
<transformSchema>
146+
<uri>http://www.baeldung.com/jaxb/gen</uri>
147+
<toPrefix>user</toPrefix>
148+
<toFile>gen-schema.xsd</toFile>
149+
</transformSchema>
150+
</transformSchemas>
151+
</configuration>
152+
</plugin>
153+
-->
154+
</plugins>
155+
</build>
156+
157+
<properties>
158+
<!-- jaxb -->
159+
<jaxb.version>2.2.11</jaxb.version>
160+
161+
<!-- logging -->
162+
<org.slf4j.version>1.7.21</org.slf4j.version>
163+
<logback.version>1.1.7</logback.version>
164+
165+
<!-- maven plugins -->
166+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
167+
<jaxb2-maven-plugin.version>2.3</jaxb2-maven-plugin.version>
168+
</properties>
169+
170+
</project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.jaxb;
2+
3+
import java.util.Date;
4+
5+
import javax.xml.bind.annotation.XmlAttribute;
6+
import javax.xml.bind.annotation.XmlElement;
7+
import javax.xml.bind.annotation.XmlRootElement;
8+
import javax.xml.bind.annotation.XmlTransient;
9+
import javax.xml.bind.annotation.XmlType;
10+
11+
@XmlRootElement(name = "book")
12+
@XmlType(propOrder = { "id", "name", "date" })
13+
public class Book {
14+
private Long id;
15+
private String name;
16+
private String author;
17+
private Date date;
18+
19+
@XmlAttribute
20+
public void setId(Long id) {
21+
this.id = id;
22+
}
23+
24+
@XmlElement(name = "title")
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
@XmlTransient
30+
public void setAuthor(String author) {
31+
this.author = author;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public Date getDate() {
39+
return date;
40+
}
41+
42+
public void setDate(Date date) {
43+
this.date = date;
44+
}
45+
46+
public Long getId() {
47+
return id;
48+
}
49+
50+
public String getAuthor() {
51+
return author;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "Book [id=" + id + ", name=" + name + ", author=" + author + ", date=" + date + "]";
57+
}
58+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.jaxb;
2+
3+
import java.text.DateFormat;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
6+
7+
import javax.xml.bind.annotation.adapters.XmlAdapter;
8+
9+
public class DateAdapter extends XmlAdapter<String, Date> {
10+
11+
private static final ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
12+
13+
@Override
14+
protected DateFormat initialValue() {
15+
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
16+
}
17+
};
18+
19+
@Override
20+
public Date unmarshal(String v) throws Exception {
21+
return dateFormat.get().parse(v);
22+
}
23+
24+
@Override
25+
public String marshal(Date v) throws Exception {
26+
return dateFormat.get().format(v);
27+
}
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.jaxb;
2+
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
import java.util.Date;
7+
8+
import javax.xml.bind.JAXBContext;
9+
import javax.xml.bind.JAXBException;
10+
import javax.xml.bind.Marshaller;
11+
import javax.xml.bind.Unmarshaller;
12+
13+
public class Main {
14+
public static void marshal() throws JAXBException, IOException {
15+
Book book = new Book();
16+
book.setId(1L);
17+
book.setName("Book1");
18+
book.setAuthor("Author1");
19+
book.setDate(new Date());
20+
21+
JAXBContext context = JAXBContext.newInstance(Book.class);
22+
Marshaller marshaller = context.createMarshaller();
23+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
24+
marshaller.marshal(book, new File("./book.xml"));
25+
}
26+
27+
public static Book unMashal() throws JAXBException, IOException {
28+
JAXBContext context = JAXBContext.newInstance(Book.class);
29+
Unmarshaller unmarshaller = context.createUnmarshaller();
30+
Book book = (Book) unmarshaller.unmarshal(new FileReader("./book.xml"));
31+
return book;
32+
}
33+
34+
public static void main(String[] args) throws JAXBException, IOException {
35+
marshal();
36+
Book book = unMashal();
37+
System.out.println(book.toString());
38+
}
39+
}

‎jaxb/src/main/resources/global.xjb‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
3+
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema"
4+
jaxb:extensionBindingPrefixes="xjc">
5+
6+
<jaxb:globalBindings>
7+
<xjc:simple />
8+
<xjc:serializable uid="-1" />
9+
<jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
10+
parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
11+
printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
12+
</jaxb:globalBindings>
13+
</jaxb:bindings>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
4+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
5+
<layout class="ch.qos.logback.classic.PatternLayout">
6+
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg %n</pattern>
7+
</layout>
8+
</appender>
9+
10+
<logger name="com.baeldung.hazelcast" level="INFO" additivity="false">
11+
<appender-ref ref="STDOUT" />
12+
</logger>
13+
14+
<root level="DEBUG">
15+
<appender-ref ref="STDOUT" />
16+
</root>
17+
18+
</configuration>

‎jaxb/src/main/resources/user.xsd‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.baeldung.com/jaxb/gen"
3+
xmlns:userns="http://www.baeldung.com/jaxb/gen" elementFormDefault="qualified">
4+
5+
<element name="userRequest" type="userns:UserRequest"></element>
6+
<element name="userResponse" type="userns:UserResponse"></element>
7+
8+
<complexType name="UserRequest">
9+
<sequence>
10+
<element name="id" type="int" />
11+
<element name="name" type="string" />
12+
</sequence>
13+
</complexType>
14+
15+
<complexType name="UserResponse">
16+
<sequence>
17+
<element name="id" type="int" />
18+
<element name="name" type="string" />
19+
<element name="gender" type="string" />
20+
<element name="created" type="dateTime" />
21+
</sequence>
22+
</complexType>
23+
</schema>

0 commit comments

Comments
 (0)