如果现有 xml 结构与 JAXB
期望的方式不完全一致,则 JAXB
不允许您将现有 xml 结构解组到 HashMap
中。
JAXB
可以很好地处理例如 LinkedList
并填充它们。
我正在考虑使用 getKey()
方法创建一个接口,并在 HashMap
周围创建一个包装器,以获取实现该接口的所有对象。然后,包装器可以使用 getKey()
方法来获取地图的所有关键相关功能。然后,包装器可以轻松实现Collection
或List
接口。
因为这个想法对我来说似乎没有创新,我认为它已经存在于某个包中,但我没有正确地谷歌搜索它......有人可以命名一个可以做到这一点的好库吗,或者我必须这样做我自己编写这个代码吗?
JAXB
doesn't let you unmarshal already existing xml structures into HashMap
s if they are not exactly the way JAXB
expects them.
JAXB
is fine with handling e.g. LinkedList
s and filling them.
I was thinking of creating a interface with a getKey()
method and a wrapper around the HashMap
taking all objects that implement that interface. The wrapper can then use the getKey()
method for all key related features of the map. The wrapper could then easily implement the Collection
or List
interface.
Because this idea doesn't seem to innovative to me I presume that it already exists in some package, but I'm not googling correctly for it... Can someone please name a good lib that can do this, or do I have to code this myself?
发布评论
评论(2)
你可以考虑扩展guava的
ForwardingList
,并在后面使用HashMap。我不知道有什么实现可以让您只留下实际的映射。另一种替代方法是创建 JAXB
XmlAdapter
来使值适应您的地图。我觉得这个比较合适。You might consider extending
ForwardingList
of guava, and using a HashMap in the back. I don't know of any implementation that will leave you only the actual mapping.Another alternative is creating JAXB
XmlAdapter
to adapt the values to your map. I think this one is more appropriate.如果您尝试将
Map
的信息内容作为Collection
传递,请使用Map.entrySet()
。这会给你一个Set>
对象;即一个集合,其元素是Map
的键/值对。要从集合中重建Map
,您需要迭代集合并对每个元素执行put
。If all you are trying to pass the information content of a
Map
as aCollection
, useMap.entrySet()
. That gives you aSet<Map.EntrySet<K,V>>
object; i.e. a collection whose elements are the key/value pairs of theMap
. To reconstruct aMap
from the collection, you will need to iterate the set and perform anput
for each element.