Skip to content

XmlMapper cannot correctly map Map #611

Closed
@HenryYihengXu

Description

@HenryYihengXu

I have an object with a map field and I want to map it to a XML file. However, the resulting XML file does not contains pairs like normal XML. Instead, it treat each key as a data type and each value as the value of that data type. For example, I have

class MyObject {
  private final Map<String, Integer> myMap;

  public MyObject() {
    myMap = new HashMap<>();
    myMap.put("a", 1);
    myMap.put("b", 2);

  // Getter
}

I get

<myMap>
  <a>1</a>
  <b>2</b>
</myMap>

But it is supposed to be

<myMap>
  <entry>
    <key>a</key>
    <value>1</value>
  </entry>
  <entry>
    <key>b</key>
    <value>2</value>
  </entry>
</myMap>

Do you know how to solve this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions