我的Xml应该看起来像:

<?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作为String返回到输出流...

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;
    }
}
}

最佳答案

这是我的代码,效果很好

@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();

关于java - Jaxb Arraylist输出流,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11046025/

10-12 02:04