简体   繁体   English

JAXB-当包装器元素为root时,如何使用XmlAdapter解组列表?

[英]JAXB - How to use XmlAdapter for unmarshalling a list when wrapper element is root?

Exists a way to unmarshall a list to a map when the wrapper element of the list is the root itself? 当列表的包装元素是根本身时,是否存在一种将列表解组到地图的方法?

I have following xml structure which I cannot change: 我有以下无法更改的xml结构:

<?xml version="1.0"?>
   <root>
     <article id="1">...</article>
     <article id="2">...</article>
    ...
  </root>

And I like to have the following Map Structure: 我喜欢具有以下地图结构:

Map<Integer,Article> map 

I tried to use the XmlAdapter but the problem is the unmarshal method of the adapter. 我尝试使用XmlAdapter,但问题是适配器的解组方法。 Here it likes to have an own wrapper element eg "articles" which I don't have. 在这里,它喜欢拥有自己的包装元素,例如我没有的“ articles”。 I found only working examples where the list had its own dedicated wrapper element. 我只找到了工作示例 ,其中列表具有自己的专用包装器元素。

I tried with "unmarshall(List articles)" but then it gets never called. 我尝试使用“ unmarshall(列表文章)”,但从未被调用。 And with "unmarshall(Object articles)" it will call th method for every article element 并使用“ unmarshall(对象文章)”将为每个文章元素调用方法

Any hints? 有什么提示吗?

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataSet {
   @XmlJavaTypeAdapter(MyAdapter.class)
   @XmlElement(name="article")
   private Map<Integer,Article> map;
}

public class MyAdapter extends XmlAdapter< ??? , Map<Integer,Article>  {

    @Override
    public Map<Integer, Article> unmarshal( ???  ) throws Exception {
     Map<Integer, Article> map = new HashMap<Integer, Article>();
     ...
     return map;
    }
    ...
}

答案是,当前不支持它,您需要自己动手做,这意味着在解组后您需要遍历对象列表并将其自己放置在地图上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM