Skip to content

Commit 9c1d1b4

Browse files
Merge branch 'pr/423-bengi'
2 parents b572a90 + 263d171 commit 9c1d1b4

3 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎jackson/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
<artifactId>commons-io</artifactId>
2323
<version>2.4</version>
2424
</dependency>
25+
<!--jackson for xml-->
26+
<dependency>
27+
<groupId>com.fasterxml.jackson.dataformat</groupId>
28+
<artifactId>jackson-dataformat-xml</artifactId>
29+
<version>2.7.4</version>
30+
</dependency>
31+
2532

2633
<dependency>
2734
<groupId>org.apache.commons</groupId>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.baeldung.jackson.xml;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.io.BufferedReader;
7+
import java.io.File;
8+
import java.io.FileInputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.InputStreamReader;
12+
13+
import org.junit.Test;
14+
15+
import com.fasterxml.jackson.core.JsonProcessingException;
16+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
17+
18+
public class TestXMLSerializeDeserialize {
19+
20+
@Test
21+
public void whenJavaSerializedToXmlStr_thenCorrect() throws JsonProcessingException {
22+
XmlMapper xmlMapper = new XmlMapper();
23+
String xml = xmlMapper.writeValueAsString(new SimpleBean());
24+
assertNotNull(xml);
25+
}
26+
27+
@Test
28+
public void whenJavaSerializedToXmlFile_thenCorrect() throws IOException {
29+
XmlMapper xmlMapper = new XmlMapper();
30+
xmlMapper.writeValue(new File("target/simple_bean.xml"), new SimpleBean());
31+
File file = new File("target/simple_bean.xml");
32+
assertNotNull(file);
33+
}
34+
35+
@Test
36+
public void whenJavaGotFromXmlStr_thenCorrect() throws IOException {
37+
XmlMapper xmlMapper = new XmlMapper();
38+
SimpleBean value = xmlMapper.readValue(
39+
"<SimpleBean><x>1</x><y>2</y></SimpleBean>", SimpleBean.class);
40+
assertTrue(value.getX() == 1 && value.getY() == 2);
41+
}
42+
43+
@Test
44+
public void whenJavaGotFromXmlFile_thenCorrect() throws IOException {
45+
File file = new File("src/test/resources/simple_bean.xml");
46+
XmlMapper xmlMapper = new XmlMapper();
47+
String xml = inputStreamToString(new FileInputStream(file));
48+
SimpleBean value = xmlMapper.readValue(xml, SimpleBean.class);
49+
assertTrue(value.getX() == 1 && value.getY() == 2);
50+
}
51+
52+
private static String inputStreamToString(InputStream is) throws IOException {
53+
BufferedReader br;
54+
StringBuilder sb = new StringBuilder();
55+
56+
String line;
57+
br = new BufferedReader(new InputStreamReader(is));
58+
while ((line = br.readLine()) != null) {
59+
sb.append(line);
60+
}
61+
br.close();
62+
return sb.toString();
63+
}
64+
}
65+
66+
class SimpleBean {
67+
private int x = 1;
68+
private int y = 2;
69+
70+
public int getX() {
71+
return x;
72+
}
73+
74+
public void setX(int x) {
75+
this.x = x;
76+
}
77+
78+
public int getY() {
79+
return y;
80+
}
81+
82+
public void setY(int y) {
83+
this.y = y;
84+
}
85+
86+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<SimpleBean>
2+
<x>1</x>
3+
<y>2</y>
4+
</SimpleBean>

0 commit comments

Comments
 (0)