日期:2014-05-20  浏览次数:20854 次

【求助】使用jaxb如何序列化 List对象啊
Java code

List<Book> books =new ArrayList<Book>();
Book book=new Book();
book.setName="书名1";
book.setContent="内容1";

Book book2=new Book();
book2.setName="书名2";
book2.setContent="内容2";
books.add(book);
books.add(book2);




请问使用jaxb 如何实现 list 和xml之间的相互转化??
非常感谢

------解决方案--------------------
book类
Java code

private String name;
    private String content;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

------解决方案--------------------
Book.java
Java code
@XmlType(propOrder={"name","content"})
@XmlRootElement(name="book")
public class Book {
    private String name;
    private String content;

    @XmlElement
    public final String getContent() {
        return content;
    }

    public final void setContent(final String content) {
        this.content = content;
    }

    @XmlElement
    public final String getName() {
        return name;
    }

    public final void setName(final String name) {
        this.name = name;
    }
}