简单的xml 学生管理系统

实现xml基本的增删改查功能

jaxen-1.1-beta-6.jar 点击下载

dom4j-1.6.1.jar  点击下载


student.xml



 
   
    100  
    zhangsan  
    20 
    
   
    101  
    lisi  
    30 
   

学生实现类

package cn.itcast.vo;

public class Student {
	private String id;
	private String name;
	private String age;
	
	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 + "]";
	}
	
}

解析器方法

package cn.itcast.service;

import java.io.FileOutputStream;
import java.util.List;

import javax.sound.sampled.LineListener;
import javax.sql.rowset.spi.XmlWriter;

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 cn.itcast.vo.Student;

public class StuService {
	//增加
	public static void addStu(Student student) throws Exception{
		/*
		 * 1.创建解析器
		 * 2.得到document
		 * 3.获取到根节点
		 * 4.在根节点上面创建stu标签
		 * 5.在stu标签依次添加id name age
		 * 6.在id name age 依次添加值
		 * 7.回写xml
		 * */
		SAXReader saxReader =new SAXReader();
		Document document = saxReader.read("src/student.xml");
		Element rooteElement = document.getRootElement();
		Element stu = rooteElement.addElement("stu");
		Element id = stu.addElement("id");
		Element name = stu.addElement("name");
		Element age = stu.addElement("age");
		id.setText(student.getId());
		name.setText(student.getName());
		age.setText(student.getAge());
		OutputFormat format = new 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 = saxReader.read("src/student.xml");
		//获取到所有id 
		//使用xpth  //id 返回list集合
		List list = document.selectNodes("//id");
		for (Node node : list) {
			if(id.equals(node.getText())){
				Element stu = node.getParent();
				Element student = stu.getParent();
				student.remove(stu);
			}
		}
		//会写
		OutputFormat format = new OutputFormat().createPrettyPrint();
		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"), format);
		xmlWriter.write(document);
		xmlWriter.close();
	}
	
	
	//查询
	//获取到所有id 
	//不需要回写
	public static Student getStudent(String id) throws Exception{
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read("src/student.xml");
		List list = document.selectNodes("//id");
		//创建student对象
		Student student = new Student();
		
		
		for (Node node : list) {
			if(id.equals(node.getText())){
				Element stu = node.getParent();
				student.setName(stu.element("name").getText()); 
				student.setId(stu.element("id").getText()); 
				student.setAge(stu.element("age").getText()); 
			}
		}
		return student;
	}
	
}

测试

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 student =new Student();
		student.setId("102");
		student.setName("xifan");
		student.setAge("18");
		StuService.addStu(student);
	}
	//测试删除
	public static void testDel() throws Exception{
		StuService.delStu("102");
	}
	
	//测试查询
	public static void testSelect() throws Exception{
		Student student = StuService.getStudent("100");
		System.out.println(student.toString()); 
	}
}

你可能感兴趣的:(简单的xml 学生管理系统)