XStream, как добавить карту (ключ, значение) в качестве списка атрибутов для корневого узла? - PullRequest
0 голосов
/ 08 июля 2019

Я пытаюсь добавить коллекцию (MAP) в качестве атрибутов узла.Я попробовал код в этом запросе Xstream Implicit Map As Attributes to Root Element , но теперь он решает проблему. Ниже приведен мой код, который я пробовал.

@XStreamAlias("TAG")
public class Tag {

    private Map<String, String> attributes;

    public void addAttribute(String name, String value) {
        if (this.attributes == null)
            this.attributes = new HashMap<String, String>();

        this.attributes.put(name, value);
    }
}

public static void main(String[] args) {
    XStream xStream = new XStream(new DomDriver());
    xStream.autodetectAnnotations(true);

    Tag tag = new Tag();
    tag.addAttribute("KEY1", "VALUE1");
    tag.addAttribute("KEY2", "VALUE2");
    tag.addAttribute("KEY3", "VALUE3");

    System.out.println(xStream.toXML(tag));
}

Фактический вывод такой, как ниже:

<TAG>
  <attributes>
    <entry>
      <string>KEY2</string>
      <string>VALUE2</string>
    </entry>
    <entry>
      <string>KEY1</string>
      <string>VALUE1</string>
    </entry>
    <entry>
      <string>KEY3</string>
      <string>VALUE3</string>
    </entry>
  </attributes>
</TAG>

Ожидаемый результат должен быть таким, как показано ниже

<TAG KEY1=VALUE1 KEY2=VALUE2 KEY3=VALUE3>
</TAG>
...