解析为LinkedHashMap时,XmlMapper缺少root

时间:2018-03-25 14:10:24

标签: java xml jackson linkedhashmap xmlmapper

public static void main(String[] args) throws IOException {
    XmlMapper xmlMapper = new XmlMapper();
    LinkedHashMap result = xmlMapper.readValue("<root><a>1</a><b>2</b></root>", LinkedHashMap.class);
    System.out.println(result);
}

它返回:

{a=1, b=2}

如何让它保持这样的根元素:

{
   root = { 
      a = 1, 
      b = 2
   }
}

1 个答案:

答案 0 :(得分:0)

Please consider creating a real POJO for this use-case as maps are not supported for root elements.