Unmarshalling从成功编组的XML返回null对象

时间:2012-02-10 16:19:10

标签: java jaxb marshalling unmarshalling

我有以下类作为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>     

然而,当我试图解组时,我只能创建客户对象而不是业余爱好,它会返回null 我在这里做错了吗?问题似乎与XML层次结构有关吗?

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

1 个答案:

答案 0 :(得分:1)

对于JAXB,addHobby(Hobby h)是不够的。你的课应该是真正的POJO,应该有void setHobbies(List<Hobby> hobbyList) { this.hobbyList = hobbyList; }。另外,我认为您可以安全地从@XmlElementRef字段中删除hobbyList

编辑:我检查了您的课程并创建了可行的版本:

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