5

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>
4
  • Slide is a complex type. Define it as another object and place the other into this class. Does this not work for you? Commented Feb 3, 2011 at 18:12
  • @fmucar: I added more reasoning to my post above. Commented Feb 3, 2011 at 18:20
  • 1
    Side note: you don't need @XmlRootElement on any class other than Module, assuming it will always be the root element of the XML document. Commented Feb 3, 2011 at 18:21
  • Were you able to get the answer for this? Even I have something like this. I know its been long time but do you remember what you did here? Commented Apr 27, 2021 at 8:21

2 Answers 2

3

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;

}

Using standard JAXB you could leverage an XmlAdapter:

Sign up to request clarification or add additional context in comments.

3 Comments

I can't get it working. I had that @XmlPath just like yours, but the text is still null.
I'm using Moxy 2.3.0-M5, if it makes any difference. :)
Okay, I fixed it. I'm missing the jaxb.properties file. Wow, this is awesome! Thanks!
0

Add a new class Layout:

public class SlideText extends Slide {
    @XmlElement
    private Layout layout;
}

public class Layout {
    @XmlAttribute
    private String  text;
}

2 Comments

Is it possible not to define Layout class at all? I'm hoping to have all the information in the concrete Slide classes. Thanks.
You could try making it an anonymous class within Slide or SlideText.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.