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 时可以展平类包含吗?


- Jersey REST 客户端:发布多部分数据 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01