XML基础综合案例【三】

实现简单的学生管理系统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 }
View Code

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 }
View Code

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 }
View Code

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 }
View Code

student,xml

相关文章
相关标签/搜索