JAXB 解组子属性而不创建子属性 class
JAXB unmarshal child attributes without creation of child class
我想像这样解组一个(简化的)XML结构:
<parent>
<a>AValue</a>
<b>BValue</b>
<c someAttribute = "true">CValue</c>
</parent>
我知道如何通过这样声明 class C 来做到这一点:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "c", propOrder = {
"someAttribute"
})
public class C{
@XmlValue
private String c;
@XmlAttribute ( name="someAttribute")
private boolean someAttribute;
//getters and setters
}
并像这样将其作为 class 父级中的成员:
public class Parent{
private String a;
private String b;
private C c;
//getters and setters for c,b,a
}
找到了,我可以通过 parent.getC().getC();
访问 C
的值
我的问题 是如何实现我不必创建 class C
并获得 value
和 attribute
C
作为 parent
的成员,无需使用新成员和其他 getter 和 setter 编辑 parent
Pojo。
我已经尝试通过 Listeners 来做到这一点并搜索了类似的结构,但我没有任何想法。
我终于想出了如何实现这一点。
有必要使用 @XmlJavaTypeAdapter
注释并将 C class 标记为 @XmlRootElement
以及 @XmlAccessorType(XmlAccessType.FIELD)
。
此外,需要在用 @XmlJavaTypeAdapter
.
注释的 String 成员的 getter 上使用 @XmlTransient
完整解决方案:
Class C:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class C{
@XmlValue
private String c;
@XmlAttribute
private boolean someAttribute;
//getters and setters for both
Class 适配器:
public class Adapter extends XmlAdapter<C, String> {
public String unmarshal(C pC) throws Exception {
//some possible handling with the attribute over pC.getSomeAttribute();
return pC.getC();
}
public C marshal(String pC) throws Exception {
C c = new C();
c.setC(pC)
//some possible handling to set the attribute to c
return c;
}
Class 家长:
public class Parent{
private String a;
private String b;
@XmlJavaTypeAdapter(Adapter.class)
private String c;
@XmlTransient
public String getC() {
return c;
}
//getters and setters for b,a and setter for C
}
我想像这样解组一个(简化的)XML结构:
<parent>
<a>AValue</a>
<b>BValue</b>
<c someAttribute = "true">CValue</c>
</parent>
我知道如何通过这样声明 class C 来做到这一点:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "c", propOrder = {
"someAttribute"
})
public class C{
@XmlValue
private String c;
@XmlAttribute ( name="someAttribute")
private boolean someAttribute;
//getters and setters
}
并像这样将其作为 class 父级中的成员:
public class Parent{
private String a;
private String b;
private C c;
//getters and setters for c,b,a
}
找到了,我可以通过 parent.getC().getC();
访问 C
的值
我的问题 是如何实现我不必创建 class C
并获得 value
和 attribute
C
作为 parent
的成员,无需使用新成员和其他 getter 和 setter 编辑 parent
Pojo。
我已经尝试通过 Listeners 来做到这一点并搜索了类似的结构,但我没有任何想法。
我终于想出了如何实现这一点。
有必要使用 @XmlJavaTypeAdapter
注释并将 C class 标记为 @XmlRootElement
以及 @XmlAccessorType(XmlAccessType.FIELD)
。
此外,需要在用 @XmlJavaTypeAdapter
.
@XmlTransient
完整解决方案:
Class C:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class C{
@XmlValue
private String c;
@XmlAttribute
private boolean someAttribute;
//getters and setters for both
Class 适配器:
public class Adapter extends XmlAdapter<C, String> {
public String unmarshal(C pC) throws Exception {
//some possible handling with the attribute over pC.getSomeAttribute();
return pC.getC();
}
public C marshal(String pC) throws Exception {
C c = new C();
c.setC(pC)
//some possible handling to set the attribute to c
return c;
}
Class 家长:
public class Parent{
private String a;
private String b;
@XmlJavaTypeAdapter(Adapter.class)
private String c;
@XmlTransient
public String getC() {
return c;
}
//getters and setters for b,a and setter for C
}