Jaxb unmarshalls带有命名空间的空列表

时间:2015-02-23 16:15:06

标签: java xml jaxb xml-namespaces unmarshalling

我有一个简单的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;
    }
}

Cat的POJO bean

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

unmarshall代码是:

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的新手,这个问题现在让我困扰了3天! 我之前提出过这个问题,但无法得到正确答案,这是一个更精确的问题。

1 个答案:

答案 0 :(得分:0)

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


}