2014-05-19 51 views
0

我在使用JAXB解組時遇到問題。我想使XML中的每一項都作爲ArrayList中的對象存儲。但據我嘗試分析它,我總是得到null值:如何使用JAXB解組XML。特殊情況

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

希望得到任何幫助。

+0

你能附上班級的全部代碼嗎? –

+1

檢查http://stackoverflow.com/questions/13178824/unmarshal-xml-into-arrays – geddamsatish

+0

@Miron Balcerzak看編輯問題 – AnZyuZya

回答

1

爲了緩解你的痛苦,這是我的建議。因爲你已經準備好了XML。 1.嘗試通過隨機在線工具生成模式(xsd文件):「XML到模式生成器」 2.從模式生成Java映射。

旁邊。我肯定是否可以像這樣放置@XmlRootElement。我想這應該在包裝類的「板」這樣的指定:

@XmlRootElement 
class Boards{ 

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

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

,然後用所有它的制定者/吸氣板類將被填補。

如果你可以操縱的模式,將其更改爲

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

希望它幫助。

+0

感謝您的回答。但我並不想把一個問題改變成另一個問題。 – AnZyuZya