77、(实例)学生管理系统

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

1、创建student.xml文件,写一些学生信息

student.xml:

<?xml version="1.0" encoding="UTF-8"?>

<student> 
  <stu> 
    <id>100</id>  
    <name>zhangsan</name>  
    <age>20</age> 
  </stu>  
  <stu> 
    <id>101</id>  
    <name>lisi</name>  
    <age>22</age> 
  </stu> 
</student>

2、增加操作
  1. 创建解析器
  2. 得到document
  3. 获取根节点
  4. 在根节点上创建stu标签
  5. 在stu标签上依次添加id,name,age(addElement方法)
  6. 在id,name,age上依次添加值(setText方法)
  7. 回写xml
3、删除操作
  1. 创建解析器
  2. 得到document
  3. 获取所有的id
  4. 遍历list集合
  5. 判断list里的id和传递的id是否相同
  6. 如果相同,把id所在的stu删除
4、查询操作
  1. 创建解析器
  2. 得到document
  3. 获取所有的id
  4. 返回list,遍历list
  5. 得到每一个id
  6. 获取id节点的值
  7. 判断list里的id和传递的id是否相同
  8. 如果相同,先获取id的父节点stu
  9. 通过stu获取name,age的值
    10.把这些值封装到一个对象里面,返回对象

代码:

在这里插入图片描述
1、student.java:

package cn.itcast.vo;

public class Student {
	private String name;
	private String id;
	private String age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	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+"]";
	}
}

2、StuService.java:

package cn.itcast.service;
import java.io.FileOutputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import cn.itcast.vo.Student;

public class StuService {
	//增加
	public static void addStu(Student student) throws Exception{
		//创建解析器
		SAXReader saxReader = new SAXReader();
		//得到document
		Document document = saxReader.read("src/student.xml");
		//得到根节点
		Element root = document.getRootElement();
		//在根节点上添加stu
		Element stu = root.addElement("stu");
		//在stu标签上依次添加id,name,age标签
		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删除学生
	public static void delStu(String id) throws Exception{
		//创建解析器
		SAXReader saxReader = new SAXReader();
		//得到document
		Document document = saxReader.read("src/student.xml");
		//得到根节点
		Element root = document.getRootElement();
		//获取id
		Node node_id = root.selectSingleNode("//id[text()='"+id+"']");
		//获取id的父节点stu
		Element stu = node_id.getParent();
		//得到stu的父节点student
		Element student = stu.getParent();
		//删除stu
		student.remove(stu);
		//回写xml
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"),format);
		xmlWriter.write(document);
		xmlWriter.close();
	}
	
	//根据id查询学生信息
	public static Student getStu(String id) throws Exception{
		//创建解析器
		SAXReader saxReader = new SAXReader();
		//得到document
		Document document = saxReader.read("src/student.xml");
		//获取所有的id
		List<Node> list = document.selectNodes("//id");
		//创建student对象
		Student student = new Student();
		//返回list集合,遍历list
		for(Node node:list){
			//得到id节点的值
			String idv = node.getText();
			//判断id和传过来的id是否相同
			if(idv.equals(id)){
				//得到id的父节点stu
				Element stu = node.getParent();
				//通过stu获取name和age
				String namev = stu.element("name").getText();
				String agev = stu.element("age").getText();
				student.setId(idv);
				student.setAge(agev);
				student.setName(namev);
			}
		}
		return student;
	}
}

3、TestStu.java:

package cn.itcast.test;

import cn.itcast.service.StuService;
import cn.itcast.vo.Student;

public class TestStu {
	public static void main(String[] args) throws Exception {
//		testAdd();
//		testDel();
		testSelect();
	}
	
	//测试添加方法
	public static void testAdd() throws Exception{
		//设置值
		Student stu = new Student();
		stu.setId("103");
		stu.setAge("23");
		stu.setName("wangwu");
		StuService.addStu(stu);
	}
	
	//测试删除方法
	public static void testDel() throws Exception{
		StuService.delStu("103");
	}
	
	//测试查询方法
	public static void testSelect() throws Exception{
		Student stu = StuService.getStu("100");
		System.out.println(stu.toString());
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值