zoukankan      html  css  js  c++  java
  • XML基础综合案例【三】

    实现简单的学生管理系统

    使用xml当做数据,存储学生信息

    ** 创建一个xml文件,写一些学生信息

    ** 增加操作
    1、创建解析器
    2、得到document

    3、获取到根节点
    4、在根节点上面创建stu标签
    5、在stu标签上面依次添加id name age
      ** addElement方法添加
    6、在id name age上面依次添加值
      ** setText方法
    7、回写xml

    ** 删除操作(根据id删除)
    1、创建解析器
    2、得到document
    3、获取到所有的id
    使用xpath //id 返回 list集合
    4、遍历list集合
    5、判断集合里面的id和传递的id是否相同
    6、如果相同,把id所在的stu删除

    ** 查询操作(根据id查询)
    1、创建解析器
    2、得到document
    3、获取到所有的id
    4、返回的是list集合,遍历list集合
    5、得到每一个id的节点
    6、id节点的值
    7、判断id的值和传递的id值是否相同
    8、如果相同,先获取到id的父节点stu
    9、通过stu获取到name age值

      ** 把这些值封装到一个对象里面 返回对象

    stuService:主要类,用于操作学生基本信息类[添加、删除、查询]
    Student:封装了学生基本信息
    testStu:主函数,测试类
    StudentUtils:封装的工具类[dom4j解析器,回写xml文件方法]
    student.xml:数据的交换
    导入:dom4j和XPath架包

    整体结构:

    e-code【stuService】

      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】

     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




  • 相关阅读:
    使用网络服务
    Linux Socket 学习(九)
    Linux Socket学习(六)
    Linux Socket学习(八)
    Developing Software in Visual Studio .NET with NonAdministrative Privileges
    html5+css3实现一款注册表单
    linux编程下signal()函数
    深入理解Oracle索引(10):索引列字符类型统计信息的32位限制
    智能手机屏幕清晰度用户体现的分析:PPI与PPI2
    架设邮件服务器windows 2003 POP3服务,SMTP服务收发邮件
  • 原文地址:https://www.cnblogs.com/cao-yin/p/9334317.html
Copyright © 2011-2022 走看看