I want to convert an xml to json.
The format of of xml is as follows -
<default>
<column>
<title>Title 1</title>
<id>id1</id>
<value>val1</value>
</column>
<column>
<title>Title 2</title>
<id>id2</id>
<value>val2</value>
</column>
<column>
<title>Title 3</title>
<id>id3</id>
<value>val3</value>
</column>
</default>
And after the conversion i am expecting following json -
{
"column": [
{
"title": "Title 1",
"id": "id1",
"value": "val1"
},
{
"title": "Title 2",
"id": "id2",
"value": "val2"
},
{
"title": "Title 3",
"id": "id3",
"value": "val3"
}
]
}
But when i use jackson for this purpose it gives me following json -
{
"column": {
"title": "Title 3",
"id": "id3",
"value": "val3"
}
}
I have tried using jackson 1.9 and jackson 2.1, but it didnt gave me the expected output.
Can some one please let me know that whether it is possible or i need to change my xml format ? Following is the code that i have written to acheive the above scenario -
try {
XmlMapper xmlMapper = new XmlMapper();
Map entries = xmlMapper.readValue(new File("xmlPath"), Map.class);
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(entries);
System.out.println(json);
} catch (Exception e) {
e.printStackTrace();
}
Thanks