User类
public class User implements Serializable { private static final long serialVersionUID = 7506854293380407436L; private Integer id; private String loginName; private String password; private Date createDate; private String username; public User() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public String getUsername() { System.out.println("User类中的getUser方法"); return username; } public void setUsername(String username) { this.username = username; } @Override public String toString() { return "User [id=" + id + ", loginName=" + loginName + ", password=" + password + ", createDate=" + createDate + ", username=" + username + "]"; }
XML文档:admin.xml
<?xml version="1.0" encoding="UTF-8"?> <user id="1"> <loginName>admin@qq.com</loginName> <password>123123</password> <username>超级管理员</username> <createDate>2017-03-07</createDate> </user>
将XML文档解析成Java对象
public class XmlParser { public static void main(String[] args) throws Exception { // 待填充user对象 User user = new User(); // 获得User的class对象,后续要使用反射 Class clazz = user.getClass(); File file = new File("D:\\jsd1707\\xtt\\workspace\\rams\\src\\main\\java\\admin.xml"); SAXReader reader = new SAXReader(); // 读取xml文件到Document中 Document doc = reader.read(file); // 获取xml文件的根节点 Element rootElement = doc.getRootElement(); System.out.println("根节点:" + rootElement.getName()); // 获得根节点的所有属性 List<Attribute> attrList = rootElement.attributes(); for (Attribute attribute : attrList) { // 根节点某个属性名 String attrName = attribute.getName(); System.out.println("当前节点属性名:" + attrName); // 根节点某个属性值 String attrValue = attribute.getValue(); // 通过属性名定位user对象中对应属性的setter方法 Field filed = clazz.getDeclaredField(attrName); Class cla = filed.getType();// 属性类型 System.out.println("当前属性类型名称:" + cla.getName()); Object val = typeTransfer(cla.getName(), attrValue); String methodName = "set" + attrName.substring(0, 1).toUpperCase() + attrName.substring(1); System.out.println("当前节点属性对应方法名:" + methodName); Method method = clazz.getDeclaredMethod(methodName, cla); method.invoke(user, val); } // 获取子节点 List<Element> elements = rootElement.elements(); for (Element element : elements) { // 根节点某个节点名 String elemName = element.getName(); System.out.println("当前节点属性名:" + elemName); // 根节点某个节点值 String elemValue = element.getText(); // 通过节点名定位user对象中对应属性的setter方法 Field filed = clazz.getDeclaredField(elemName); Class cla = filed.getType();// 属性类型 System.out.println("当前属性类型名称:" + cla.getName()); Object val = typeTransfer(cla.getName(), elemValue); String methodName = "set" + elemName.substring(0, 1).toUpperCase() + elemName.substring(1); System.out.println("当前节点属性对应方法名:" + methodName); Method method = clazz.getDeclaredMethod(methodName, cla); method.invoke(user, val); } System.out.println(user); } private static Object typeTransfer(String typeName, String vlaue) throws ParseException { switch (typeName) { case "java.lang.Integer": return Integer.valueOf(vlaue); case "java.lang.String": return vlaue; case "java.util.Date": return new SimpleDateFormat("yyyy-MM-dd").parse(vlaue); } return null; } }
原文:http://www.cnblogs.com/ToukoXu/p/7867978.html