JAXB - 在编组到 XML 时可以扁平化类包含吗? [英] JAXB - can class containment be flattened when marshalling to XML?
本文介绍了JAXB - 在编组到 XML 时可以扁平化类包含吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
比如说,我有两个班级:
Say, I have two classes:
@XmlRootElement
class A {
@XmlElement
String propertyOfA;
@XmlElement
B b;
}
class B {
@XmlElement
String propertyOfB;
}
JAXB 返回以相应方式格式化的 XML:
JAXB returns an XML formatted in the according way:
<a>
<propertyOfA>valueA</propertyOfA>
<b>
<propertyOfB>valueB</propertyOfB>
</b>
</a>
我的问题是如何扁平化 XML 中的层次结构?所以我有:
<a>
<propertyOfA>valueA</propertyOfA>
<propertyOfB>valueB</propertyOfB>
</a>
这可以通过注释来完成吗?
Can this be done with annotations?
目前我正在考虑为 A 创建一种包装类,它可以按照我希望在 XML 中看到的方式构建字段.有没有更好的办法?
At the moment I am thinking to create a kind of wrapper class for A, that would have fields built the way I want to see them in the XML. Is there a better way?
推荐答案
注意:我是EclipseLink JAXB (MOXy) 领导和成员 JAXB 2 (JSR-222) 专家组.
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
您可以使用 MOXy 的 @XmlPath
扩展来映射此用例:
You could use MOXy's @XmlPath
extension to map this use case:
import java.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
class A {
@XmlElement
String propertyOfA;
@XmlPath(".")
B b;
}
了解更多信息
- http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
- http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html
- http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html
- http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
这篇关于JAXB - 在编组到 XML 时可以扁平化类包含吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文