Jaxb返回空值

时间:2014-05-05 16:53:37

标签: java xml jaxb

我有以下XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<profile>
  <registrations>
    <registration>
        <call-id>test</call-id>
        <user>test</user>
        <contact>test;</contact>
        <agent>test</agent>
        <status>test</status>
        <host>test</host>
        <network-ip>test</network-ip>
        <network-port>1234</network-port>
        <sip-auth-user>test</sip-auth-user>
        <sip-auth-realm>test</sip-auth-realm>
        <mwi-account>test</mwi-account>
    </registration>
  </registrations>
</profile>

两个模型类:

@XmlRootElement(name = "profile")
@XmlAccessorType(XmlAccessType.FIELD)
public class Parent implements IMarker {

    @XmlElementWrapper(name = "registrations")
    @XmlElement(name = "registration")
    private List<Registration> registrations;

    public List<Registration> getRegistrations() {
        return registrations;
    }

    public void setRegistrations(List<Registration> registrations) {
        this.registrations = registrations;
    }

}



@XmlAccessorType(XmlAccessType.FIELD)
public class Registration {

    @XmlAttribute(name = "user")
    private String user;

    @XmlAttribute(name = "contact")
    private String contact;

    @XmlAttribute(name = "agent")
    private String agent;

    @XmlAttribute(name = "status")
    private String status;

    @XmlAttribute(name = "host")
    private String host;

    @XmlAttribute(name = "network-ip")
    private String networkIp;

    @XmlAttribute(name = "network-port")
    private Integer networkPort;

    @XmlAttribute(name = "sip-auth-user")
    private String sipAuthUser;

    @XmlAttribute(name = "sip-auth-realm")
    private String sipAuthRealm;

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getAgent() {
        return agent;
    }

    public void setAgent(String agent) {
        this.agent = agent;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getNetworkIp() {
        return networkIp;
    }

    public void setNetworkIp(String networkIp) {
        this.networkIp = networkIp;
    }

    public Integer getNetworkPort() {
        return networkPort;
    }

    public void setNetworkPort(Integer networkPort) {
        this.networkPort = networkPort;
    }

    public String getSipAuthUser() {
        return sipAuthUser;
    }

    public void setSipAuthUser(String sipAuthUser) {
        this.sipAuthUser = sipAuthUser;
    }

    public String getSipAuthRealm() {
        return sipAuthRealm;
    }

    public void setSipAuthRealm(String sipAuthRealm) {
        this.sipAuthRealm = sipAuthRealm;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }
}

IMarker只是一个标记界面,没有任何方法。 当我解组指定的XML时 - 所有注册字段都为空。 有人可以解释这种行为,我该如何正确地解组这个XML?

1 个答案:

答案 0 :(得分:0)

您将获得null,因为注册标记元素中没有属性,所有都是元素,并且您已使用@XmlAttribute注释映射了所有属性 所以注释你的属性@XmlElement