XML基础综合案例【三】

2023-10-15,,

实现简单的学生管理系统

使用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】

 package boom.service;

 import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.text.Format;
import java.util.List; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter; import boom.student.Student;
import boom.utils.StudentUtils; /***
* 操作学生基本信息类
* @author Administrator
*
*/
public class stuService {
/**
* 添加
* @param student
* @throws Exception
*/
public static void addStu(Student student) throws Exception{
/*// 创建解析器
SAXReader saxReader = new SAXReader();
// 得到document
Document document = saxReader.read("src/student.xml");*/
Document document = StudentUtils.getDocument(StudentUtils.PATH); // 得到根节点
Element root = document.getRootElement();
// 在根节点上创建stu标签
Element stu = root.addElement("stu");
// 在stu标签上创建学生属性
Element id1 = stu.addElement("id");
Element name1 = stu.addElement("name");
Element age1 = stu.addElement("age");
// 在id name age属性上设置属性值
id1.setText(student.getId());
name1.setText(student.getName());
age1.setText(student.getAge());
// 回写xml文件
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"), format);
xmlWriter.write(document);
xmlWriter.close();
}
/**
* 删除学生(根据id删除)
* @param student
* @throws Exception
*/
public static void delStu(String id) throws Exception{
/*// 创建解析器
SAXReader saxReader = new SAXReader();
// 得到document
Document document = saxReader.read("src/student.xml");*/
Document document = StudentUtils.getDocument(StudentUtils.PATH); // 得到所有的id(XPath去获取)
List<Node> list = document.selectNodes("//id");
// 遍历list集合
for (Node node : list) {// node是每个id的元素
// 得到ID的值
String ID = node.getText();
// 判断ID是否与传递进来的id值是否
if(ID.equals(id)) { // id相同
// 得到stu的父节点
Element stu = node.getParent();
// 获取stu父节点(得到student根节点)
Element student = stu.getParent();
student.remove(stu);
}
}
// 回写xml文件
/*OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"), format);
xmlWriter.write(document);
xmlWriter.close();*/
StudentUtils.xmlWriter(StudentUtils.PATH,document);
}
/**
* 查询学生(根据id)
* @param id
* @throws Exception
*/
public static Student getStu(String id) throws Exception{
// 创建解析器并得到document
Document document = StudentUtils.getDocument(StudentUtils.PATH);
// 获取所有的id
List<Node> list = document.selectNodes("//id");
// 创建student对象
Student student = new Student();
// 遍历list
for (Node node : list) {
// 的到id节点的值
String ID = node.getText();
// 判断id是否相同
if(ID.equals(id)) {
// 得到id的父节点stu
Element stu = node.getParent();
// 通过stu获取到name age值
String name = stu.element("name").getText();
String age = stu.element("age").getText();
// 对象里设置值
student.setId(id);
student.setName(name);
student.setAge(age);
}
}
// 返回
return student;
} }

e-code【Student】

 package boom.student;
/**
* 封装学生基本信息
* @author Administrator
*
*/
public class Student {
private String id;
private String name;
private String age; // 生成get set方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
} }

e-code【TestStu】

 package boom.test;

 import boom.service.stuService;
import boom.student.Student; public class TestStu { /**
* 测试类
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// testAdd();
// testDel();
testSelect();
}
/**
* 查询方法
* @throws Exception
*/
public static void testSelect() throws Exception{
Student stu = stuService.getStu("2015112402");
System.out.println(stu.toString());
}
/**
* 删除方法
* @throws Exception
*/
public static void testDel() throws Exception{
stuService.delStu("2015112403"); }
/**
* 添加方法
* @throws Exception
*/
public static void testAdd() throws Exception{
// 创建学生对象并设置值
Student stu = new Student();// 对象被封装在Student类里
stu.setId("2015112403");
stu.setName("大兄逮");
stu.setAge("18");
// 调用操作学生类[stuService]
stuService.addStu(stu);
} }

e-code【StudentUtils】

 package boom.utils;

 import java.io.FileOutputStream;

 import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter; public class StudentUtils {
public static final String PATH = "src/student.xml"; // 返回document
public static Document getDocument(String path) {
try {
// 创建解析器
SAXReader reader = new SAXReader();
// 得到document
Document document = reader.read(path);
return document;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} // 回写xml的方法
public static void xmlWriter(String path, Document document) {
try {
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(path),format);
xmlWriter.write(document);
xmlWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

student,xml

XML基础综合案例【三】的相关教程结束。

《XML基础综合案例【三】.doc》

下载本文的Word格式文档,以方便收藏与打印。