1

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.

Xstian
  • 8,184
  • 10
  • 42
  • 72
Riju Mahna
  • 6,718
  • 12
  • 52
  • 91
  • @Zielu .. Yes it is a duplicate but maybe my question was not clear earlier and the solution didn't work. Hence I asked it again with clean code and xml. Your comments are appreciated but I am still stuck with this issue. – Riju Mahna Feb 23 '15 at 17:17

1 Answers1

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


}
Xstian
  • 8,184
  • 10
  • 42
  • 72
  • Thanks for your response. But now, instead of a null pointer, I am getting a blank list. The `getCats()` in the last line returns an empty list with no data in it. – Riju Mahna Feb 23 '15 at 16:55
  • @RijuMahna Do you use above classes with all annotations that i added? .. strange .. which JDK are you using? In my environment works fine – Xstian Feb 23 '15 at 17:50
  • Yes, I've actually copy-pasted your code to test. I am using JDK 7.. I even tried adding a `setCats` method to the Animal class as it was missing in your code.. But still the same result. :( – Riju Mahna Feb 23 '15 at 18:08
  • 1
    Riju it is time to learn debuging. We both gave you the same answer, and we both checked that it works. Just marshal/unmarshal using your code, check the output xml, see which direction it fails (wrong xml, nor reading back correctly...) – Zielu Feb 23 '15 at 19:18
  • @Zielu . .I did marshall some data into an xml and the namespaces turn out to be quite different. The parent Animal tag has a namespace now. The created xml is : ` myKitty1 myKitty2 ` Not sure what to do with this now .. – Riju Mahna Feb 23 '15 at 19:47
  • Also, this new marshalled xml is working fine while unmarshalling. But again, my issue is with the unmarshalling the ORIGINAL xml, (which I cannot alter). .And thanks for getting this question closed. Didn't know it was so difficult to accept that your solution doesn't work ! – Riju Mahna Feb 23 '15 at 20:06
  • You are welcomed. The output matches your example, the namespace prefixes are irrelevant so does the location of ns declaration. I just run your example through Xstian code and it of course works. So copy your example from this page, ditch your classes, copy the exact Xstian code, save it and try yourself. The setCats method was not missing and is not needed there. I think that two EXACTLY same answers to your question are more than enough. – Zielu Feb 23 '15 at 22:53