我一直在使用Xsrteam将XML解析为ArrayList。它起作用了,但是由于某种原因我无法到达数组的内容。
处理ArrayList数据的类:
@XStreamAlias("BOARDS")
public class Boards {
@XStreamImplicit(itemFieldName = "id")
public ArrayList ids = new ArrayList();
//region GETTERS-SETTERS
public void setIds(ArrayList temp){
this.ids = temp;
}
public List getIds(){
return ids;
}
// endregion
}
@XStreamAlias("id")
class Id {
@XStreamAlias("board")
private String board;
@XStreamAlias("description")
private String description;
@XStreamAlias("price")
private String price;
@XStreamAlias("shape")
private String shape;
@XStreamAlias("riding_level")
private String ridingLevel;
@XStreamAlias("riding_style")
private String ridingStyle;
@XStreamAlias("camber_profile")
private String camber;
@XStreamAlias("stance")
private String stance;
@XStreamAlias("picture")
private String picture;
<<public getters - setters here>>
}
我如何尝试访问这些
getters
:Boards boards = (Boards) xstream.fromXML(reader); // parse xml into array list
boards.getIds().get(0).getPrice(); //!!getPrice() cannot be resolved
first
是Object first = boards.getIds().get(0);
这是使用调试器的外观:
最佳答案
Boards
具有原始类型,因为不清楚,ArrayList ids
中应包含哪些对象类型。因此,您应该显式地转换boards.getIds().get(0)
的结果:
((Id) boards.getIds().get(0)).getPrice()
或生成
Boards
类:public class Boards<E> {
public List<E> ids = new ArrayList<E>();
//region GETTERS-SETTERS
public void setIds(ArrayList<E> temp){
this.ids = temp;
}
public List<E> getIds(){
return ids;
}
// endregion
}
您可以阅读有关泛型here的信息