package com.du.utils;
import java.io.FileOutputStream;
import java.io.IOException;
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 XmlTool {
private static String filepath;
static{
filepath=XmlTool.class.getClassLoader().getResource("users.xml").getPath();
//filepath = XmlUtils.class.getClassLoader().getResource("users.xml").getPath();
}
//读取文档
public static Document getDcument() throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(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();
}
}
注意:
1.要写入和读取xml 要用到dom4j开发包。
Dao层:
import java.text.SimpleDateFormat;
import java.util.Date;
import org.dom4j.Document;
import org.dom4j.Element;
import com.du.dao.UserDao;
import com.du.domain.Users;
import com.du.utils.XmlTool;
public class UserDaoImpl implements UserDao {
// 存入user 到xml
/*
* (non-Javadoc)
*
* @see com.du.dao.impl.UserDao#add(com.du.domain.Users)
*/
public void add(Users user) {
try {
Document document = XmlTool.getDcument();
Element root = document.getRootElement();
Element e = root.addElement("user");
e.setAttributeValue("id", user.getId());
e.setAttributeValue("username", user.getUsername());
e.setAttributeValue("password", user.getPassword());
e.setAttributeValue("email", user.getEmail());
e.setAttributeValue("birthday", user.getBirthday() == null ? null
: user.getBirthday().toLocaleString());
e.setAttributeValue("nickname", user.getNickname());
XmlTool.write2Xml(document);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
// 根据用户名和秘密查找出这个用户的所有信息
/*
* (non-Javadoc)
*
* @see com.du.dao.impl.UserDao#find(java.lang.String, java.lang.String)
*/
public Users find(String username, String password) {
try {
Document document = XmlTool.getDcument();
Element e = (Element) document
.selectSingleNode("//user[@username='" + username
+ "' and @password='" + password + "']");
if (e == null) {
return null;
}
Users user = new Users();
String date = e.attributeValue("birthday");
if (date == null || date.equals("")) {
user.setBirthday(null);
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(date);
user.setBirthday(d);
}
user.setId(e.attributeValue("id"));
user.setNickname(e.attributeValue("nickname"));
user.setEmail(e.attributeValue("email"));
user.setUsername(username);
user.setPassword(password);
return user;
} catch (Exception e) {
new RuntimeException(e);
}
return null;
}
// 查询xml中是否有同 用户名的人
// 用于判断是否可以添加到数据库(xml)
/*
* (non-Javadoc)
*
* @see com.du.dao.impl.UserDao#find(java.lang.String)
*/
public boolean find(String username) {
try{
Document document =XmlTool.getDcument();
Element e = (Element) document.selectSingleNode("//user[@username='"+username+"']");
if(e==null){
return false;
}
return true;
}catch (Exception e) {
throw new RuntimeException(e);
}
}
}
注意:
1.不会写的时候看dom4j的开发文档中的快速入门