Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a list of object for JAXB?

NOTE:

I don't need to create the java objects because I just have to check some values, but I didn't find anything to unmarshal them as a generic Object or a Tree or anything. Something like the JsonNode from Jackson. If this is possible let me know, so I can avoid all this mess with objects to map everything.

Now the problem:

I've to unmarshal a simple xml but the result is always null. I've tried different annotations but if they're not failing the result is null.

This seems to be the same case of this question but using the same annotations is not working.

The 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've created two inner classes like this:

@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'm unmarshalling the xml inside a static method, so I had to put the pojos as static.

Any help on this?

Thanks in advance.

like image 728
Enrichman Avatar asked Jan 24 '26 14:01

Enrichman


1 Answers

try to remove @XmlElementWrapper(name = "ServiceList"), it should work

like image 106
Evgeniy Dorofeev Avatar answered Jan 26 '26 03:01

Evgeniy Dorofeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!