初次学习用dom操作xml文件,有很多不足之处,牛人多给点建议,练习时我没对中文做乱码处理,也没做验证哦!O(∩_∩)O~
实体类:User
public class User { private String name; private String pwd; private String email; 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 String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
数据访问层接口:UserDao
public interface UserDao { boolean login(String name, String pwd); void insertUser(User user); List<User> selectUser(); void updateUser(User user); boolean deleteUser(String name); public User findByName(String name); }
接口实现类:UserDaoImpl
public class UserDaoImpl implements UserDao { private static final String PATH="xml文件路径"; private void build(Document dom) { try { //定义转换器 Transformer f = TransformerFactory.newInstance().newTransformer(); //设置输出的编码格式 f.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //构建dom源 DOMSource source = new DOMSource(dom); //指定文件存放的目标路劲 StreamResult sr = new StreamResult(new File(PATH)); //执行转换操作 f.transform(source, sr); } catch (Exception e) { e.printStackTrace(); } } //登陆 public boolean login(String name, String pwd) { boolean flag = false; try { //根据以有的xml文件创建Document对象 Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(PATH)); //获取user节点下的子节点 NodeList list = dom.getElementsByTagName("user"); //遍历list,数据匹配退出 for(int i = 0; i<list.getLength(); i++) { Element el = (Element)list.item(i); if(name.equals(el.getAttribute("name")) && pwd.equals(el.getAttribute("pwd"))) { flag = true; break; } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return flag; } //添加操作 public void insertUser(User user) { try { //创建全新的Document对象 Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); //创建根节点users Element el = dom.createElement("users"); //将根节点添加到dom中 dom.appendChild(el); //创建子节点 Element e2 = dom.createElement("user"); //将子节点添加到根节点中 el.appendChild(e2); //取得xml文件中现有的信息 List<User> users = this.selectUser(); for(int i = 0; i < users.size(); i++){ //创建节点user Element el3 = dom.createElement("user"); User us =users.get(i); //设置节点的属性(name,pwd,email) el3.setAttribute("name", us.getName()); el3.setAttribute("pwd", us.getPwd()); el3.setAttribute("email", us.getEmail()); //添加到根节点中 el.appendChild(el3); } e2.setAttribute("name", user.getName()); e2.setAttribute("pwd", user.getPwd()); e2.setAttribute("email", user.getEmail()); build(dom); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //查询操作 public List<User> selectUser() { List<User> userList = new ArrayList<User>(); try { //根据已有的xml文件创建dom Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(PATH)); //获取所有user节点下的子节点 NodeList list = dom.getElementsByTagName("user"); for(int i = 0;i <list.getLength();i++){ User user =new User(); Element element = (Element)list.item(i); user.setName(element.getAttribute("name")); user.setPwd(element.getAttribute("pwd")); user.setEmail(element.getAttribute("email")); userList.add(user); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return userList; } //修改操作 public void updateUser(User user) { try { //根据以有的xml文件创建dom Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(PATH)); //获取user节点下的子节点 NodeList list = dom.getElementsByTagName("user"); //遍历list for(int i = 0;i < list.getLength();i++) { Element el = (Element)list.item(i); //根据name属性去修改 if(user.getName().equals(el.getAttribute("name"))) { el.setAttribute("pwd", user.getPwd()); el.setAttribute("email", user.getEmail()); build(dom); } } } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //删除操作 public boolean deleteUser(String name) { try { //根据以有xml文件创建domcumet对象 Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(PATH)); //获取user节点下的子节点 NodeList list = dom.getElementsByTagName("user"); //遍历list for(int i=0;i<list.getLength();i++) { Element el = (Element)list.item(i); if(name.equals(el.getAttribute("name"))) { el.getParentNode().removeChild(el); build(dom); return true; } } } catch (Exception e) { // TODO: handle exception } return false; } //根据name查找 public User findByName(String name) { User user = new User(); try { //根据以后的xml文件创建document对象 Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(PATH)); //获取user节点下的子节点集合 NodeList list = dom.getElementsByTagName("user"); for(int i=0;i<list.getLength();i++) { Element el = (Element)list.item(i); if(name.equals(el.getAttribute("name"))) { user.setName(el.getAttribute("name")); user.setPwd(el.getAttribute("pwd")); user.setEmail(el.getAttribute("email")); break; } } } catch (Exception e) { e.printStackTrace(); } return user; } }
业务逻辑层接口:UserService