Skip to content

Commit 99d309b

Browse files
fscorromaibin
authored andcommitted
BAEL-2237 Using Jackson to parse XML and return JSON (eugenp#5478)
1 parent 6cabd68 commit 99d309b

1 file changed

Lines changed: 15 additions & 28 deletions

File tree

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.baeldung.jackson.xmlToJson;
22

3-
4-
import com.fasterxml.jackson.core.JsonProcessingException;
53
import com.fasterxml.jackson.databind.JsonNode;
64
import com.fasterxml.jackson.databind.ObjectMapper;
75
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
@@ -14,43 +12,32 @@
1412
public class XmlToJsonUnitTest {
1513

1614
@Test
17-
public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() {
15+
public void givenAnXML_whenUseDataBidingToConvertToJSON_thenReturnDataOK() throws IOException{
1816
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
1917

20-
try {
21-
XmlMapper xmlMapper = new XmlMapper();
22-
Flower poppy = xmlMapper.readValue(flowerXML, Flower.class);
18+
XmlMapper xmlMapper = new XmlMapper();
19+
Flower poppy = xmlMapper.readValue(flowerXML, Flower.class);
2320

24-
assertEquals(poppy.getName(), "Poppy");
25-
assertEquals(poppy.getColor(), Color.RED);
26-
assertEquals(poppy.getPetals(), new Integer(9));
21+
assertEquals(poppy.getName(), "Poppy");
22+
assertEquals(poppy.getColor(), Color.RED);
23+
assertEquals(poppy.getPetals(), new Integer(9));
2724

28-
ObjectMapper mapper = new ObjectMapper();
29-
String json = mapper.writeValueAsString(poppy);
25+
ObjectMapper mapper = new ObjectMapper();
26+
String json = mapper.writeValueAsString(poppy);
3027

31-
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}");
32-
System.out.println(json);
33-
} catch(IOException e) {
34-
e.printStackTrace();
35-
}
28+
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":9}");
3629
}
3730

3831
@Test
39-
public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() {
32+
public void givenAnXML_whenUseATreeConvertToJSON_thenReturnDataOK() throws IOException {
4033
String flowerXML = "<Flower><name>Poppy</name><color>RED</color><petals>9</petals></Flower>";
4134

42-
try {
43-
XmlMapper xmlMapper = new XmlMapper();
44-
JsonNode node = xmlMapper.readTree(flowerXML.getBytes());
45-
46-
ObjectMapper jsonMapper = new ObjectMapper();
47-
String json = jsonMapper.writeValueAsString(node);
35+
XmlMapper xmlMapper = new XmlMapper();
36+
JsonNode node = xmlMapper.readTree(flowerXML.getBytes());
4837

49-
System.out.println(json);
38+
ObjectMapper jsonMapper = new ObjectMapper();
39+
String json = jsonMapper.writeValueAsString(node);
5040

51-
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}");
52-
} catch(IOException e) {
53-
e.printStackTrace();
54-
}
41+
assertEquals(json, "{\"name\":\"Poppy\",\"color\":\"RED\",\"petals\":\"9\"}");
5542
}
5643
}

0 commit comments

Comments
 (0)