[英]How to marshall an unwrapped collection in JAXB?
This is my class: 这是我的班级:
@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;
}
}
Class Bar
is also simple: Class Bar
也很简单:
@XmlType(name = "bar")
@XmlAccessorType(XmlAccessType.NONE)
public static final class Bar {
@XmlElement
public String getMessage() {
return "hello, world!";
}
}
This is what I'm getting after marshalling of Foo
: 这是我整理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>
While I'm expecting to get: 当我期望得到:
<foo>
<bar>
<message>hello, world!</message>
</bar>
<bar>
<message>hello, world!</message>
</bar>
</foo>
What should I fix? 我该怎么办?
You will need to annotate the elements
property with @XmlElement(name="bar")
: 您将需要使用@XmlElement(name="bar")
注释elements
属性:
@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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.