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.