JAXB - 可以编组为XML类围堵被压扁? [英] JAXB - can class containment be flattened when marshalling to XML?

查看:156
本文介绍了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层次所以,我有:

<a>
  <propertyOfA>valueA</propertyOfA>
  <propertyOfB>valueB</propertyOfB>
</a>

可以这样用注释做了什么?

Can this be done with annotations?

目前,我正在考虑创建一种用于包装类的,这将有场建成我希望看到他们在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(莫西) 领导和的 2 JAXB(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屋!

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