3

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.

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Simple and effective! Thanks. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.