问题描述
说,我有两个班:
@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 中的层次结构? 所以我有:
My question is how to flatten the hierarchy in the XML? So that I have:
<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