以如下格式的exam.xml文件为例
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <exam> <student idcard="111" examid="222"> <name>张三</name> <location>沈阳</location> <grade>89</grade> </student> <student idcard="333" examid="444"> <name>李四</name> <location>大连</location> <grade>97</grade> </student> </exam>
编程实现如下功能
添加用户的界面如下:
删除用户的界面如下:
查询用户的界面如下:
用JAXP DOM方式完成:
把功能分层设计,好处多多,分层图分析如下:
分层图功能说明:
增:用户通过界面输入学生数据,程序接收后通过JavaBean的设置方法封装成JavaBean对象,并传给DAO,DAO通过JavaBean的获取方法获取传过来的JavaBean学生信息,经过处理后写入XML;
查:用户通过界面输入要查询的学生姓名,并传给DAO,DAO通过XML获取符合要求的学生信息,并通过JavaBean的设置方法封装成JavaBean,再传给Main,然后Main通过JavaBean的获取方法取出学生信息。
删:用户通过界面输入要删除的学生的准考证号,并传给DAO,DAO删除XML中符合要求的学生
根据分层设计图创建如下包和类:
1、com.itheima.domain包: Student.java (JavaBean)
2、com.itheima.util包:JaxpUtil.java (getDocument()、write2xml()方法,用于获取DOM树对象和把DOM树对象写入XML文件)
3、com.itheima.dao包:StudentDaoImpl.java (学生DAO实现类,方法:addStudent() ,queryStudent(),deleteStudent(),即增、查、删三个方法)
4、com.itheima.test包:StudentDaoImplTest.java(用于临时测试StudentDaoImpl.java里面的增、查、删方法)
5、com.itheima.view包:Main.java(用于编写用户界面,接收用户信息)
具体代码如下:
1、com.itheima.domain包: Student.java
package com.itheima.domain; public class Student { private String idcard; private String examid; private String name; private String location; private float grade; public String getIdcard() { return idcard; } public void setIdcard(String idcard) { this.idcard = idcard; } public String getExamid() { return examid; } public void setExamid(String examid) { this.examid = examid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public float getGrade() { return grade; } public void setGrade(float grade) { this.grade = grade; } @Override public String toString() { return "Student [idcard=" + idcard + ", examid=" + examid + ", name=" + name + ", location=" + location + ", grade=" + grade + "]"; } }
2、com.itheima.util包:JaxpUtil.java
package com.itheima.util; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class JaxpUtil { public static Document getDocument() throws Exception{ DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = db.parse("src/exam.xml"); return document; } public static void write2xml(Document document) throws Exception { Transformer t = TransformerFactory.newInstance().newTransformer(); t.transform(new DOMSource(document), new StreamResult("src/exam.xml")); } }
3、com.itheima.dao包:StudentDaoImpl.java
package com.itheima.dao; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.itheima.domain.Student; import com.itheima.util.JaxpUtil; public class StudentDaoImpl { public static boolean addStudent(Student student){ boolean result = false; try{ //1、获取DOM树 Document document = JaxpUtil.getDocument(); //2、创建学生元素,并添加属性或主体内容 Element studentE = document.createElement("student"); studentE.setAttribute("idcard", student.getIdcard()); studentE.setAttribute("examid", student.getExamid()); Element nameE = document.createElement("name"); nameE.setTextContent(student.getName()); Element locationE = document.createElement("location"); locationE.setTextContent(student.getLocation()); Element gradeE = document.createElement("grade"); gradeE.setTextContent(student.getGrade()+""); //3、把学生元素添加到DOM Node examN = document.getElementsByTagName("exam").item(0); examN.appendChild(studentE); studentE.appendChild(nameE); studentE.appendChild(locationE); studentE.appendChild(gradeE); //4、把DOM树写回XML文档中 JaxpUtil.write2xml(document); result = true; }catch(Exception e){ throw new RuntimeException(e); } return result; } public static Student queryStudent(String examid){ Student student = null; //1、获得DOM树 Document document; try { document = JaxpUtil.getDocument(); } catch (Exception e) { throw new RuntimeException(e); } //2、获得所有student节点,遍历这些节点的examid属性, //以便找到要查找的examid,并获得这个属性的studetn节点 NodeList studentNodeList = document.getElementsByTagName("student"); Element studentE = null; for(int i=0;i<studentNodeList.getLength();i++){ studentE = (Element) studentNodeList.item(i); if(studentE.getAttribute("examid").equals(examid)) break;//此时的studentE就是要查找的目标 } //3、把打到的studentE的所有属性值/节点的主体内容封装成StudentBean并返回 student = new Student(); student.setExamid(studentE.getAttribute(examid)); student.setIdcard(studentE.getAttribute("idcard")); student.setName(studentE.getElementsByTagName("name").item(0).getTextContent()); student.setLocation(studentE.getElementsByTagName("location").item(0).getTextContent()); student.setGrade(Float.parseFloat(studentE.getElementsByTagName("grade").item(0).getTextContent())); return student; } public static boolean deleteStudent(String name){ boolean result = false; //1、获得DOM树 Document document; try { document = JaxpUtil.getDocument(); } catch (Exception e) { throw new RuntimeException(e); } //2、获得所有的name列表,并遍历找到需要的name节点并删除name的父节点(student) NodeList nameNodeList = document.getElementsByTagName("name"); for(int i=0;i<nameNodeList.getLength();i++){ if(nameNodeList.item(i).getTextContent().equals(name)){ nameNodeList.item(i).getParentNode().getParentNode(). removeChild(nameNodeList.item(i).getParentNode()); result = true; break; } } //3、把删除节点后的DOM树写回XML文档 try { JaxpUtil.write2xml(document); } catch (Exception e) { throw new RuntimeException(e); } return result; } }
4、com.itheima.test包:StudentDaoImplTest.java
package com.itheima.test; import com.itheima.dao.StudentDaoImpl; import com.itheima.domain.Student; public class StudentDaoImplTest { public static void main(String[] args) { /*Student student = new Student(); student.setIdcard("3838438"); student.setExamid("520"); student.setName("阿Q"); student.setLocation("地球"); student.setGrade(99.5f); StudentDaoImpl.addStudent(student);*//测试添加功能 Student student = StudentDaoImpl.queryStudent("444");//测试查询功能 if(!(student == null)) System.out.println(student.getName()); else System.out.println("没有这个人!"); StudentDaoImpl.deleteStudent("阿Q");//测试删除功能 } }
5、com.itheima.view包:Main.java
package com.itheima.view; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import com.itheima.dao.StudentDaoImpl; import com.itheima.domain.Student; public class Main { public static void main(String[] args) { System.out.println("a)添加用户:\tb)查询用户:" + "\tc)删除用户:\n请输入操作类型:"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { String line = reader.readLine(); if(line.equals("a")){ System.out.println("请输入学生姓名:"); String name = reader.readLine(); System.out.println("请输入学生准考证号:"); String examid = reader.readLine(); System.out.println("请输入学生身份证号:"); String idcard = reader.readLine(); System.out.println("请输入学生所在地:"); String location = reader.readLine(); System.out.println("请输入学生成绩:"); String grade = reader.readLine(); //添加学生JavaBean对象 Student student = new Student(); student.setName(name); student.setExamid(examid); student.setIdcard(idcard); student.setLocation(location); student.setGrade(Float.parseFloat(grade)); //将学生JavaBean对象信息添加到XML文件中 if(StudentDaoImpl.addStudent(student)) System.out.println("恭喜,添加成功!"); else System.out.println("对不起,服务器忙!"); }else if(line.equals("b")){ System.out.println("请输入查询的学生准考证号:"); String examid = reader.readLine(); Student student = StudentDaoImpl.queryStudent(examid); if(student != null) System.out.println("您查询学生的信息为:\n" + student); else System.out.println("对不起,服务器忙!"); }else if(line.endsWith("c")){ System.out.println("请输入要删除的学生姓名:"); String name = reader.readLine(); if(StudentDaoImpl.deleteStudent(name)) System.out.println("已成功删除学生信息!"); else System.out.println("您输入的学生不存在!"); }else System.out.println("输入错误,请重新输入"); } catch (IOException e) { System.out.println("不好意思,服务器忙!"); } } }