使用jaxb解组xml,列表中的元素丢失

时间:2016-08-04 10:06:01

标签: xml jaxb

我正在使用jaxb来解析xml。管理完成大部分工作,除了我在过程中丢失了一些元素。 这是我的示例代码:

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class JAXBTest {
    public static void main(String[] args) {
        File fXmlFile = new File("src\\main\\resource\\balls.xml");
        if (fXmlFile.isFile()) {
            try {
                JAXBContext context = JAXBContext.newInstance(Balls.class);
                Unmarshaller unmarshaller = context.createUnmarshaller();
                Balls balls = (Balls) unmarshaller.unmarshal(fXmlFile);
                System.out.println(balls);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

@XmlRootElement(name = "Balls")
class Balls {
    private List<Ball> balls = new ArrayList<Ball>();

    @XmlElement(name = "Ball")
    public List<Ball> getBalls() {
        return balls;
    }

    public void setBalls(List<Ball> balls) {
        this.balls = balls;
    }
}

class Ball {
    private String name;
    private List<Color> colors = new ArrayList<Color>();

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

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

    @XmlElement(name = "Colors")
    public List<Color> getColors() {
        return colors;
    }

    public void setColors(List<Color> colors) {
        this.colors = colors;
    }
}

class Color {
    private String color;

    @XmlElement(name = "Color")
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

这是我的样本xml:

<Balls>
<Ball>
<Name>Basketball</Name>
<Colors>
<Color>red</Color>
<Color>black</Color>
</Colors>
</Ball>
</Balls>

我输了#34;红色&#34;在颜色列表中。我错过了什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用以下课程

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;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "ball"
})
@XmlRootElement(name = "Balls")
public class Balls
    implements Serializable
{

    private final static long serialVersionUID = 657456748458L;
    @XmlElement(name = "Ball", required = true)
    protected Ball ball;


    public Ball getBall() {
        return ball;
    }


    public void setBall(Ball value) {
        this.ball = value;
    }

    public boolean isSetBall() {
        return (this.ball!= null);
    }


}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "name",
    "colors"
})
class Ball
    implements Serializable
{

    private final static long serialVersionUID = 657456748458L;
    @XmlElement(name = "Name", required = true)
    protected String name;
    @XmlElement(name = "Colors", required = true)
    protected Colors colors;


    public String getName() {
        return name;
    }

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

    public boolean isSetName() {
        return (this.name!= null);
    }

    public Colors getColors() {
        return colors;
    }


    public void setColors(Colors value) {
        this.colors = value;
    }

    public boolean isSetColors() {
        return (this.colors!= null);
    }


}


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "color"
})
class Colors
    implements Serializable
{

    private final static long serialVersionUID = 657456748458L;
    @XmlElement(name = "Color")
    protected List<String> color;


    public List<String> getColor() {
        if (color == null) {
            color = new ArrayList<String>();
        }
        return this.color;
    }
}