我的XML應該是這樣的:JAXB的ArrayList的OutputStream
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<results>
<version>1.0</version>
<status>ok</status>
<lastUpdate>2011-11-21 09:23:59.0</lastUpdate>
<total>2</total>
<row>
<areaId></areaId>
<nameEng></nameEng>
<nameGer></nameGer>
</row>
… more <row></row> blocks …
</results>
我怎麼能做到這一點..? 目前我有以下..但我不知道我怎麼能album2返回的OutputStream作爲一個字符串...
List<Row> rows = new ArrayList<Row>();
while(rs.next()){
int albumId = rs.getInt(1);
int bookDocId = rs.getInt(2);
String picUrl = rs.getString(3);
String descEng = rs.getString(4);
String descGer = rs.getString(5);
Row row = new Row();
row.setAlbumId(albumId);
row.setBookDocId(bookDocId);
row.setPicUrl(picUrl);
row.setDescEng(descEng);
row.setDescGer(descGer);
rows.add(row);
}
Album album = new Album();
album.setRows(rows);
File file = new File("album.xml");
JAXB.marshal(album, file);
Album album2 = JAXB.unmarshal(file, Album.class);
file.deleteOnExit();
編輯:
@XmlRootElement
public class Album {
private List<Row> rows = new ArrayList<Row>();
@XmlElement(name="row")
public List<Row> getRows(){
return this.rows;
}
public void setRows(List<Row> rows){
this.rows = rows;
}
Row.class:
public class Row {
private int albumId;
private int bookDocId;
private String picUrl;
private String descEng;
private String descGer;
public int getAlbumId() {
return albumId;
}
public int getBookDocId() {
return bookDocId;
}
public String getPicUrl() {
return picUrl;
}
public String getDescEng() {
return descEng;
}
public String getDescGer() {
return descGer;
}
public void setAlbumId(int albumId) {
this.albumId = albumId;
}
public void setBookDocId(int bookDocId) {
this.bookDocId = bookDocId;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public void setDescEng(String descEng) {
this.descEng = descEng;
}
public void setDescGer(String descGer) {
this.descGer = descGer;
}
}
}
顯示Album.class – Ilya
我編輯的主要發佈.. – krackmoe
更新答案爲返回字符串 – Ilya