Dom4j学习感受

package cdu.edu.cy.test2.dao;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.*;

import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.io.*;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;



import cdu.edu.cy.test2.model.User;
public class UserDaoXML {
private User u = null;
private String xmlPath="E:/workplace/XMLTest4/src/cdu/edu/cy/test2/dao/users.xml";
//DOM访问中要使用的变量
public DocumentBuilderFactory factory;
public DocumentBuilder builder;
public Document doc;
File file = new File(xmlPath);
private Document getDocument(File file) throws Exception {
SAXReader reader = new SAXReader();
Document doc = reader.read(file);
return doc;
}


public UserDaoXML(){
try{
factory = DocumentBuilderFactory.newInstance();
    builder = factory.newDocumentBuilder();
  //  doc = builder.parse(xmlPath);
}catch(Exception e){
e.printStackTrace();
}

}
private boolean Add(User user1) throws Exception{

Document doc=getDocument(file);
Element root=doc.getRootElement();
Element num=(Element)root.elementIterator().next();
int ID = Integer.parseInt(num.getText());
//修改num的值
num.setText(String.valueOf(++ID));

Element user=root.addElement("user");
Element BaseInfo = user.addElement("BaseInfo");
Element OtherInfo = user.addElement("OtherInfo");
BaseInfo.addElement("id").addText(String.valueOf(++ID));
BaseInfo.addElement("uname").addText(user1.getUname());
BaseInfo.addElement("password").addText(user1.getPassword());
BaseInfo.addElement("sex").addText(user1.getSex());
OtherInfo.addElement("birthyear").addText(String.valueOf(user1.getBirthYear()));
OtherInfo.addElement("birthmonth").addText(String.valueOf(user1.getBirthMonth()));
OtherInfo.addElement("birthday").addText(String.valueOf(user1.getBirthDay()));
OtherInfo.addElement("school").addText(user1.getSchool());
OtherInfo.addElement("emailaddress").addText(user1.getEmailAddress());
OutputFormat format=new OutputFormat();
format.setEncoding("GBK");
XMLWriter writer=new XMLWriter(new FileWriter(file),format);
writer.write(doc);
writer.close();
return true;
}
/**
* 添加新用户
*/
public boolean UserAdd(User user1)throws Exception{
if(Add(user1))
{
return true;
}
return false;
}
/**
* 删除用户消息
*/
public boolean UserDelete(int id) throws Exception{
Document doc=getDocument(file);
Element root =doc.getRootElement();
List<Element> list=root.elements("user");
for (Element el: list) {
Element ur = (Element)el;
Element Base = el.element("BaseInfo");
if (Integer.parseInt(Base.element("id").getTextTrim())==id) {
root.remove(el);
OutputFormat format=new OutputFormat();
     format.setEncoding("GBK");
XMLWriter writer=new XMLWriter(new FileWriter(file));
writer.write(doc);
writer.close();
return true;
}
}
return false;
}
/**
* 更新用户消息
*/
public boolean UserUpdate(User user) throws Exception{
Document doc=getDocument(file);
Element root =doc.getRootElement();
List<Element> list=root.elements("user");
for (Element el: list) {
Element ur = (Element)el;
Element Base = el.element("BaseInfo");
Element Other = el.element("OtherInfo");

if (Integer.parseInt(Base.element("id").getTextTrim())==user.getId()) {
//更新该元素
Base.element("uname").setText(user.getUname());
Base.element("password").setText(user.getPassword());
Base.element("sex").setText(user.getSex());
Other.element("birthyear").setText(String.valueOf(user.getBirthYear()));
Other.element("birthmonth").setText(String.valueOf(user.getBirthMonth()));
Other.element("birthday").setText(String.valueOf(user.getBirthDay()));
Other.element("school").setText(String.valueOf(user.getSchool()));
Other.element("emailaddress").setText(String.valueOf(user.getEmailAddress()));
OutputFormat format=new OutputFormat();
     format.setEncoding("GBK");
     XMLWriter writer=new XMLWriter(new FileWriter(file),format);
     writer.write(doc);
writer.close();
return true;
}
}
return false;
}
/**
* 查询所有注册用户
*/
public List<User> getAllUsers() throws Exception{
List list = new ArrayList<User>();
Document doc=getDocument(file);
Element root =doc.getRootElement();
List<Element> list1=root.elements("user");
for (Element el: list1) {
User us = new User();
Element Base = el.element("BaseInfo");
us.setId(Integer.parseInt(Base.element("id").getText()));
us.setUname(Base.element("uname").getText());
us.setPassword(Base.element("password").getTextTrim());
us.setSex(Base.element("sex").getTextTrim());
Element Other = el.element("OtherInfo");
us.setBirthYear(Integer.parseInt(Other.element("birthyear").getTextTrim()));
us.setBirthMonth(Integer.parseInt(Other.element("birthmonth").getTextTrim()));
us.setBirthDay(Integer.parseInt(Other.element("birthday").getTextTrim()));
us.setSchool(Other.element("school").getText());
us.setEmailAddress(Other.element("emailaddress").getTextTrim());

list.add(us);
}
return list;
}
/**
* 注册用户登录
*/
public int UserLogin(User user) throws Exception{
int flag=0;
Document doc=getDocument(file);
Element root =doc.getRootElement();
List<Element> list=root.elements("user");
for (Element el: list) {
Element ur = (Element)el;
Element Base = el.element("BaseInfo");
if (Base.element("uname").getTextTrim().equals(user.getUname())) {
if(Base.element("password").getTextTrim().equals(user.getPassword())){
flag = Integer.parseInt(Base.element("id").getText());
return flag;
}
}
}
return flag;
}
}

你可能感兴趣的:(DAO,xml)