JAXB - 编组为 XML 时可以展平类包含吗? [英] JAXB - can class containment be flattened when marshalling to XML?

查看:46
本文介绍了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;
}

更多信息

这篇关于JAXB - 编组为 XML 时可以展平类包含吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆