代码之家  ›  专栏  ›  技术社区  ›  BigBen

JAXB是否可以将两个或多个元素编组到一个域对象字段中?

  •  3
  • BigBen  · 技术社区  · 12 年前

    我有两个不同的XML结构,我想映射到一个域对象。我正在使用MOXy的外部绑定支持,这样我就可以选择动态使用哪个绑定。

    这是我的问题。我有一个XML结构,如下所示:

    <entity>
       <compoundID_one>foo</compoundID_one>
       <compoundID_two>bar</compoundID_two>
    </entity>
    

    我想要一个单人间 List<String> 域类中包含“foo”和“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 wrapper我只得到列表中的一个compoundID。

    我发现了这个 question 这似乎表明这应该奏效。我做错了什么吗?或者有更好的方法吗?

    1 回复  |  直到 8 年前
        1
  •  2
  •   BigBen    12 年前

    我只是绑定XML出错了,应该是:

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

    现在一切正常。