2012-06-15 44 views
1

我的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; 
    } 
} 
} 
+0

顯示Album.class – Ilya

+0

我編輯的主要發佈.. – krackmoe

+0

更新答案爲返回字符串 – Ilya

回答

3

這是我的代碼,它工作得很好

@javax.xml.bind.annotation.XmlType 
@javax.xml.bind.annotation.XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.FIELD) 
public class Album 
{ 
    long version; 

    String status; 

    java.util.List<Row> rows; 

} 


@javax.xml.bind.annotation.XmlType 
@javax.xml.bind.annotation.XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.FIELD) 
public class Row 
{ 
    String areaId; 

    String nameEng; 

    String nameGer; 
} 

測試

public static void main(final String[] args) throws IOException 
    { 
     Album al = new Album(); 
     List<Row> rows = new ArrayList<Row>(); 
     final Row row1 = new Row(); 
     row1.areaId = "area1"; 
     row1.nameEng = "eng1"; 
     row1.nameGer = "ger1"; 
     final Row row2 = new Row(); 
     row2.areaId = "area2"; 
     row2.nameEng = "eng2"; 
     row2.nameGer = "ger2"; 
     rows.add(row2); 
     rows.add(row1); 
     al.status = "stat"; 
     al.rows = rows; 
     final File file = new File("D:/test.xml"); 
     final FileOutputStream out = new FileOutputStream(file); 

     JAXB.marshal(al, out); 

     final Album after = JAXB.unmarshal(file, Album.class); 
     assert after.status.equals(al.status); 
     assert after.rows.size() == al.rows.size(); 
    } 

您可以更改訪問private,並添加的getter,setter方法

要回到像String使用

ByteArrayOutputStream output = new ByteArrayOutputStream(); 
    JAXB.marshal(al, output); 
    output.toString(); 
1

如果你問如何寫一個專輯到輸出流而不是寫入文件,那麼答案很簡單:JAXB.marshal(Object, OutputStream)

如果你問如何將一個Album轉換成一個XML字符串,那麼答案也很簡單:JAXB.marshal(Object, String)

如果您有其他問題,請澄清您的問題。

+0

,但這是一個相冊,其中包含行的arraylist ..這是否也是這樣工作? – krackmoe

+1

爲什麼不呢?您的Album對象具有將對象映射到XML的JAXB註釋。將所需的JAXB註釋添加到Row類,並且一切都會好起來的。 –

+0

Btw ..如果我有一個行對象,是否有可能定義的getter和setter只是在輸出xml中使用時,他們不是空的? – krackmoe