How do I map a list of objects for JAXB?

Note:

I don't need to create java objects because I just need to check some values, but I haven't found anything to decouple them as a generic Object or tree or whatever. Something like JsonNode from Jackson. If possible, let me know, so I can avoid all this mess with objects to display everything.

Now the problem:

I have to unmount a simple xml, but the result is always null. I've tried different annotations, but if they don't fail, the result is zero.

It looks like the same case this question , but using the same annotations doesn't work.

xml is something like:

<ServiceList>
   <Service Id="1" Code="c" Name="name" ServiceRegistrationStatusID="3" CheckedRegistrationStatusID="2" />
</ServiceList>

      

and I'm unmarshalling it like this:

ServiceList list = JAXB.unmarshal(new StringReader(xml), ServiceList.class);

      

so I created two inner classes:

@XmlRootElement
public static class ServiceList {
    private List<Service> services;

    public List<Service> getServices() { return services; }
    @XmlElementWrapper(name = "ServiceList")
    @XmlElement(name = "Service")
    public void setServices(List<Service> services) { this.services = services; }

}

@XmlRootElement(name = "Service")
public static class Service {
    private String id;
    private String code;
    private String name;
    private String serviceRegistrationStatusID;
    private String checkedRegistrationStatusID;

    public String getId() {
        return id;
    }
    @XmlAttribute(name = "Id")
    public void setId(String id) {
        this.id = id;
    }
    public String getCode() {
        return code;
    }
    @XmlAttribute(name = "Code")
    public void setCode(String code) {
        this.code = code;
    }
    public String getName() {
        return name;
    }
    @XmlAttribute(name = "Name")
    public void setName(String name) {
        this.name = name;
    }
    public String getServiceRegistrationStatusID() {
        return serviceRegistrationStatusID;
    }
    @XmlAttribute(name = "ServiceRegistrationStatusID")
    public void setServiceRegistrationStatusID(String serviceRegistrationStatusID) {
        this.serviceRegistrationStatusID = serviceRegistrationStatusID;
    }

    public String getCheckedRegistrationStatusID() {
        return checkedRegistrationStatusID;
    }
    @XmlAttribute(name = "CheckedRegistrationStatusID")
    public void setCheckedRegistrationStatusID(String checkedRegistrationStatusID) {
        this.checkedRegistrationStatusID = checkedRegistrationStatusID;
    }

}

      

I am parsing the xml inside a static method, so I had to put pojos as static.

Any help on this?

Thanks in advance.

+3


source to share


1 answer


try uninstalling @XmlElementWrapper(name = "ServiceList")

it should work



+5


source







All Articles