*使用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、增加操作
- 创建解析器
- 得到document
- 获取根节点
- 在根节点上创建stu标签
- 在stu标签上依次添加id,name,age(addElement方法)
- 在id,name,age上依次添加值(setText方法)
- 回写xml
3、删除操作
- 创建解析器
- 得到document
- 获取所有的id
- 遍历list集合
- 判断list里的id和传递的id是否相同
- 如果相同,把id所在的stu删除
4、查询操作
- 创建解析器
- 得到document
- 获取所有的id
- 返回list,遍历list
- 得到每一个id
- 获取id节点的值
- 判断list里的id和传递的id是否相同
- 如果相同,先获取id的父节点stu
- 通过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());
}
}