java对xml文档的增删改查

package cn.csdn.dao;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

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

import cn.csdn.domain.Sex;
import cn.csdn.domain.Student;

public class StudentDaoImpl implements StudentDao {

private String url = Student.class.getClassLoader().getResource("")
   .getPath().replace("%20", " ");
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");

public boolean insert(Student entity) {
  boolean flag = false;
  try {
   // 创建解析器
   SAXReader saxReader = new SAXReader();
   // 加载文件并解析
   Document doc = saxReader.read(new File(url + "student.xml"));
   // 得到根节点元素
   Element root = doc.getRootElement();
   // 创建根节点的子元素并添加到根元素里
   Element student = root.addElement("student");
   // 创建子元素的子元素并添加
   student.addElement("name").setText(entity.getName());
   student.addElement("age").setText(entity.getAge() + "");
   student.addElement("sex").setText(entity.getSex().getValue());
   student.addElement("birthday").setText(
     simpleDate.format(entity.getBirthday()));
   student.addElement("email").setText(entity.getEmail());
   student.addElement("address").setText(entity.getAddress());

   // 定义格式
   OutputFormat outFormat = new OutputFormat("   ", true);
   // 获取写入流对象并写入文件
   XMLWriter xmlWriter = new XMLWriter(new FileWriter(url
     + "student.xml"), outFormat);
   // 将doc对象的属性写入文件
   xmlWriter.write(doc);
   // 关闭流操作
   xmlWriter.close();
   flag = true;
  } catch (Exception e) {
   e.printStackTrace();
  }
  return flag;
}

public boolean delete(String name) {
  boolean falg = false;
  try {
   SAXReader saxReader = new SAXReader();
   Document doc = saxReader.read(new File(url + "student.xml"));
   Element root = doc.getRootElement();
   @SuppressWarnings("unchecked")
   List<Element> list = root.elements();
   for (int i = 0; i < list.size(); i++) {
    Element stu = list.get(i);
    if (stu.element("name").getText().equals(name)) {
     stu.getParent().remove(stu);
     falg = true;
    }
   }
   // 定义格式
   OutputFormat outFormat = new OutputFormat("   ", true);
   // 获取写入流对象并写入文件
   XMLWriter xmlWriter = new XMLWriter(new FileWriter(url
     + "student.xml"), outFormat);
   // 将doc对象的属性写入文件
   xmlWriter.write(doc);
   // 关闭流操作
   xmlWriter.close();

  } catch (Exception e) {
   e.printStackTrace();
  }

  return falg;
}

public boolean update(Student entity) {
  boolean flag = false;
  try {
   // 创建解析器
   SAXReader saxReader = new SAXReader();
   // 加载文件并解析
   Document doc = saxReader.read(new File(url + "student.xml"));
   // 得到根节点元素
   Element root = doc.getRootElement();
   // 创建根节点的子元素并添加到根元素里
   @SuppressWarnings("unchecked")
   List<Element> list = root.elements();
   Element student = list.get(entity.getPos());
   // 创建子元素的子元素并添加
   student.element("name").setText(entity.getName());
   student.element("age").setText(entity.getAge() + "");
   student.element("sex").setText(entity.getSex().getValue());
   student.element("birthday").setText(
     simpleDate.format(entity.getBirthday()));
   student.element("email").setText(entity.getEmail());
   student.element("address").setText(entity.getAddress());

   // 定义格式
   OutputFormat outFormat = new OutputFormat("   ", true);
   // 获取写入流对象并写入文件
   XMLWriter xmlWriter = new XMLWriter(new FileWriter(url
     + "student.xml"), outFormat);
   // 将doc对象的属性写入文件
   xmlWriter.write(doc);
   // 关闭流操作
   xmlWriter.close();
   flag = true;
  } catch (Exception e) {
   e.printStackTrace();
  }
  return flag;
}

public Student select(String name) {
  Student student = null;
  try {
   // 创建解析器
   SAXReader sax = new SAXReader();
   // 装载并解析文件
   Document doc = sax.read(new File(url + "student.xml"));
   // 获取根元素名称
   Element root = doc.getRootElement();
   @SuppressWarnings("unchecked")
   List<Element> list = root.elements();
   for (int i = 0; i < list.size(); i++) {
    Element stu = list.get(i);
    if (stu.element("name").getText().equals(name)) {
     student = new Student();
     student.setPos(i);
     student.setName(stu.element("name").getText());
     if (stu.element("sex").getText().equals("男")) {
      student.setSex(Sex.MALE);
     } else {
      student.setSex(Sex.FEMALE);
     }
     student.setAge(Integer
       .valueOf(stu.element("age").getText()));
     student.setEmail(stu.element("email").getText());
     student.setAddress(stu.element("address").getText());
     student.setBirthday(simpleDate.parse(stu
       .element("birthday").getText()));
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return student;
}

public List<Student> findAll() {
  List<Student> allentites = new ArrayList<Student>();
  try {
   // 创建解析器
   SAXReader sax = new SAXReader();
   // 装载并解析文件
   Document doc = sax.read(new File(url + "student.xml"));
   // 获取根元素名称
   Element root = doc.getRootElement();
   @SuppressWarnings("unchecked")
   List<Element> list = root.elements();
   for (int i = 0; i < list.size(); i++) {
    Element stu = list.get(i);
    Student student = new Student();
    student.setPos(i);
    student.setName(stu.element("name").getText());
    if (stu.element("sex").getText().equals("男")) {
     student.setSex(Sex.MALE);
    } else {
     student.setSex(Sex.FEMALE);
    }
    student.setAge(Integer.valueOf(stu.element("age").getText()));
    student.setEmail(stu.element("email").getText());
    student.setAddress(stu.element("address").getText());
    student.setBirthday(simpleDate.parse(stu.element("birthday")
      .getText()));
    allentites.add(student);
   }

  } catch (Exception e) {
   e.printStackTrace();
  }
  return allentites;
}

public boolean delete(Student entity) {
  boolean falg = false;
  try {
   SAXReader saxReader = new SAXReader();
   Document doc = saxReader.read(new File(url + "student.xml"));
   Element root = doc.getRootElement();
   @SuppressWarnings("unchecked")
   List<Element> list = root.elements();
   for (int i = 0; i < list.size(); i++) {
    Element stu = list.get(i).element("name");
    if (stu.getText().equals(entity.getName())) {
     System.out.println(stu.getText() + "---------" + url);
     stu.getParent().getParent().remove(stu.getParent());
     falg = true;
    }
   }
   // 定义格式
   OutputFormat outFormat = new OutputFormat("   ", true);
   // 获取写入流对象并写入文件
   XMLWriter xmlWriter = new XMLWriter(new FileWriter(url
     + "student.xml"), outFormat);
   // 将doc对象的属性写入文件
   xmlWriter.write(doc);
   // 关闭流操作
   xmlWriter.close();

  } catch (Exception e) {
   e.printStackTrace();
  }

  return falg;
}

}
如有疑问,请留言!

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