Question

I have the following 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>

And two model classes:

@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 is just a marker interface whithout any methods. When I'm unmarshalling specified XML - all Registration fields are null. Could somebody explain this behaviour and how can I unmarshal this XML correctly?

Was it helpful?

Solution

You are getting null, because there is no attribute inside registration tag element, all are elements,and you have mapped all property with @XmlAttribute annotation so annotate your properties @XmlElement

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top