I'm having trouble trying to map nested elements into the same Java class.
XML
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>
Module class
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Module {
@XmlAttribute
private String name;
@XmlElements({
@XmlElement(name = "slide", type = SlideText.class)
})
private Slide slide;
}
Slide class
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Slide {
@XmlAttribute
private String id;
}
SlideText class
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;
}
Is there a way to map <layout><text>hello</text></layout> into SlideText's text property?
Thanks.
UPDATE
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.
Essentially, if this works, I'll be creating SlideImage and SlideTextVideo that extends Slide.
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>
@XmlRootElementon any class other thanModule, assuming it will always be the root element of the XML document.