如何使用JAXB解组XML。特殊案例

时间:2014-05-19 14:47:53

标签: java xml jaxb

我在使用JAXB进行解组时遇到问题。我想让XML中的每个项目都作为对象存储在ArrayList中。但是,就我尝试解析它而言,我总是得到null值:

Board{ID=null, Brand=null, Name=null, Price=null, shape=null, camber=null, stance=null}

任何人都可以提示一下问题在哪里。我是JAXB的新手。

XML文件boards.xml

<BOARDS>
<ID id="0">
    <BRAND brand="Apo">
        <NAME name="Supreme">
            <description>
                The APO Supreme is an interesting board.  To start off I think someone filed down the edges because we have never experienced so little edge hold. It was to the point where it was a bit scary. Hopefully this was not how the board really rides because there is some snap and pop to it.
            </description>
            <riding_style> All Mountain Freestyle </riding_style>
            <riding_level> Intermediate - Expert </riding_level>
            <shape> Directional Twin </shape>
            <camber_profile> Flat to Rocker </camber_profile>
            <stance> Centered </stance>
            <price> $499 </price>
            <picture>drawable\\snb_apo_supreme.jpg</picture>
        </NAME>
    </BRAND>
</ID>

<ID id="1">
    <BRAND brand="Arbor">
        <NAME name="Draft">
            <description>
                The Arbor Draft was the first snowboard in their line up to go rocker back in 2010. Over the years the Arbor Draft has pretty much remained the same board with the exception of a few minor refinements. This is one of the better jib park boards we came across and it also isn’t bad out of the jib park.
            </description>
            <riding_style> Jib / Street </riding_style>
            <riding_level> Beginner - Expert </riding_level>
            <shape> True Twin </shape>
            <camber_profile> Continuous Rocker </camber_profile>
            <stance> Centered </stance>
            <price> $399 </price>
            <picture>drawable\\snb_arbor_draft.jpg</picture>
        </NAME>
    </BRAND>
</ID>
<BOARDS>

这是应该存储信息的类。 Board课程:

import java.util.ArrayList;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="BOARDS")
class Board {
    private String id;
    private String brand;
    private String name;
    private String description;
    private String price;
    private String shape;
    private String ridingLevel;
    private String ridingStyle;
    private String camber;
    private String stance;
    private String picture;

    ArrayList<Board> listOfBoards;

    public ArrayList<Board> getListOfBoards() {
        return listOfBoards;
    }

    public void setListOfBoards(ArrayList<Board> listOfBoards) {
        this.listOfBoards = listOfBoards;
    }

    public String ToString(){
        return "Board{" + "ID=" + id + ", Brand=" + brand + ", Name=" + name + ", Price=" + price + ", shape=" + shape + ", camber=" + camber + ", stance=" + stance+'}';

    }

    @XmlElement
    public void setId(String id){
        this.id = id;
    }
    @XmlElement
    public void setBrand(String brand){
        this.brand = brand;
    }
    @XmlElement
    public void setName(String name){
        this.name = name;
    }
    @XmlElement
    public void setShape(String shape){
        this.shape = shape;
    }
    @XmlElement
    public void setCamber(String camber){
        this.camber = camber;
    }
    @XmlElement
    public void setRidingLevel(String ridingLevel){
        this.ridingLevel = ridingLevel;
    }
    @XmlElement
    public void setRidingStyle(String ridingStyle){
        this.ridingStyle = ridingStyle;
    }
    @XmlElement
    public void setPrice(String price){
        this.price = price;
    }
    @XmlElement
    public void setStance(String stance){
        this.stance = stance;
    }
    @XmlElement
    public void setDescription(String description){
        this.description = description;
    }
    @XmlElement
    public void setPicture(String picture){
        this.picture = picture;
    }


    public String getId(){
        return id;
    }
    public String getBrand(){
        return brand;
    }
    public String getName(){
        return name;
    }
    public String getDescription(){
        return description;
    }
    public String getPrice(){
        return price;
    }
    public String getRidingLevel(){
        return ridingLevel;
    }
    public String getShape(){
        return shape;
    }
    public String getCamber(){
        return camber;
    }
    public String getStance(){
        return stance;
    }
    public String getRidingStyle(){
        return ridingStyle;
    }
    public String getPicture(){
        return picture;
    }
    }

这里是void main()

try {

        File file = new File("boards.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Board.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Board board = (Board) jaxbUnmarshaller.unmarshal(file);
        System.out.println(board.ToString()); //ToString displays every paramater

    } catch (JAXBException e) {
        e.printStackTrace();
    }

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

为了缓解你的痛苦,这就是我的建议。因为您已准备好XML。 1.尝试通过随机在线工具生成模式(xsd文件):“XML to Schema generator” 2.从架构生成java映射。

侧面。我不确定@XmlRootElement是否可以像这样放置。我想它应该在包装类“Boards”中指定:

@XmlRootElement
class Boards{

   @XmlElement(name="ID")
   private List<Id> id;
}

class Id{
   @XmlElement(name="BOARD")
   private Board board;
}

然后将填写所有它的setter / getter的Board class。

如果您可以操作架构,请将其更改为

   <BRAND brand="Arbor" id="1">

希望它有所帮助。