4

Given this XML:

<response>
    <detail Id="123" Length="10" Width="20" Height="30" />
</response>

This is what I have now, but it is not working (I'm getting empty result):

@XmlRootElement(name="response")
public class MyResponse {
    List<ResponseDetail> response;
    //+getters +setters +constructor
}

public class MyResponseDetail {
    Integer Id;
    Integer Length;
    Integer Width;
    Integer Height;
    //+getters +setters
}

I'm making a call to a remote service using RestOperations and I want to parse the <detail ..> element. I've tried passing both MyResponse and MyResponseDetail classes to RestOperations but the result is always empty.

What should my object structure look like to match that XML?

stackular
  • 1,431
  • 1
  • 19
  • 41

1 Answers1

4

You need to annotate your classes like that:

@XmlRootElement
public class Response {

    private List<Detail> detail;

    public void setDetail(List<Detail> detail) {
        this.detail = detail;
    }
    public List<Detail> getDetail() {
        return detail;
    }

}

public class Detail {

    private String id;
    /* add other attributes here */

    @XmlAttribute(name = "Id")
    public void setId(String id) {
        this.id = id;
    }
    public String getId() {
        return id;
    }

}
bdoughan
  • 147,609
  • 23
  • 300
  • 400
Benoit Wickramarachi
  • 6,096
  • 5
  • 36
  • 46