Jaxb unmarshalls empty list with namespace [duplicate]

泪湿孤枕 提交于 2019-12-11 10:38:39

问题


I have a simple xml to unmarshall. But I get only an empty list in the output. No exceptions are thrown. This is a third party generated xml and I need to make this work without any changes in the xml.

The XML:

<Animal  xmlns="http://allmycats.com/serviceplatform/1.0/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Cat z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<name>kitty</name>
</Cat>
<Cat z:Id="i2" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<name>kitty2</name>
</Cat>
</Animal>

The POJO bean for Animal:

@XmlRootElement(name = "Animal",namespace = "http://allmycats.com/serviceplatform/1.0/")

public class Animal{
    List<Cat> cats;
    @XmlElement(name = "Cat")
    public List<Cat> getCats() {
        return cats;
    }
    public void setCats(List<Cat>cats) {
        this.cats= cats;
    }
}

The POJO bean for Cat

@XmlRootElement(name = "Cat")
public class Cat {
    private String zId;
    private String name;
    @XmlAttribute(name = "Id", namespace="http://schemas.microsoft.com/2003/10/Serialization/")
    public String getzId() {
        return zId;
    }
    public void setzId(String zId) {
        this.zId = zId;
    }
    @XmlElement(name = "name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

The unmarshall code is:

File file = new File(filepath);
System.out.println("file exists? : "+ file.exists()); // prints true

JAXBContext jaxbContext = JAXBContext.newInstance(Animal2.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Animal2 animals = (Animal2)jaxbUnmarshaller.unmarshal( file );
System.out.println("--file size: "+animals.getCats().size());

The last line gives a null pointer exception.

Am I doing something wrong with the namespaces in the BEAN classes? I am new to Jaxb and this issue is bugging me for 3 days now ! I asked this question earlier but couldn't get proper answer and this is a more precise question.


回答1:


Animal.java

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "Animal",namespace = "http://allmycats.com/serviceplatform/1.0/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Animal", propOrder = {
    "cats"
})
public class Animal implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @XmlElement(name = "Cat")
    protected List<Cat> cats;

    public List<Cat> getCats() {
        if (cats == null) { // This solve the nullpointer
            cats = new ArrayList<Cat>();
        }
        return this.cats;
    }

}

Cat.java

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "Cat")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Cat", propOrder = {
        "name"
})
public class Cat implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @XmlAttribute(name = "Id", namespace="http://schemas.microsoft.com/2003/10/Serialization/")
    private String zId;

    @XmlElement(name = "name")
    private String name;

    public String getzId() {
        return zId;
    }

    public void setzId(String zId) {
        this.zId = zId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}


来源:https://stackoverflow.com/questions/28678138/jaxb-unmarshalls-empty-list-with-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!