是否可以使用JAXB将两个或多个元素编组到一个域对象字段中? [英] is it possible with JAXB to marshal two or more elements into one domain object field?
问题描述
我有两种不同的XML结构,我想映射到一个域对象。我正在使用MOXy的外部绑定支持,因此我可以选择动态使用哪种绑定。
I have two different XML structures I'd like to map to one domain object. I'm using MOXy's external binding support so I can choose which binding to use dynamically.
这是我的问题。我有一个类似下面的XML结构:
Here's my question. I have an XML structure like the one below:
<entity>
<compoundID_one>foo</compoundID_one>
<compoundID_two>bar</compoundID_two>
</entity>
我想要一个 List< String> $我的域类中的c $ c>字段包含'foo'和'bar'
I'd like to have a single List<String>
field in my domain class which would contain 'foo' and 'bar'
我试过这个:
...
<java-attributes>
<xml-elements>
<xml-element java-attribute="idList" name="compoundID_one" />
<xml-element java-attribute="idList" name="compoundID_two" />
</xml-elements>
</java-attributes>
...
但我得到 null
表示域对象中的字段。如果我省略 xml-elements
包装器,我只会得到列表中的一个复合ID。
but I just get null
for the field in the domain object. If I omit the xml-elements
wrapper I only get one of the compoundID's in the list.
我发现了这个问题似乎建议这应该工作。我做错了什么或有更好的方法吗?
I found this question which seems to suggest this should work. Am I doing something wrong or is there a better way to do this?
推荐答案
我刚刚绑定XML错了,它应该be:
I just had the binding XML wrong, it should be:
...
<java-attributes>
<xml-elements java-attribute="idList">
<xml-element name="compoundID_one" />
<xml-element name="compoundID_two" />
</xml-elements>
</java-attributes>
...
现在一切正常。
这篇关于是否可以使用JAXB将两个或多个元素编组到一个域对象字段中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!