我在使用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();
}
希望得到任何幫助。
你能附上班級的全部代碼嗎? –
檢查http://stackoverflow.com/questions/13178824/unmarshal-xml-into-arrays – geddamsatish
@Miron Balcerzak看編輯問題 – AnZyuZya