Unmarshalling возвратил нулевой объект из успешно упорядоченного XML - PullRequest
0 голосов
/ 10 февраля 2012

у меня есть следующие классы, которые маршалируются как 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>     

Однако, когда я попытался демаршировать, я могу создать только объект клиента, но не хобби, которое возвращает ноль. Я делаю что-то не так?Вот?Кажется, проблема в иерархии 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 Ответ

1 голос
/ 12 февраля 2012

Наличие addHobby(Hobby h) недостаточно для JAXB.Ваш класс должен быть настоящим 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);
    }
}
...