javaBean:文件名 Users.java package com.lpy.test;public class Users {private int id;private String name;private String pwd;private int rolesId;private Roles roles;public int getId() {return id;}public void setId(int id) {this.id = id;}pub
package com.lpy.test; public class Users { private int id; private String name; private String pwd; private int rolesId; private Roles roles; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public int getRolesId() { return rolesId; } public void setRolesId(int rolesId) { this.rolesId = rolesId; } public Roles getRoles() { return roles; } public void setRoles(Roles roles) { this.roles = roles; } @Override public String toString() { return "Users [id=" + id + ", name=" + name + ", pwd=" + pwd + ", rolesId=" + rolesId + ", roles=" + roles + "]"; } public Users() { super(); // TODO Auto-generated constructor stub } public Users(int id, String name, String pwd, int rolesId, Roles roles) { super(); this.id = id; this.name = name; this.pwd = pwd; this.rolesId = rolesId; this.roles = roles; } }javaBean:文件名 Roles.java
package com.lpy.test; public class Roles { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Roles(int id, String name) { super(); this.id = id; this.name = name; } public Roles() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Roles [id=" + id + ", name=" + name + "]"; } }测试类:Test.java
package com.lpy.test; import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Test { public static void main(String[] args) throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/Users.xml")); Element root = document.getRootElement(); Listxml文件Users.xmllist = new ArrayList (); getElementsByXml(root,Users.class,list); for (Users users : list) { System.out.println(users); } } public static void getElementsByXml(Element root, Class c,List list) throws Exception { List elements = root.elements(); Object object = c.newInstance(); for (Element element : elements) { if(c.getSimpleName().equals(element.getName())){ object = setInfo(element,c); list.add(object); } } } public static Object setInfo(Element root, Class c) throws Exception{ Object object = c.newInstance(); List elements = root.elements(); for (Method me : c.getMethods()) { for (Element son : elements) { if(me.getName().equalsIgnoreCase("set"+son.getName())){ Class paramc = me.getParameterTypes()[0]; Object value = null; if(!hasNextNode(son)){ String elementText = son.getText(); switch (paramc.getSimpleName()) { case "int": case "Integer": value = Integer.parseInt(elementText); break; case "float": case "Float": value=Float.parseFloat(elementText); break; case "double": case "Double": value=Double.parseDouble(elementText); break; case "long": case "Long": value=Long.parseLong(elementText); break; case "boolean": case "Boolean": value=Boolean.parseBoolean(elementText); break; case "short": case "Short": value=Short.parseShort(elementText); break; case "byte": case "Byte": value=Byte.parseByte(elementText); break; default: value=elementText; } }else{ value = setInfo(son,paramc); } me.invoke(object,value); } } } return object; } public static boolean hasNextNode(Element root){ List elements = root.elements(); if(elements.size()!=0){ return true; } return false; } }
1001 张三 123 1 1 boss 1002 李四 123456 2 2 manager 1003 王五 159 1 1 boss