JAXB2:将嵌套元素映射到同一Java类中 [英] JAXB2: Mapping nested elements into the same Java class
问题描述
我在尝试将嵌套元素映射到同一个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;
}
- http://bdoughan.blogspot.com/2010/09/xpath-based- mapping-geocode-example.html
- http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
- http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html
使用标准JAXB可以利用XmlAdapter:
Using standard JAXB you could leverage an XmlAdapter:
这篇关于JAXB2:将嵌套元素映射到同一Java类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!