Skip to main content

Unmarshalling returned null object from a successfully marshalled XML



i have the following classes which is marshalled as an XML







<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<customer id="100">

<age>21</age>

<hobbies>

<hobby>

<cost>sd</cost>

<name>na</name>

</hobby>

<hobby>

<cost>sd</cost>

<name>nb</name>

</hobby>

</hobbies>

<name>test</name>

</customer>







However, when i tried to unmarshall, I can only create the customer object but not hobby, which returns null Am i doing something wrong here? The problem seems to be with the XML hierarchy?







package com.mytest.jxb;



import java.util.ArrayList;

import java.util.List;



import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlElementRef;

import javax.xml.bind.annotation.XmlElementWrapper;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlSeeAlso;



@XmlRootElement(name="customer")

@XmlSeeAlso({Hobby.class})

public class Customer {



String name;

int age;

int id;

@XmlElementRef

private List<Hobby> hobbyList = new ArrayList<Hobby>();



public Customer(){

//hobbyList = new ArrayList<Hobby>();

}



//@XmlElement

public String getName() {

return name;

}



@XmlElement

public void setName(String name) {

this.name = name;

}



//@XmlElement

public int getAge() {

return age;

}



@XmlElement

public void setAge(int age) {

this.age = age;

}

//@XmlAttribute

public int getId() {

return id;

}



@XmlAttribute

public void setId(int id) {

this.id = id;

}

public void addHobby(Hobby h) {

hobbyList.add(h);

}

@XmlElementWrapper(name="hobbies")

@XmlElement(name = "hobby")

public List<Hobby> getHobbies(){

return hobbyList;

}

}



package com.mytest.jxb;



import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;



@XmlRootElement

class Hobby {



private String name;

private String cost;



public Hobby(){

}



public Hobby(String name, String cost){

this.name = name;

this.cost = cost;

}

//@XmlElement

public void setName(){

this.name = name;

}

@XmlElement

public String getName(){

return name;

}

//@XmlElement

public void setCost(){

this.cost = cost;

}

@XmlElement

public String getCost(){

return cost;

}

}




Comments

  1. Having addHobby(Hobby h) is not enough for JAXB. Your class should be real POJO and should have void setHobbies(List<Hobby> hobbyList) { this.hobbyList = hobbyList; }. Also I think you can safely remove @XmlElementRef from hobbyList field.

    EDIT: I have checked your classes and created the version which works OK:

    package test;

    import java.util.ArrayList;
    import java.util.List;

    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlElementRef;
    import javax.xml.bind.annotation.XmlElementWrapper;
    import javax.xml.bind.annotation.XmlRootElement;

    import org.apache.commons.lang.builder.ToStringBuilder;
    import org.apache.commons.lang.builder.ToStringStyle;

    @XmlRootElement(name = "customer")
    public class Customer {

    String name;
    int age;
    int id;

    private List<Hobby> hobbyList = new ArrayList<Hobby>();

    public Customer() {
    }

    @XmlElement
    public String getName() {
    return name;
    }

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

    @XmlElement
    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }

    @XmlAttribute
    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public void addHobby(Hobby h) {
    hobbyList.add(h);
    }

    @XmlElementWrapper(name = "hobbies")
    @XmlElement(name = "hobby")
    public List<Hobby> getHobbies() {
    return hobbyList;
    }

    @Override
    public String toString() {
    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
    }

    package test;

    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;

    import org.apache.commons.lang.builder.ToStringBuilder;
    import org.apache.commons.lang.builder.ToStringStyle;

    @XmlRootElement
    class Hobby {

    private String name;
    private String cost;

    public Hobby() {
    }

    public Hobby(String name, String cost) {
    this.name = name;
    this.cost = cost;
    }

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

    @XmlElement
    public String getName() {
    return name;
    }

    public void setCost(String cost) {
    this.cost = cost;
    }

    @XmlElement
    public String getCost() {
    return cost;
    }

    @Override
    public String toString() {
    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...