日期:2014-05-20 浏览次数:20904 次
public class People { private String sex; private List userList; } public calss User { private String name; private String address; }
<people> <sex>sex</sex> <user> <name>name</name> <address>address</address> </user> <user> <name>name</name> <address>address</address> </user> <user> <name>name</name> <address>address</address> </user> </people>
@XStreamAlias("people") public class People { private String sex; @XStreamImplicit private List<User> userList; public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } } @XStreamAlias("user") public class User { private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } public class WritePeople { public static void main(String[] args) { People p = new People(); p.setSex("male"); List<User> list = new ArrayList<User>(); User u = new User(); u.setName("zhang"); u.setAddress("beijing"); list.add(u); p.setUserList(list); //Serialize the object XStream xs = new XStream(); xs.processAnnotations(People.class); //Write to a file in the file system try { FileOutputStream fs = new FileOutputStream("people.txt"); xs.toXML(p, fs); } catch (FileNotFoundException e1) { e1.printStackTrace(); } } } public class ReadPeople { public static void main(String[] args) { XStream xs = new XStream(new DomDriver()); xs.processAnnotations(People.class); People p = new People(); try { FileInputStream fis = new FileInputStream("people.txt"); xs.fromXML(fis, p); //print the data from the object that has been read System.out.println(p.getUserList().get(0).getName()); } catch (FileNotFoundException ex) { ex.printStackTrace(); } } }
------解决方案--------------------
1.dto结构如下:
public class people{ private Sex sex; private List<User> user; //getter,setter } public class Sex{ private String sex; //getter,setter } public classs User{ private String name; private String address; //getter,setter }
------解决方案--------------------