这是我的班级:
@XmlRootElement(name = "foo")
@XmlAccessorType(XmlAccessType.NONE)
public class Foo {
@XmlElement
public Collection getElements() {
List elements = new ArrayList();
elements.add(new Bar);
elements.add(new Bar);
return elements;
}
}
班级Bar
也很简单:
@XmlType(name = "bar")
@XmlAccessorType(XmlAccessType.NONE)
public static final class Bar {
@XmlElement
public String getMessage() {
return "hello, world!";
}
}
这是我在编组Foo
之后得到的:
<foo>
<elements xsi:type="foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<message>hello, world!</message>
</elements>
<elements xsi:type="foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<message>hello, world!</message>
</elements>
</foo>
虽然我期待得到:
<foo>
<bar>
<message>hello, world!</message>
</bar>
<bar>
<message>hello, world!</message>
</bar>
</foo>
我应该修理什么?
答案 0 :(得分:3)
您需要使用elements
注释@XmlElement(name="bar")
属性:
@XmlRootElement(name = "foo")
@XmlAccessorType(XmlAccessType.NONE)
public class Foo {
@XmlElement(name="bar")
public Collection getElements() {
List elements = new ArrayList();
elements.add(new Bar);
elements.add(new Bar);
return elements;
}
}