1 //存入数据库 2 try { 3 //解析xml 4 SAXReader reader = new SAXReader(); 5 String path = this.getServletContext().getRealPath("WEB-INF/classes/users.xml"); 6 Document dom = reader.read(path); 7 Element root = dom.getRootElement(); 8 //凭空创建userEle保存用户信息 9 Element userEle = DocumentHelper.createElement("user"); 10 userEle.setAttributeValue("username", username); 11 userEle.setAttributeValue("password", password); 12 userEle.setAttributeValue("nickname", nickname); 13 userEle.setAttributeValue("email", email); 14 //挂载到根节点 15 root.add(userEle); 16 //更新xml 17 XMLWriter wrtier = new XMLWriter(new FileOutputStream(path),OutputFormat.createPrettyPrint()); 18 wrtier.write(dom); 19 wrtier.close(); 20 } catch (Exception e) { 21 e.printStackTrace(); 22 throw new RuntimeException(e); 23 } 24 }