I am trying to convert xml to json. First I created java class using the following xml
<CompositeResponse><CompositeIndividualResponse><PersonIdentification>2222</PersonIdentification></CompositeIndividualResponse></CompositeResponse>
The following java classes are following:
public class Main { public CompositeResponse CompositeResponse; public CompositeResponse getCompositeResponse() { return CompositeResponse; } public void setCompositeResponse(CompositeResponse CompositeResponse) { this.CompositeResponse = CompositeResponse; }}public class CompositeResponse { private List<CompositeIndividualResponse> CompositeIndividualResponse;public List<CompositeIndividualResponse> getCompositeIndividualResponse() { return CompositeIndividualResponse;}public void setCompositeIndividualResponse(List<CompositeIndividualResponse> CompositeIndividualResponse) { CompositeIndividualResponse = CompositeIndividualResponse;}}public class CompositeIndividualResponse { private String Persondentification; public String getPersondentification() { return Persondentification; } public void setPersonIdentification (String PersonIdentification) { this.PersonIdentification = PersonIdentification; }}I am using the following code for conversion: import java.io.IOException;import java.nio.file.Files;import java.nio.file.Paths;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.dataformat.xml.XmlMapper;public class XMLToJson { public static void main(String[] args) throws IOException { String content = new String(Files.readAllBytes(Paths.get("test.xml"))); XmlMapper xmlMapper = new XmlMapper(); Main poppy = xmlMapper.readValue(content, Main.class); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(poppy); System.out.println(json); }}
But i am getting the following exception that CompositeIndividualResponse is not recognized.
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "CompositeIndividualResponse" (class com.test.custom.copy.Main), not marked as ignorable (2 known properties: "CompositeResponse", "compositeResponse"]) at [Source: (StringReader); line: 3, column: 32] (through reference chain: com.test.custom.copy.Main["CompositeIndividualResponse"])
I believe my java pojo is not fit with xml data. So how to define collection of pojo to solve this problem so I can get the following json:
{ "CompositeResponse":{ "CompositeIndividualResponse": [ { "PersonSSNIdentification":"221212501" } ] }}