jaxb 删除列表标签
jaxb delete list tag
我需要整理 java class 以获得 xml,但我不知道如何删除生成的标签中的标签。
我有一个 class,其中的对象列表采用这种形式
@XmlRootElement(name = "Element")
public class Element {
private List<Foo> foos;
@XmlElementWrapper("fooList")
public List<Foo> getfoos() {
return foos;
}
public void setFoos(List<Foo> foos) {
this.foos = foos;
}
}
而列表的 class Foo 是这样的:
@XmlRootElement
public class Foo {
private String id;
private String code;
@XmlElement
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
当编组得到 xml 我得到这个:
<Element>
<fooList>
<foos>
<string1>asd</string1>
<string2>qwe</string2>
</foos>
<foos>
<string1>poi</string1>
<string2>lkj</string2>
</foos>
</fooList>
</Element>
但是我想得到没有 foos 标签的,像这样:
<Element>
<fooList>
<string1>asd</string1>
<string2>qwe</string2>
<string1>poi</string1>
<string2>lkj</string2>
</fooList>
</Element>
谁能帮帮我?
非常感谢!!
你可以这样做:
@XmlRootElement(name = "Element")
@XmlAccessorType(XmlAccessType.FIELD)
public class Element {
@XmlElementWrapper(name = "fooList")
@XmlElements({
@XmlElement(name = "id", type = Id.class),
@XmlElement(name = "code", type = Code.class),
})
private List<FooItem> foos;
public List<FooItem> getfoos() {
return foos;
}
public void setFoos(List<FooItem> foos) {
this.foos = foos;
}
}
然后 ID 和代码 class 看起来很相似:
public class Id implements FooItem {
@XmlValue
private String id;
public Id() {}
public Id(String id) {
this.id = id;
}
}
它们被一个不做太多事情的接口所限制:
public interface FooItem { }
此结构将允许您将 xml 编组为您指定的所需结构。
您拥有的 class 结构的挑战是 class Foo 有 2 个字段,@XmlValue 只能应用于每个 class 的一个字段。所以有 2 个字段 "forces" 它们代表 @XmlElement 并且它们又必须是 xml 元素的子元素。这就是为什么您的列表中的每个 Foo 实例在 xml 中都有 "intermediate" foo 元素。
我需要整理 java class 以获得 xml,但我不知道如何删除生成的标签中的标签。
我有一个 class,其中的对象列表采用这种形式
@XmlRootElement(name = "Element")
public class Element {
private List<Foo> foos;
@XmlElementWrapper("fooList")
public List<Foo> getfoos() {
return foos;
}
public void setFoos(List<Foo> foos) {
this.foos = foos;
}
}
而列表的 class Foo 是这样的:
@XmlRootElement
public class Foo {
private String id;
private String code;
@XmlElement
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
当编组得到 xml 我得到这个:
<Element>
<fooList>
<foos>
<string1>asd</string1>
<string2>qwe</string2>
</foos>
<foos>
<string1>poi</string1>
<string2>lkj</string2>
</foos>
</fooList>
</Element>
但是我想得到没有 foos 标签的,像这样:
<Element>
<fooList>
<string1>asd</string1>
<string2>qwe</string2>
<string1>poi</string1>
<string2>lkj</string2>
</fooList>
</Element>
谁能帮帮我? 非常感谢!!
你可以这样做:
@XmlRootElement(name = "Element")
@XmlAccessorType(XmlAccessType.FIELD)
public class Element {
@XmlElementWrapper(name = "fooList")
@XmlElements({
@XmlElement(name = "id", type = Id.class),
@XmlElement(name = "code", type = Code.class),
})
private List<FooItem> foos;
public List<FooItem> getfoos() {
return foos;
}
public void setFoos(List<FooItem> foos) {
this.foos = foos;
}
}
然后 ID 和代码 class 看起来很相似:
public class Id implements FooItem {
@XmlValue
private String id;
public Id() {}
public Id(String id) {
this.id = id;
}
}
它们被一个不做太多事情的接口所限制:
public interface FooItem { }
此结构将允许您将 xml 编组为您指定的所需结构。
您拥有的 class 结构的挑战是 class Foo 有 2 个字段,@XmlValue 只能应用于每个 class 的一个字段。所以有 2 个字段 "forces" 它们代表 @XmlElement 并且它们又必须是 xml 元素的子元素。这就是为什么您的列表中的每个 Foo 实例在 xml 中都有 "intermediate" foo 元素。