问题描述
我正在尝试做类似于@XmlElements
的事情,并合并两个字段,但是我想要一种使它们适应相同输出类型的方法。就我而言,我有一个列表标记<Bar>
,但是如果只有一个元素,它将跳过使用<Bar>
并将其直接放入xml元素中。
单个Foo
项目
<Thing id="0x01">
<Foo>
<bla>42</bla>
<baz>test</baz>
</Foo>
<etc>
</Thing>
多个Foo
个项目
<Thing id="0x02">
<Bar>
<Foo>
<bla>42</bla>
<baz>first</baz>
</Foo>
<Foo>
<bla>48</bla>
<baz>second</baz>
</Foo>
</Bar>
<etc>
</Thing>
如果Foo
在父元素中,我想用一个元素将输出收集为一个列表。因此,第一个示例将解析为Arrays.asList(new Foo(42,"test"))
,第二个示例将解析为Arrays.asList(new Foo(42,"first"),new Foo(48,"second"))
。
@XmlRootElement(name = "Thing")
@XmlAccessorType(XmlAccesstype.FIELD)
public class Thing {
@XmlAttribute
public String id;
// I want some way to adapt one variant to the correct type
@XmlElements({
@XmlElement(name="Foo",adapter=PutSingleFooInList.class),@XmlElement(name="Bar"),})
public List<Foo> foos;
// Or in other words,I want some way to fall back on an alias/adapter combo
@XmlElement(name = "Bar")
@XmlAliasAdapter(name="Foo",adapter=PutSingleFooInList.class)
public List<Foo> foos;
}
此外,保证每个Thing
都具有Foo
或Bar
,但绝不能两者都有。 Thing
也不是唯一包含Foo / Bar List<Foo>
的元素类型,因此如果它不依赖于为XmlAdapter
创建Thing
会很有帮助。
目前,我只需要对数据进行反序列化,这样一种以这种格式进行序列化的方法就不错了,但不是必需的。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)