" + xmlText + """ /> " + xmlText + """ /> " + xmlText + """/>

How to configure Jackson2 XmlMapper to automatically detect repeated XML children nodes and replace them with a named array

34 views Asked by At

I am using the following code:

            ObjectMapper xmlMapper = new XmlMapper();
            JsonNode jsonNode = xmlMapper.readTree("<fakeRoot>" + xmlText + "</fakeRoot>");
            String jsonString = jsonNode.toString();
            System.out.println("jsonString = " + jsonString);

I have the following value in the xmlText:

  <a>
      <b>
          <c>xxxx</c>
      </b>
      <b>
          <c>yyyy</c>
      </b>
      <b>
          <c>zzzz</c>
      </b>
  </a>

I would expect this JSON to be produced:

  {
    "a": {
      "b": [
        { "c": "xxxx" },
        { "c": "yyyy" },
        { "c": "zzzz" }
      ]
    }
  }

Why is the XmlMapper returning the following incorrect JSON?

{"a":{"b":{"c":"zzzz"}}}

How can I configure it to produce the desired JSON (without declaring any Java classes for each of the "a", "b", "c") ?

How can I configure XmlMapper to automatically detect repeated XML children nodes and replace them with a named array ?

0

There are 0 answers