簡體   English   中英

如何使用jaxb讀取屬性?

[英]How do I read attributes using jaxb?

鑒於此XML:

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

這就是我現在所擁有的,但它不起作用(我得到空的結果):

@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
}

我正在使用RestOperations調用遠程服務,我想解析<detail ..>元素。 我已經嘗試將MyResponseMyResponseDetail類傳遞給RestOperations但結果始終為空。

我的對象結構應該與XML相匹配?

你需要像這樣注釋你的類:

@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;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM