在解组时,JAXB将多个属性转换为一种类型 [英] JAXB convert multiple attributes to one type while unmarshalling

查看:113
本文介绍了在解组时,JAXB将多个属性转换为一种类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片我有这样的元素< someElement a =b =number =1scale =meters>< child />< / someElement>

比例和数字总是一起显示,但它们没有在一个独占元素中定义。

scale and number always appear together but they are not defined in one exclusive element.

当将 someElement 元素解组到 SomeElement java实例时,我想要<$ c $中的单个成员c> SomeElement 类代表数字 scale

When unmarshalling the someElement element to a SomeElement java instance I want a single member in the SomeElement class to represent number and scale.

目前,数字和比例都是属性,然后 afterUnmarshall()方法用于计算一个数字。

At the moment both number and scale are attributes and then the afterUnmarshall() method is used for calculating one number.

如果没有两个成员字段的大小和比例,有没有办法做到这一点?也许用 XmlAdapter ?在这种情况下我如何使用它?

Is there a way to do this without having the two member fields size and scale? Maybe with a XmlAdapter? How would I use this in this case?

推荐答案

注意:我是 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提供 @XmlTransformation 扩展,允许您将Java字段/属性表示为多个XML节点。

MOXy offers the @XmlTransformation extension that allows you to represent a Java field/property as several XML nodes.

更多信息

  • http://blog.bdoughan.com/2010/08/xmltransformation-going-beyond.html

更新

您也可以使用此用例的 XmlAdapter 。适配器将负责将表示数字和比例的单一类型(在下面表示为FOO)转换为具有数字和比例的单独属性的类型。您仍然需要使用MOXy实现来利用 @XmlPath 扩展名:

Alternatively you could use an XmlAdapter for this use case. The adapter will we responsible for converting the single type representing number and scale (represented as FOO below) to a type with separate properties for number and scale. You will still need to use the MOXy implementation to leverage the @XmlPath extension:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SomeElement {

    @XmlAttribute
    private String a;

    @XmlAttribute
    private String b;

    @XmlJavaTypeAdapter(NumberAndScaleAdapter.class)
    @XmlPath(".")
    private FOO numberAndScale;

    private Child child;

}

更多信息

  • http://blog.bdoughan.com/search/label/XmlAdapter
  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

这篇关于在解组时,JAXB将多个属性转换为一种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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