JAXB - can class containment be flattened when marshalling to XML?(JAXB - 编组为 XML 时可以展平类包含吗?)
问题描述
说,我有两个班:
@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
这篇关于JAXB - 编组为 XML 时可以展平类包含吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JAXB - 编组为 XML 时可以展平类包含吗?


基础教程推荐
- 大摇大摆的枚举 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 从 python 访问 JVM 2022-01-01