对XML文件进行的添加、删除、修改、查询操作。

【对XML文件进行的添加、删除、修改、查询操作。】整个类 和 xml 以及其他文件中在包中的位置如下图:本代码需要导入包jaxen-1.1-beta-6.jardom4j-1.6.1.jar
对XML文件进行的添加、删除、修改、查询操作。
文章图片

ui 表现层:StudentUI2 代码如下:

package test2.ui; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.PrintWriter; import java.util.Scanner; import test2.dao.StudentDao; import test2.domain.Student; //表现层 public class StudentUI2 { public static void main(String[] args) throws Exception { add(); } // 修改 public static void update() throws Exception { Scanner scanner = new Scanner(System.in); System.out.println("输入要修改的编号:"); String id = scanner.nextLine(); System.out.println("输入要修改后的姓名:"); String name = scanner.nextLine(); System.out.println("输入要修改的年龄:"); String age = scanner.nextLine(); Student stu = new Student(); stu.setId(id); stu.setAge(age); stu.setName(name); new StudentDao().update(stu); } //读取 public static void read() throws Exception { Scanner scanner = new Scanner(System.in); System.out.println("输入要查询的编号:"); String id = scanner.nextLine(); new StudentDao().read(id); } // 删除 public static void delete() throws Exception { Scanner scanner = new Scanner(System.in); System.out.println("输入要删除的编号:"); String id = scanner.nextLine(); new StudentDao().delete(id); } // 添加 public static void add() throws Exception { Scanner scanner = new Scanner(System.in); // 文件中存储的是当前添加对象的id值 File f = new File("src/test2/ui/id.txt"); BufferedReader bf = new BufferedReader(new FileReader(f)); String id = bf.readLine(); System.out.println("姓名:"); String name = scanner.nextLine(); System.out.println("年龄:"); String age = scanner.nextLine(); // 封装成java对象 Student student = new Student(); student.setId(id); student.setName(name); student.setAge(age); // 调用Dao对象 StudentDao studao = new StudentDao(); boolean flag = studao.create(student); if (flag) { System.out.println("操作成功!"); //更新存储id的文本的值 f.delete(); PrintWriter pw = new PrintWriter(f); pw.println(Integer.parseInt(id) + 1); pw.flush(); pw.close(); } else { System.out.println("操作失败!"); } } }


dao 访问层:StudentDao
package test2.dao; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import test2.domain.Student; //访问层 public class StudentDao { //修改元素 public void update(Student stu) throws Exception{ String xpath = "//student[@id='"+stu.getId()+"']"; Document doc = readxml(); //获取符合条件的元素 Element ele = (Element) doc.selectSingleNode(xpath); if(ele != null){ ele.element("name").setText(stu.getName()); ele.element("age").setText(stu.getAge()); write2XML(doc); }else{ System.out.println("不存在!"); } } //查询元素 public void read(String id) throws Exception{ String xpath = "//student[@id='"+id+"']"; Document doc = readxml(); Element ele = (Element) doc.selectSingleNode(xpath); if(ele != null){ System.out.println("编号:"+ele.attributeValue("id")); System.out.println("姓名:"+ele.elementText("name")); System.out.println("年龄:"+ele.elementText("age")); }else{ System.out.println("不存在!"); } } //删除元素 public void delete(String id) throws IOException, UnsupportedEncodingException, IOException{ String xpath = "//student[@id='"+id+"']"; Document doc = readxml(); Element ele = (Element) doc.selectSingleNode(xpath); if(ele != null){ //父节点删除子节点 ele.getParent().remove(ele); write2XML(doc); System.out.println("删除成功!"); }else{ System.out.println("不存在!"); } } //添加元素 public boolean create(Student student) throws Exception{ boolean flag = false; if(student !=null){ Document doc = readxml(); Element rootele = doc.getRootElement(); Element studentele = rootele.addElement("student"); studentele.addAttribute("id", student.getId()); studentele.addElement("name").setText(student.getName()); studentele.addElement("age").setText(student.getAge()); write2XML(doc); flag=true; } return flag; } //读取硬盘中的xml文件到内存中 private Document readxml() { SAXReader sax = new SAXReader(); Document doc = null; try { doc = sax.read(new File("src/test2/db/Student.xml")); } catch (DocumentException e) { doc = DocumentHelper.createDocument(); doc.addElement("student"); } return doc; } //将内存中的xml写到文本硬盘文本中 private void write2XML(Document doc) throws FileNotFoundException, UnsupportedEncodingException, IOException { OutputFormat opf = OutputFormat.createPrettyPrint(); OutputStream os = new FileOutputStream("src/test2/db/Student.xml"); XMLWriter xmlwriter = new XMLWriter(os,opf); xmlwriter.write(doc); xmlwriter.close(); } }


Student.xml数据层:
jerry 21herry 21andy 20lulu 21jerry 22


javabean student.java
package test2.domain; public class Student { private String name; private String id; private String age; public Student(){} public String getName() { return name; } public void setName(String name) { if(name.matches("[a-zA-Z]+")){ this.name = name; }else{ throw new RuntimeException("姓名"); } } public String getId() { return id; } public void setId(String id) { if(id.matches("[0-9]+")){ this.id = id; }else{ throw new RuntimeException("编号"); } } public String getAge() { return age; } public void setAge(String age) { if(age.matches("[1-9][0-9]")){ this.age = age; }else{ throw new RuntimeException("年龄"); } }; }


文本文件id.txt 6

    推荐阅读