编程语言
首页 > 编程语言> > java – 如何映射JAXB的对象列表?

java – 如何映射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.

现在的问题是:

我要解组一个简单的xml,但结果总是为null.我尝试了不同的注释,但如果它们没有失败,则结果为null.

这似乎与this question的情况相同,但使用相同的注释是行不通的.

xml类似于:

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

我这样解组它:

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

所以我创建了两个这样的内部类:

@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;
    }

}

我在静态方法中解组xml,所以我不得不把pojos作为静态.

对此有何帮助?

提前致谢.

解决方法:

尝试删除@XmlElementWrapper(name =“ServiceList”),它应该工作

标签:unmarshalling,java,jaxb
来源: https://codeday.me/bug/20190901/1781828.html