實現簡單的學生管理系統java
使用xml當作數據,存儲學生信息node
** 建立一個xml文件,寫一些學生信息dom
** 增長操做
一、建立解析器
二、獲得documentide
三、獲取到根節點
四、在根節點上面建立stu標籤
五、在stu標籤上面依次添加id name age
** addElement方法添加
六、在id name age上面依次添加值
** setText方法
七、回寫xml函數
** 刪除操做(根據id刪除)
一、建立解析器
二、獲得document
三、獲取到全部的id
使用xpath //id 返回 list集合
四、遍歷list集合
五、判斷集合裏面的id和傳遞的id是否相同
六、若是相同,把id所在的stu刪除工具
** 查詢操做(根據id查詢)
一、建立解析器
二、獲得document
三、獲取到全部的id
四、返回的是list集合,遍歷list集合
五、獲得每個id的節點
六、id節點的值
七、判斷id的值和傳遞的id值是否相同
八、若是相同,先獲取到id的父節點stu
九、經過stu獲取到name age值測試
** 把這些值封裝到一個對象裏面 返回對象
stuService:主要類,用於操做學生基本信息類[添加、刪除、查詢]
Student:封裝了學生基本信息
testStu:主函數,測試類
StudentUtils:封裝的工具類[dom4j解析器,回寫xml文件方法]
student.xml:數據的交換
導入:dom4j和XPath架包
總體結構:this
e-code【stuService】
spa
1 package boom.service; 2 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.OutputStream; 6 import java.nio.file.Path; 7 import java.text.Format; 8 import java.util.List; 9 10 import org.dom4j.Document; 11 import org.dom4j.DocumentException; 12 import org.dom4j.Element; 13 import org.dom4j.Node; 14 import org.dom4j.io.OutputFormat; 15 import org.dom4j.io.SAXReader; 16 import org.dom4j.io.XMLWriter; 17 18 import boom.student.Student; 19 import boom.utils.StudentUtils; 20 21 /*** 22 * 操做學生基本信息類 23 * @author Administrator 24 * 25 */ 26 public class stuService { 27 /** 28 * 添加 29 * @param student 30 * @throws Exception 31 */ 32 public static void addStu(Student student) throws Exception{ 33 /*// 建立解析器 34 SAXReader saxReader = new SAXReader(); 35 // 獲得document 36 Document document = saxReader.read("src/student.xml");*/ 37 Document document = StudentUtils.getDocument(StudentUtils.PATH); 38 39 // 獲得根節點 40 Element root = document.getRootElement(); 41 // 在根節點上建立stu標籤 42 Element stu = root.addElement("stu"); 43 // 在stu標籤上建立學生屬性 44 Element id1 = stu.addElement("id"); 45 Element name1 = stu.addElement("name"); 46 Element age1 = stu.addElement("age"); 47 // 在id name age屬性上設置屬性值 48 id1.setText(student.getId()); 49 name1.setText(student.getName()); 50 age1.setText(student.getAge()); 51 // 回寫xml文件 52 OutputFormat format = OutputFormat.createPrettyPrint(); 53 XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"), format); 54 xmlWriter.write(document); 55 xmlWriter.close(); 56 } 57 /** 58 * 刪除學生(根據id刪除) 59 * @param student 60 * @throws Exception 61 */ 62 public static void delStu(String id) throws Exception{ 63 /*// 建立解析器 64 SAXReader saxReader = new SAXReader(); 65 // 獲得document 66 Document document = saxReader.read("src/student.xml");*/ 67 Document document = StudentUtils.getDocument(StudentUtils.PATH); 68 69 // 獲得全部的id(XPath去獲取) 70 List<Node> list = document.selectNodes("//id"); 71 // 遍歷list集合 72 for (Node node : list) {// node是每一個id的元素 73 // 獲得ID的值 74 String ID = node.getText(); 75 // 判斷ID是否與傳遞進來的id值是否 76 if(ID.equals(id)) { // id相同 77 // 獲得stu的父節點 78 Element stu = node.getParent(); 79 // 獲取stu父節點(獲得student根節點) 80 Element student = stu.getParent(); 81 student.remove(stu); 82 } 83 } 84 // 回寫xml文件 85 /*OutputFormat format = OutputFormat.createPrettyPrint(); 86 XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"), format); 87 xmlWriter.write(document); 88 xmlWriter.close();*/ 89 StudentUtils.xmlWriter(StudentUtils.PATH,document); 90 } 91 /** 92 * 查詢學生(根據id) 93 * @param id 94 * @throws Exception 95 */ 96 public static Student getStu(String id) throws Exception{ 97 // 建立解析器並獲得document 98 Document document = StudentUtils.getDocument(StudentUtils.PATH); 99 // 獲取全部的id 100 List<Node> list = document.selectNodes("//id"); 101 // 建立student對象 102 Student student = new Student(); 103 // 遍歷list 104 for (Node node : list) { 105 // 的到id節點的值 106 String ID = node.getText(); 107 // 判斷id是否相同 108 if(ID.equals(id)) { 109 // 獲得id的父節點stu 110 Element stu = node.getParent(); 111 // 經過stu獲取到name age值 112 String name = stu.element("name").getText(); 113 String age = stu.element("age").getText(); 114 // 對象裏設置值 115 student.setId(id); 116 student.setName(name); 117 student.setAge(age); 118 } 119 } 120 // 返回 121 return student; 122 } 123 124 }
e-code【Student】
code
1 package boom.student; 2 /** 3 * 封裝學生基本信息 4 * @author Administrator 5 * 6 */ 7 public class Student { 8 private String id; 9 private String name; 10 private String age; 11 12 // 生成get set方法 13 public String getId() { 14 return id; 15 } 16 public void setId(String id) { 17 this.id = id; 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 public String getAge() { 26 return age; 27 } 28 public void setAge(String age) { 29 this.age = age; 30 } 31 @Override 32 public String toString() { 33 return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; 34 } 35 36 }
e-code【TestStu】
1 package boom.test; 2 3 import boom.service.stuService; 4 import boom.student.Student; 5 6 public class TestStu { 7 8 /** 9 * 測試類 10 * @param args 11 * @throws Exception 12 */ 13 public static void main(String[] args) throws Exception { 14 // testAdd(); 15 // testDel(); 16 testSelect(); 17 } 18 /** 19 * 查詢方法 20 * @throws Exception 21 */ 22 public static void testSelect() throws Exception{ 23 Student stu = stuService.getStu("2015112402"); 24 System.out.println(stu.toString()); 25 } 26 /** 27 * 刪除方法 28 * @throws Exception 29 */ 30 public static void testDel() throws Exception{ 31 stuService.delStu("2015112403"); 32 33 } 34 /** 35 * 添加方法 36 * @throws Exception 37 */ 38 public static void testAdd() throws Exception{ 39 // 建立學生對象並設置值 40 Student stu = new Student();// 對象被封裝在Student類裏 41 stu.setId("2015112403"); 42 stu.setName("大兄逮"); 43 stu.setAge("18"); 44 // 調用操做學生類[stuService] 45 stuService.addStu(stu); 46 } 47 48 }
e-code【StudentUtils】
1 package boom.utils; 2 3 import java.io.FileOutputStream; 4 5 import org.dom4j.Document; 6 import org.dom4j.io.OutputFormat; 7 import org.dom4j.io.SAXReader; 8 import org.dom4j.io.XMLWriter; 9 10 public class StudentUtils { 11 public static final String PATH = "src/student.xml"; 12 13 // 返回document 14 public static Document getDocument(String path) { 15 try { 16 // 建立解析器 17 SAXReader reader = new SAXReader(); 18 // 獲得document 19 Document document = reader.read(path); 20 return document; 21 } catch (Exception e) { 22 e.printStackTrace(); 23 } 24 return null; 25 } 26 27 // 回寫xml的方法 28 public static void xmlWriter(String path, Document document) { 29 try { 30 OutputFormat format = OutputFormat.createPrettyPrint(); 31 XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(path),format); 32 xmlWriter.write(document); 33 xmlWriter.close(); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } 37 } 38 }
student,xml