The usage of Jaxb

1. add The dependencies for JAXB

1
2
3
4
5
6
7
8
9
10
11
12
13
14

dependencies {

...

compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'
compile group: 'com.sun.xml.bind', name: 'jaxb-core', version: '2.3.0.1'
compile group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '2.2.11'
compile group: 'javax.activation', name: 'activation', version: '1.1.1'

...

}

2. type the code for marshalling and unmarshalling using the JAXB.

Marshalling (POJO -> XML)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

public String jaxbMarshalling(Object target) {
String result = "";

try {
JAXBContext jaxbContext = JAXBContext.newInstance(target.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
StringWriter stringWriter = new StringWriter();
jaxbMarshaller.marshal(target, stringWriter);
result = stringWriter.toString();
} catch (JAXBException e) {
e.printStackTrace();
}

return result;
}

UnMarshalling (XML -> POJO)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

public Object jaxbUnmarshalling(Class<?> type, String target) {
Object result = null;

try {
JAXBContext jaxbContext = JAXBContext.newInstance(type);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
result = jaxbUnmarshaller.unmarshal(new StringReader(target));

} catch (JAXBException e) {
e.printStackTrace();
}

return result;
}

Actually, Doing to Make the Jaxb Object like JAXBContext is too heavy task to computer. so I recommend to use the code related to ‘JAXBContext’ separately when you make it.

3. Sample The XML file

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
53
54
55
56
57
58
59
60
61
62
63
64
65

String xmlSample = "<?xml version=\"1.0\"?>" +
"<Tests>" +
"<Test TestId=\"0001\" TestType=\"CMD\">" +
"<Name>Convert number to string</Name>" +
"<CommandLine>Examp1.EXE</CommandLine>" +
"<Input>1</Input>" +
"<Output>One</Output>" +
"<Messages>" +
"<Content>Message Content Sample1</Content>" +
"<Date>Message Date Sample2</Date>" +
"</Messages>" +
"</Test>" +
"<Test TestId=\"0002\" TestType=\"CMD\">" +
"<Name>Find succeeding characters</Name>" +
"<CommandLine>Examp2.EXE</CommandLine>" +
"<Input>abc</Input>" +
"<Output>def</Output>" +
"<Messages>" +
"<Content>Message Content Sample1</Content>" +
"<Date>Message Date Sample2</Date>" +
"</Messages>" +
"</Test>" +
"<Test TestId=\"0003\" TestType=\"GUI\">" +
"<Name>Convert multiple numbers to strings</Name>" +
"<CommandLine>Examp2.EXE /Verbose</CommandLine>" +
"<Input>123</Input>" +
"<Output>One Two Three</Output>" +
"<Messages>" +
"<Content>Message Content Sample1</Content>" +
"<Date>Message Date Sample2</Date>" +
"</Messages>" +
"</Test>" +
"<Test TestId=\"0004\" TestType=\"GUI\">" +
"<Name>Find correlated key</Name>" +
"<CommandLine>Examp3.EXE</CommandLine>" +
"<Input>a1</Input>" +
"<Output>b1</Output>" +
"<Messages>" +
"<Content>Message Content Sample1</Content>" +
"<Date>Message Date Sample2</Date>" +
"</Messages>" +
"</Test>" +
"<Test TestId=\"0005\" TestType=\"GUI\">" +
"<Name>Count characters</Name>" +
"<CommandLine>FinalExamp.EXE</CommandLine>" +
"<Input>This is a test</Input>" +
"<Output>14</Output>" +
"<Messages>" +
"<Content>Message Content Sample1</Content>" +
"<Date>Message Date Sample2</Date>" +
"</Messages>" +
"</Test>" +
"<Test TestId=\"0006\" TestType=\"GUI\">" +
"<Name>Another Test</Name>" +
"<CommandLine>Examp2.EXE</CommandLine>" +
"<Input>Test Input</Input>" +
"<Output>10</Output>" +
"<Messages>" +
"<Content>Message Content Sample1</Content>" +
"<Date>Message Date Sample2</Date>" +
"</Messages>" +
"</Test>" +
"</Tests> ";

I proposed the above xml file for understanding the process to make the VO file when you use JAXB Marsharlling, UnMarshalling. It has various type of the data like List, the nested VO Object , Default type data.

4. Create the VO files.

Importantly, JAXB can’t parse any data partly. If you want to get some data from the api written by xml, then you should take all of the xml api without any the skip. In the above sample xml data, You should make 3 VO files totally. They are the parent node of the sample xml data.

1
2
3
4
...> touch TestsVO.java
...> touch TestVO.java
...> touch MessagesVO.java

TestsVO.java

1
2
3
4
5
6
7
8
9
10
11

@Getter
@Setter
@ToString
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Tests")
public class TestsVO {
@XmlElement(name="Test")
protected List<TestVO> test;
}

TestVO.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

@Getter
@Setter
@ToString
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Test")
public class TestVO {
@XmlElement(name="Name")
protected String name;
@XmlElement(name="CommandLine")
protected String commandLine;
@XmlElement(name="Input")
protected String input;
@XmlElement(name="Output")
protected String output;
@XmlElement(name="Messages")
protected MessagesVO messages;
}

MessagesVO

1
2
3
4
5
6
7
8
9
10
11
12
13

@Getter
@Setter
@ToString
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Messages")
public class MessagesVO {
@XmlElement(name="Content")
protected String content;
@XmlElement(name="Date")
protected String date;
}

These 3 VO files are exactly expressed to XML data.

@XmlRootElement
this annotation is for expressing the each root node of the xml. and the annotation can has the ‘name’ attribute. It’s for occuring the exactly xml tag name. If the names are different between POJO and XML then JAXB can’t parse. then This annotation throw the exception of JAXB.

@XmlElement
this annotation is for the each filed of the JAXB VO classes. It also can has the ‘name’ attribute, This attribute is the role for connection between POJO variable name and XML tag name like the @XmlRootElement annotation. If those names are not same then the field will have the null value.

@XmlAccessorType(XmlAccessType.FIELD)
It’s for definition of the namespace when you parse the data using JAXB. If you don’t use this annotation then maybe your jaxb vo fields are overlapped.

5. Example of the usage of Jaxb.

1
2
3
4
5
6
7

TestsVO tests = (TestsVO)jaxbUnmarshalling(TestsVO.class, xmlSample);
System.out.println(tests);

String xml = jaxbMarshalling(tests);
System.out.println(xml);

Share