1:引入jar qn
<dependency> <groupId>xstream</groupId> <artifactId>xstream</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>xpp3</groupId> <artifactId>xpp3_min</artifactId> <version>1.1.3.4.O</version> </dependency>
2:XML (admin.xml)
<ConfigUser> <type>ADMIN</type> <users> <AdminUser> <name>zhaoyb</name> <pwd>abc123</pwd> <ip> <string>127.0.0.1</string> <string>203.119.80.128</string> </ip> </AdminUser> <AdminUser> <name>liangqunxing</name> <pwd>abc123</pwd> <ip> <string>127.0.0.1</string> <string>202.173.100.126</string> </ip> </AdminUser> <AdminUser> <name>liuyu</name> <pwd>abc123</pwd> <ip> <string>127.0.0.1</string> <string>203.119.80.108</string> </ip> </AdminUser> </users> </ConfigUser>
3:java 代码
InputStream xmlInputStream = new ClassPathResource("admin.xml").getInputStream(); XStream xStream = new XStream(); xStream.alias("AdminUser", AdminUser.class); xStream.alias("ConfigUser", ConfigUsers.class); ConfigUsers users = (ConfigUsers) xStream.fromXML(xmlInputStream); List<AdminUser> adminUsers = users.getUsers();
public class ConfigUsers { private String type; private List<AdminUser> users; public String getType() { return type; } public void setType(String type) { this.type = type; } public List<AdminUser> getUsers() { return users; } public void setUsers(List<AdminUser> users) { this.users = users; } @Override public String toString() { return new ToStringBuilder(this) .append("type", type) .append("users", users) .toString(); } }
public class AdminUser { private String name; private String pwd; private List<String> ip; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getIp() { return ip; } public void setIp(List<String> ip) { this.ip = ip; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return new ToStringBuilder(this).append("name", name) .append("pwd", pwd).append("ip", ip).toString(); } }
时间: 2024-10-07 05:07:46