xml工具类 package cn.jxau.utils;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import org.dom4j.Document;import org.dom4j.Do
package cn.jxau.utils; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class XmlUtils { private static String filepath; static{ filepath = XmlUtils.class.getClassLoader().getResource("users.xml").getPath(); } public static Document getDocument() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File(filepath)); return document; } public static void write2Xml(Document document) throws IOException{ OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream(filepath), format ); writer.write( document ); writer.close(); } }增删改查实现
package cn.jxau.dao.impl; import java.text.SimpleDateFormat; import org.dom4j.Document; import org.dom4j.Element; import cn.jxau.dao.UserDao; import cn.jxau.domain.User; import cn.jxau.exception.DaoException; import cn.jxau.utils.XmlUtils; public class UserDaoXmlImpl implements UserDao { public void add(User user){ try{ Document document = XmlUtils.getDocument(); Element root = document.getRootElement(); Element user_tag = root.addElement("user"); user_tag.setAttributeValue("id", user.getId()); user_tag.setAttributeValue("username", user.getUsername()); user_tag.setAttributeValue("password", user.getPassword()); user_tag.setAttributeValue("email", user.getEmail()); user_tag.setAttributeValue("birthday", user.getBirthday()==null?"":user.getBirthday().toLocaleString()); user_tag.setAttributeValue("nickname", user.getNickname()); XmlUtils.write2Xml(document); }catch (Exception e) { throw new DaoException(e); } } public User find(String username,String password){ try{ Document document = XmlUtils.getDocument(); Element e = (Element) document.selectSingleNode("//user[@username='"+username+"' and @password='"+password+"']"); if(e==null){ return null; } User user = new User(); String date = e.attributeValue("birthday"); // "" 1980-09-09 if(date==null || date.equals("")){ user.setBirthday(null); }else{ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); user.setBirthday(df.parse(date)); } user.setEmail(e.attributeValue("email")); user.setId(e.attributeValue("id")); user.setNickname(e.attributeValue("nickname")); user.setPassword(e.attributeValue("password")); user.setUsername(e.attributeValue("username")); return user; }catch (Exception e) { throw new DaoException(e); } } //查找注册的用户是否在数据库中存在 public boolean find(String username){ try{ Document document = XmlUtils.getDocument(); Element e = (Element) document.selectSingleNode("//user[@username='"+username+"']"); if(e==null){ return false; } return true; }catch (Exception e) { throw new DaoException(e); } } }