Make use of @XmlValue in subclass

若如初见. 提交于 2019-12-08 02:53:26

I managed to solve the issue using a XmlAdapter.

In the subclass replace the XmlValue annotation:

@XmlPath(".")
@XmlJavaTypeAdapter(AClassAdapter.class)
private String aValue;

And the adapter implementation:

public class AClassAdapter extends XmlAdapter<AdaptedValue, String> {

    public static class AdaptedValue {

        @XmlValue
        public String value;

        public AdaptedValue() {
        }

        public AdaptedValue(String value) {
            this.value = value;
        }
    }

    @Override
    public String unmarshal(AdaptedValue v) throws Exception {
        return v.value;
    }

    @Override
    public AdaptedValue marshal(String v) throws Exception {
        return new AdaptedValue(v);
    }
}

The XmlPath(".") did the trick. Without it, the marshalled XML still has the value of aValue wrapped in <aValue> nodes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!