is it possible with JAXB to marshal two or more elements into one domain object field?

懵懂的女人 提交于 2019-12-23 20:26:27

问题


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.

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>

I'd like to have a single List<String> field in my domain class which would contain 'foo' and 'bar'

I've tried this:

...
<java-attributes>
    <xml-elements>
        <xml-element java-attribute="idList" name="compoundID_one" />
        <xml-element java-attribute="idList" name="compoundID_two" />
    </xml-elements>
</java-attributes>
...

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?


回答1:


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>
...

All works fine now.



来源:https://stackoverflow.com/questions/15917610/is-it-possible-with-jaxb-to-marshal-two-or-more-elements-into-one-domain-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!