JAXB空解组列表与命名空间[复制](Jaxb unmarshalls empty list wit

2019-10-22 01:10发布

这个问题已经在这里有一个答案:

  • 问题在个XML命名空间中的JAXB解组 1个回答

我有一个简单的XML来解读。 但我得到的只是输出空列表。 没有异常抛出。 这是一个第三方生成的xml,我需要进行这项工作,而在XML中的任何改变。

该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>

该POJO bean的动物:

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

该POJO豆的猫

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

该和解组代码是:

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());

最后一行给出了一个空指针异常。

我做得不对的bean类的命名空间? 我是新来JAXB现在这个问题是窃听我三天三夜! 我问这个问题前面,但不能得到正确的答案,这是一个更精确的问题。

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


}


文章来源: Jaxb unmarshalls empty list with namespace [duplicate]