使用JAXB编写对象的迭代器

时间:2012-03-08 15:02:58

标签: java iterator jaxb

我遇到了使用JAXB

编组对象迭代器的问题

用户类:

@XmlRootElement(name="User")
public class User{
    private Long id;
    private String name,mailid;
    private boolean isActive;
    public Long getId() {

        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMailid() {
        return mailid;
    }
    public void setMailid(String mailid) {
        this.mailid = mailid;
    }
    public boolean isActive() {
        return isActive;
    }
    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

}

返回用户的迭代器的功能

public static Iterator<User> mapDoToUserObject(DataObject dao){

    final Iterator<Row> userRow = getRow(dao,USER.TABLE);


    return new Iterator<User>() {
        @Override
        public boolean hasNext() {
            return userRow.hasNext();
        }

        @Override
        public User next() {
            Row user = userRow.next();
            User user = new User();
            user.setId((Long)user.get(USER.USER_ID));
            user.setName((String) user.get(USER.FIRST_NAME));
            user.setMailid((String)user.get(USER.EMAIL_ID));
            return user;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Remove not supported");

        }
    };  
}

如何使用JAXB封送用户的迭代器?

1 个答案:

答案 0 :(得分:4)

您需要将迭代器排放到集合中然后编组(这是最简单的解决方案),或者使用JAXB的增量编组功能,这意味着自己迭代迭代器并编组各个对象。有关如何执行此操作的说明,请参阅Can JAXB Incrementally Marshall An Object?