1

我的 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>

我怎样才能做到这一点..?目前我有以下内容..但我不知道如何将专辑2作为字符串返回到输出流...

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

行类:

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

2 回答 2

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();
于 2012-06-15T07:42:28.237 回答
1

如果您询问如何将专辑写入输出流而不是将其写入文件,那么答案很简单:JAXB.marshal(Object, OutputStream)

如果您要问如何将专辑转换为 XML 字符串,那么答案也很简单:JAXB.marshal(Object, String)

如果您要问其他问题,请澄清您的问题。

于 2012-06-15T07:30:51.170 回答