XML unmarshalling using JAXB

半世苍凉 提交于 2019-12-04 17:21:26

You just need to do following changes and it will work. Change setParams method in Config to

@XmlElement(name = "params") //<--Annotation added here
public void setParam(Params params) {
    this.params = params;
}
Evgeniy Dorofeev

try

@XmlRootElement
class Config {
    private List<Param> params = new ArrayList<Param>();

    @XmlElementWrapper
    @XmlElement(name="param")
    public List<Param> getParams() {
        return params;
    }

    public void setParams(List<Param> params) {
        this.params = params;
    }
}

class Param {
    String a;
    String b;
          ...
}

I think both Param and it's wrapper should be unmarsh. You just unmarsh the list.But the Param model is also need to be unmarshed.

By default a JAXB (JSR-222) implementation will treat all public fields and properties as mapped. A property is recognized as having matching get/set methods. You just need to change the setParam method in the Config class to setParams to match the getParams method.

@XmlRootElement
public class Config {

    private Params params = new Params();

    public Params getParams() {
        return params;
    }
    public void setParams(Params params) {
        this.params = params;
    }

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