2015-02-23 87 views
1

我有一個簡單的xml unmarshall。但是在輸出中只有一個空列表。沒有例外被拋出。 這是第三方生成的XML,我需要使這個工作沒有任何改變的XML。Jaxb unmarshalls命名空間空列表

的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的新手,現在這個問題讓我煩惱了3天! 我之前問過這個問題,但無法得到正確的答案,這是一個更精確的問題。

+0

@Zielu ..是的,它是一個重複的,但也許我的問題不早了明確和解決方案沒有工作。因此我再次用乾淨的代碼和xml來問它。您的意見是讚賞,但我仍然堅持這個問題。 – 2015-02-23 17:17:32

回答

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


} 
+0

感謝您的回覆。但是現在,我得到一個空白列表,而不是一個空指針。最後一行中的getCats()返回一個沒有數據的空列表。 – 2015-02-23 16:55:46

+0

@RijuMahna你使用上面的類與所有我註釋的註釋? ..奇怪..你在使用哪個JDK?在我的環境中工作正常 – Xstian 2015-02-23 17:50:34

+0

是的,我實際上覆制粘貼了您的代碼來測試。我正在使用JDK 7 ..我甚至嘗試在Animal類中添加一個'setCats'方法,因爲它在代碼中缺失。但仍然是相同的結果。 :( – 2015-02-23 18:08:45