1
votes

I'm experiencing no problems while unmarshalling with MOXy. This is the XML, I've unmarshalled.

<eng><shape type="square"><square-specific>dasdasdas</square-specific></shape></eng>

But when marshalling, I get this:

<eng><shape><type/><square-specific>dasdasdas</square-specific></shape></eng>

Here's my model files:

@XmlRootElement(name="eng")
public class Eng {

    private Shape shape;

    public void setShape(Shape shape) {
        this.shape = shape;
    }

    @XmlElement
    public Shape getShape() {
        return shape;
    }
}


@XmlDiscriminatorNode("type")
public abstract class Shape {

}


@XmlDiscriminatorValue("square")
public class Square extends Shape {

    private String squareSpecificAttribute;

    @XmlElement(name="square-specific")
    public String getSquareSpecificAttribute() {
        return squareSpecificAttribute;
    }

    public void setSquareSpecificAttribute(String s) {
        this.squareSpecificAttribute = s;
    }
}

And this is the method in my controller:

@GET
@Produces(MediaType.APPLICATION_XML)
public Eng get(){
    Eng e = new Eng();
    Square s = new Square();
    s.setSquareSpecificAttribute("dasdasdas");
    e.setShape(s);

    return e;
}

I guess I'm missing something, any idea what could it be?

Thanks.

1

1 Answers

1
votes

@XmlDescriminator node takes an XPath. To indicate that type is an attribute you can do the following:

@XmlDescriminatorNode("@type")

For an example see: