JAXB2:将嵌套元素映射到同一Java类中 [英] JAXB2: Mapping nested elements into the same Java class

查看:140
本文介绍了JAXB2:将嵌套元素映射到同一Java类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试将嵌套元素映射到同一个Java类时遇到了麻烦。

I'm having trouble trying to map nested elements into the same Java class.

XML

我在这里要做的是将 id 属性和 text 元素设置为 SlideText class。

What I'm trying to do here is to set id attribute and text element into SlideText class.

<module name="test project">
    <slide id="1">
        <layout>
            <text>hello</text>
        </layout>
    </slide>
</module>

模块类

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Module {
    @XmlAttribute
    private String  name;

    @XmlElements({
        @XmlElement(name = "slide", type = SlideText.class)
    })
    private Slide   slide;
}

幻灯片类

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Slide {
    @XmlAttribute
    private String  id;
}

SlideText类

我尝试在 text 属性上使用 @XmlElementWrapper ,但我得到一个例外 @XmlElementWrapper 只能应用于集合。

I tried using @XmlElementWrapper on text property, but I get an exception that @XmlElementWrapper can only be applied to a collection.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SlideText extends Slide {

    // how to map this to layout/text elements?
    private String  text;
}

有没有办法映射< layout> < text> hello< / text>< / layout> into SlideText 's text 属性?

Is there a way to map <layout><text>hello</text></layout> into SlideText's text property?

谢谢。

更新

为了说明我在这里要完成的任务,幻灯片可以是任何类型,具体取决于使用的布局。 模块知道它是幻灯片但是它不知道它是什么幻灯片,这就是为什么我有摘要幻灯片类。

To illustrate what I'm trying to accomplish here, the slide can be of any type depending on what layout is used. A module knows it's a slide but it doesn't know what slide it is, which is why I have the abstract Slide class.

基本上,如果这样做,我将创建 SlideImage SlideTextVideo 扩展幻灯片

Essentially, if this works, I'll be creating SlideImage and SlideTextVideo that extends Slide.

以下是实际XML文件的外观: -

Here's how the actual XML file looks like:-

<module name="test project">
    <slide id="1">
        <layout-text>
            <text>hello</text>
        </layout-text>
    </slide>
</module>
<module name="test project">
    <slide id="2">
        <layout-image>
            <image-path>img.jpg</image-path>
        </layout-image>
    </slide>
</module>
<module name="test project">
    <slide id="3">
        <layout-text-video>
            <text>hello</text>
            <video-path>a.mp4</video-path>
        </layout-text-video>
    </slide>
</module>


推荐答案

如果你使用 EclipseLink JAXB(MOXy)然后你可以利用@XmlPath扩展(我是MOXy技术主管):

If you use EclipseLink JAXB (MOXy) then you can leverage the @XmlPath extension for this (I'm the MOXy tech lead):

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SlideText extends Slide {

    @XmlPath("layout/text/text()")
    private String  text;

}



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