Jackson XML to JSON converter removes multiple child records

别说谁变了你拦得住时间么 提交于 2021-01-27 05:21:45

问题


I am using following code to convert my source XML to JSON. However, this code removes any multiple occurrence of child records in source XML and output JSON only contains last child record.

How do I get Jackson XML to JSON converter to output all child records in JSON?

Code

XmlMapper xmlMapper = new XmlMapper();
Map entries = xmlMapper.readValue(new File("source.xml"), LinkedHashMap.class);
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writer().writeValueAsString(entries);
System.out.println(json);

Source XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<File>
  <NumLeases>1</NumLeases>
  <FLAG>SUCCESS</FLAG>
  <MESSAGE>Test Upload</MESSAGE>
  <Lease>
     <LeaseVersion>1</LeaseVersion>
     <F1501B>
        <NEDOCO>18738</NEDOCO>
        <NWUNIT>0004</NWUNIT>
        <NTRUSTRECORDKEY>12</NTRUSTRECORDKEY>
     </F1501B>
     <F1501B>
        <NEDOCO>18739</NEDOCO>
        <NWUNIT>0005</NWUNIT>
        <NTRUSTRECORDKEY>8</NTRUSTRECORDKEY>
     </F1501B>
  </Lease>
</File>

Actual Output

{
  "NumLeases": "1",
  "FLAG": "SUCCESS",
  "MESSAGE": "Test Upload",
  "Lease": {
    "LeaseVersion": "1",
    "F1501B": {
      "NEDOCO": "18739",
      "NWUNIT": "0005",
      "NTRUSTRECORDKEY": "8"
    }
  }
}

Expected Output

{
  "NumLeases": "1",
  "FLAG": "SUCCESS",
  "MESSAGE": "Test Upload",
  "Lease": {
    "LeaseVersion": "1",
    "F1501B": [
      {
        "NEDOCO": "18738",
        "NWUNIT": "0004",
        "NTRUSTRECORDKEY": "12"
      },
      {
        "NEDOCO": "18739",
        "NWUNIT": "0005",
        "NTRUSTRECORDKEY": "8"
      }
    ]
  }
}

Any help shall be appreciated. Thanks!


回答1:


I was able to get the solution to this problem by using org.json API to convert source XML to JSONObject and then to JSON by Jackson API.

Code

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.json.XML;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

...
...

try (InputStream inputStream = new FileInputStream(new File(
                "source.xml"))) {
    String xml = IOUtils.toString(inputStream);
    JSONObject jObject = XML.toJSONObject(xml);
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    Object json = mapper.readValue(jObject.toString(), Object.class);
    String output = mapper.writeValueAsString(json);
    System.out.println(output);
}

...
...

Source XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<File>
  <NumLeases>1</NumLeases>
  <FLAG>SUCCESS</FLAG>
  <MESSAGE>Test Upload</MESSAGE>
  <Lease>
     <LeaseVersion>1</LeaseVersion>
     <F1501B>
        <NEDOCO>18738</NEDOCO>
        <NWUNIT>0004</NWUNIT>
        <NTRUSTRECORDKEY>12</NTRUSTRECORDKEY>
     </F1501B>
     <F1501B>
        <NEDOCO>18739</NEDOCO>
        <NWUNIT>0005</NWUNIT>
        <NTRUSTRECORDKEY>8</NTRUSTRECORDKEY>
     </F1501B>
  </Lease>
</File>

Output

{
  "File" : {
    "NumLeases" : "1",
    "FLAG" : "SUCCESS",
    "MESSAGE" : "Test Upload",
    "Lease" : {
      "LeaseVersion" : "1",
      "F1501B" : [ {
        "NEDOCO" : "18738",
        "NWUNIT" : "0004",
        "NTRUSTRECORDKEY" : "12"
      }, {
        "NEDOCO" : "18739",
        "NWUNIT" : "0005",
        "NTRUSTRECORDKEY" : "8"
      } ]
    }
  }
}



回答2:


The problem is that XML does not have distinction between concepts of "Objects" (in JSON term, that is, collection of key/value pairs) and "Arrays" (ordered sequence of values without name). When using data-binding (POJOs), it is possible to determine from Java type which one a particular XML element means -- if Java property is a List or array, it has to be "Array"; otherwise "Object" -- but when only telling mapper to map XML to java.util.Map there is no such distinction, and default handling will essentially consider all XML Elements to indicate "Objects". And since "Objects" can not have properties with duplicate names, only one of the values (last one) is retained.

There is no simple way to avoid that using XmlMapper: you will need custom handling of some sort.



来源:https://stackoverflow.com/questions/36410922/jackson-xml-to-json-converter-removes-multiple-child-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!