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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex