标签:
一: 需求描述
学生成绩管理系统,使用xml存储学生信息,可以对学生信息进行增、删、删除操作。
主要目的:练习操作xml元素的增删改查
二:代码结构
1:xml存储数据如下
exam.xml
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 2 <students> 3 <student> 4 <name sid="111">李四</name> 5 <age>23</age> 6 <gender>男</gender> 7 </student> 8 <student> 9 <name sid="222">张三</name> 10 <age>21</age> 11 <gender>女</gender> 12 </student> 13 </students>
2:Student类封装学生信息
1 /** 2 * 3 */ 4 package com.hlcui.entity; 5 6 /** 7 * @author Administrator 8 * 9 */ 10 public class Student { 11 private int id; 12 private String name; 13 private int age; 14 private String gender; 15 16 public int getId() { 17 return id; 18 } 19 20 public void setId(int id) { 21 this.id = id; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 32 public int getAge() { 33 return age; 34 } 35 36 public void setAge(int age) { 37 this.age = age; 38 } 39 40 public String getGender() { 41 return gender; 42 } 43 44 public void setGender(String gender) { 45 this.gender = gender; 46 } 47 48 }
3:数据访问层,封装操作xml数据的方法
1 /** 2 * 3 */ 4 package com.hlcui.dao; 5 6 import org.w3c.dom.Document; 7 import org.w3c.dom.Element; 8 import org.w3c.dom.NodeList; 9 10 import com.hlcui.entity.Student; 11 import com.hlcui.exception.NameNotFoundException; 12 import com.hlcui.utils.StudentUtils; 13 14 /** 15 * @author Administrator 16 * 17 */ 18 public class StudentDAO { 19 // 添加学生信息 20 public void add(Student stu) { 21 try { 22 Document doc = StudentUtils.getDocument(); 23 Element student = doc.createElement("student"); 24 student.setAttribute("id", stu.getId() + ""); // 给学生元素添加属性 25 26 // 分别创建姓名、年龄、性别节点 27 Element name = doc.createElement("name"); 28 name.setTextContent(stu.getName()); 29 Element age = doc.createElement("age"); 30 age.setTextContent(stu.getAge() + ""); 31 Element gender = doc.createElement("gender"); 32 gender.setTextContent(stu.getGender()); 33 34 // 将姓名、年龄、性别节点添加到学生节点上 35 student.appendChild(name); 36 student.appendChild(age); 37 student.appendChild(gender); 38 39 // 在将student节点添加到students节点上 40 doc.getElementsByTagName("students").item(0).appendChild(student); 41 StudentUtils.write2XML(doc); 42 System.out.println("添加成功!"); 43 } catch (Exception e) { 44 e.printStackTrace(); 45 } 46 } 47 48 // 根据学生姓名查询学生信息 49 public Student find(String name) throws NameNotFoundException { 50 try { 51 Document doc = StudentUtils.getDocument(); 52 NodeList list = doc.getElementsByTagName("student"); 53 for (int i = 0; i < list.getLength(); i++) { 54 Element ele = (Element) list.item(i); 55 if (name.equals((ele.getElementsByTagName("name")).item(0) 56 .getTextContent())) { 57 Student stu = new Student(); 58 stu.setId(Integer.parseInt(ele.getAttribute("id"))); 59 stu.setName(name); 60 stu.setAge(Integer.parseInt((ele 61 .getElementsByTagName("age")).item(0) 62 .getTextContent())); 63 stu.setGender(ele.getElementsByTagName("gender").item(0) 64 .getTextContent()); 65 return stu; 66 } 67 } 68 throw new NameNotFoundException(name + "不存在!!!"); 69 } catch (NameNotFoundException e) { 70 throw e; 71 } catch (Exception e) { 72 e.printStackTrace(); 73 } 74 return null; 75 } 76 77 // 根据id删除学生信息 78 public boolean delete(int id) { 79 try { 80 Document doc = StudentUtils.getDocument(); 81 NodeList list = doc.getElementsByTagName("student"); 82 for (int i = 0; i < list.getLength(); i++) { 83 Element ele = (Element) list.item(i); 84 if (String.valueOf(id).equals(ele.getAttribute("id"))) { 85 ele.getParentNode().removeChild(ele); 86 StudentUtils.write2XML(doc); 87 return true; 88 } 89 } 90 } catch (Exception e) { 91 e.printStackTrace(); 92 } 93 return false; 94 } 95 }
4:自定义异常类,封装异常
1 /** 2 * 3 */ 4 package com.hlcui.exception; 5 6 /** 7 * @author Administrator 8 * 9 */ 10 public class NameNotFoundException extends Exception { 11 12 /** 13 * 14 */ 15 private static final long serialVersionUID = 1L; 16 17 /** 18 * 19 */ 20 public NameNotFoundException() { 21 } 22 23 /** 24 * @param message 25 */ 26 public NameNotFoundException(String message) { 27 super(message); 28 } 29 30 /** 31 * @param cause 32 */ 33 public NameNotFoundException(Throwable cause) { 34 super(cause); 35 } 36 37 /** 38 * @param message 39 * @param cause 40 */ 41 public NameNotFoundException(String message, Throwable cause) { 42 super(message, cause); 43 } 44 45 }
5:junit框架测试DAO方法
1 /** 2 * 3 */ 4 package com.hlcui.test; 5 6 import org.junit.Test; 7 8 import com.hlcui.dao.StudentDAO; 9 import com.hlcui.entity.Student; 10 import com.hlcui.exception.NameNotFoundException; 11 12 /** 13 * @author Administrator 14 * 15 */ 16 public class TestStudentDAO { 17 private StudentDAO dao = new StudentDAO(); 18 19 @Test 20 public void testAdd() { 21 Student stu = new Student(); 22 stu.setId(333); 23 stu.setName("王二"); 24 stu.setAge(27); 25 stu.setGender("男"); 26 dao.add(stu); 27 } 28 29 @Test 30 public void testFind() throws NameNotFoundException { 31 String name = "王二"; 32 Student stu = dao.find(name); 33 System.out.println("学号:" + stu.getId() + "\n姓名:" + stu.getName() 34 + "\n年龄:" + stu.getAge() + "\n性别:" + stu.getGender()); 35 36 } 37 38 @Test 39 public void testDelete() { 40 int id = 333; 41 boolean flag = dao.delete(id); 42 System.out.println(flag ? "删除成功!!" : "删除失败!!"); 43 } 44 }
6:封装操作dom文件功能方法
1 /** 2 * 3 */ 4 package com.hlcui.utils; 5 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 import javax.xml.parsers.DocumentBuilder; 10 import javax.xml.parsers.DocumentBuilderFactory; 11 import javax.xml.parsers.ParserConfigurationException; 12 import javax.xml.transform.Transformer; 13 import javax.xml.transform.TransformerConfigurationException; 14 import javax.xml.transform.TransformerFactory; 15 import javax.xml.transform.dom.DOMSource; 16 import javax.xml.transform.stream.StreamResult; 17 18 import org.w3c.dom.Document; 19 20 /** 21 * @author Administrator 22 * 23 */ 24 public class StudentUtils { 25 26 /* 获取Document对象* */ 27 public static Document getDocument() throws Exception { 28 DocumentBuilderFactory sfb = DocumentBuilderFactory.newInstance(); 29 DocumentBuilder db = sfb.newDocumentBuilder(); 30 Document doc = db.parse("xml/exam.xml"); 31 return doc; 32 } 33 34 /* 将内存中的内容写到硬盘* */ 35 public static void write2XML(Document doc) throws Exception { 36 TransformerFactory factory = TransformerFactory.newInstance(); 37 Transformer tf = factory.newTransformer(); 38 tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream( 39 "xml/exam.xml"))); 40 } 41 }
7:用户交互界面
1 /** 2 * 3 */ 4 package com.hlcui.ui; 5 6 import java.io.BufferedReader; 7 import java.io.IOException; 8 import java.io.InputStreamReader; 9 10 import com.hlcui.dao.StudentDAO; 11 import com.hlcui.entity.Student; 12 import com.hlcui.exception.NameNotFoundException; 13 14 /** 15 * @author Administrator 用户交互界面 16 */ 17 public class Main { 18 19 public static StudentDAO dao = new StudentDAO(); 20 21 /** 22 * 23 * @param args 24 */ 25 public static void main(String[] args) { 26 27 System.out.println("请选择操作模式:a:添加用户 b:查找用户 c:删除用户 exit:退出系统"); 28 BufferedReader br = null; 29 try { 30 while (true) { 31 br = new BufferedReader(new InputStreamReader(System.in)); 32 String type = br.readLine(); 33 if ("a".equals(type)) { 34 35 // 录入数据 36 System.out.println("请输入学号:"); 37 int id = Integer.parseInt(br.readLine()); 38 System.out.println("请输入姓名:"); 39 String name = br.readLine(); 40 System.out.println("请输入年龄:"); 41 int age = Integer.parseInt(br.readLine()); 42 System.out.println("请输入性别:"); 43 String gender = br.readLine(); 44 // 封装数据 45 Student stu = new Student(); 46 stu.setId(id); 47 stu.setName(name); 48 stu.setAge(age); 49 stu.setGender(gender); 50 51 // 插入数据 52 dao.add(stu); 53 } else if ("b".equals(type)) { 54 System.out.println("请输入姓名:"); 55 String name = br.readLine(); 56 try { 57 Student stu = dao.find(name); 58 System.out.println("***********学生信息如下***********"); 59 System.out.println("学号:" + stu.getId() + "\n姓名:" 60 + stu.getName() + "\n年龄:" + stu.getAge() 61 + "\n性别:" + stu.getGender()); 62 } catch (NameNotFoundException e) { 63 System.out.println(e.getMessage()); 64 } 65 } else if ("c".equals(type)) { 66 System.out.println("请输入学号:"); 67 int id = Integer.parseInt(br.readLine()); 68 boolean flag = dao.delete(id); 69 if (flag) { 70 System.out.println("删除成功!"); 71 } else { 72 System.out.println("删除失败!"); 73 } 74 } else if ("exit".equals(type.toLowerCase())) { 75 System.out.println("系统正在退出..."); 76 try { 77 Thread.sleep(3000); 78 } catch (InterruptedException e) { 79 e.printStackTrace(); 80 } 81 break; 82 } else { 83 System.out.println("您的操作暂不支持,请重新输入:"); 84 } 85 } 86 87 } catch (IOException e) { 88 e.printStackTrace(); 89 } finally { 90 if (null != br) { 91 try { 92 br.close(); 93 } catch (IOException e) { 94 e.printStackTrace(); 95 } 96 } 97 } 98 } 99 }
代码均已经验证正确!
标签:
原文地址:http://www.cnblogs.com/warrior4236/p/5860508.html