简体   繁体   中英

How unmarshal XML using JAXB. Particular case

I'm having problems with unmarshaling using JAXB. I want to make every item from XML to be stored as object in ArrayList. But as far as I try to parse it, I always get null values:

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

Could anyone give a hint where is the problem. I'm new to JAXB.

XML file 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>

Here's the class where information should be stored. Board class:

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

and here's 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();
    }

Would appreciate any help.

To ease your pain, here's what I suggest. Since you have got your XML ready. 1. Try generating the schema (xsd file) out of it through random online tools: "XML to Schema generator" 2. Generate java mappings from schema.

On the side. I aint sure if @XmlRootElement can be put just like that. I guess it should be specified on the wrapper class "Boards" like that:

@XmlRootElement
class Boards{

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

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

and then Board class with all it's setters/getters would be filled.

If you can manipulate the schema, change it to

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

etc.

hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM