如何使用jaxb读取属性? [英] How do I read attributes using jaxb?

查看:169
本文介绍了如何使用jaxb读取属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此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
}

我正在打电话使用 RestOperations 到远程服务,我想解析< detail ..> 元素。我已经尝试将 MyResponse MyResponseDetail 类传递给 RestOperations 但结果总是空的。

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.

我的对象结构应该与该XML匹配?

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

推荐答案

你需要像这样注释你的类:

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

}

这篇关于如何使用jaxb读取属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆