XML文件学生管理小系统

基于XML简单写了一个学生管理的系统

XML文件:Student.xml
根目录,Student

在这里插入图片描述


<student>
student>

简单只用了三个信息,其他信息可以参考自行添加

学号:id,姓名:name,年龄:age

domain 学生类

package cn.itsource._06Test;

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;
	}
}

StudentDao类,主要是对学生进行增删改查的方法类

package cn.itsource._06Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

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;

public class StudentDAO{
	static File file = new File("G:/WebWorkPlace/0509Xml/src/cn/itsource/_05HW/student.xml");
	/**
	 * 添加学生的方法
	 * @param stu
	 * @throws DocumentException
	 * @throws IOException
	 */
	@SuppressWarnings({ "unchecked", "resource" })
	public void add(Student stu) throws DocumentException, IOException{
		Scanner scanner=new Scanner(System.in);
		while(true){
    		System.out.println("点击任意键继续添加学生信息,停止添加输入0");
    		if(scanner.nextInt()==0)break;
    		System.out.println("请输入学生信息:学号,姓名,年龄,");
    		String str=scanner.next();
    		stu.setId(str);
    		stu.setName(scanner.next());
    		stu.setAge(scanner.next());
    		SAXReader saxReader = new SAXReader();
    		Document doc = saxReader.read(file);
    		Element rootElement = doc.getRootElement();
    		List<Node> stulist = doc.selectNodes("//@id");
    		for (Node node : stulist) {
				if(node.getText().equals(str)){
					System.out.println("此学号已占用,请重新输入");
					return;
				}
			}
        	Element stu1 = rootElement.addElement("stu");
        	stu1.addAttribute("id",stu.getId());
        	stu1.addElement("name").addText(stu.getName());
        	stu1.addElement("age").addText(stu.getAge());
        	FileWriter fileWriter = new FileWriter(file);
        	OutputFormat format = OutputFormat.createPrettyPrint();
        	XMLWriter xmlWriter = new XMLWriter(fileWriter,format);
        	xmlWriter.write(doc);
        	xmlWriter.close();
        	System.out.println("信息导入成功");
    	}
	}
	/**
	 * 删除学生信息的方法
	 * @param id
	 * @throws DocumentException
	 * @throws IOException
	 */
	@SuppressWarnings({ "unchecked" })
	public void delete(String id) throws DocumentException, IOException{
		SAXReader saxReader = new SAXReader();
		Document doc = saxReader.read(file);
		Element rootElement = doc.getRootElement();
		List<Node> stulist = doc.selectNodes("//@id");
		int i=0;
		for (Node node : stulist) {
			if(node.getText().equals(id)){
				Element parent = node.getParent();
				rootElement.remove(parent);
				break;
			}
			i++;
			if(i==stulist.size()){
				System.out.println("查无此人");
				return;
			}
		}
		FileWriter fileWriter = new FileWriter(file);
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter xmlWriter = new XMLWriter(fileWriter,format);
		xmlWriter.write(doc);
		xmlWriter.close();
		System.out.println("删除成功");
	}
	/**
	 * 修改学生信息的方法
	 * @param id
	 * @throws DocumentException
	 * @throws IOException
	 */
	@SuppressWarnings({ "unchecked", "resource" })
	public void change(String id) throws DocumentException, IOException{
		Scanner scanner=new Scanner(System.in);
		SAXReader saxReader = new SAXReader();
		Document doc = saxReader.read(file);
		List<Node> stulist = doc.selectNodes("//@id");
		int i=0;
		for (Node node : stulist) {
			if(node.getText().equals(id)){
				System.out.println("请输入要修改的学生的信息:学号,姓名,年龄:");
				Element parent = node.getParent();
				parent.attribute("id").setText(scanner.next());
				parent.element("name").setText(scanner.next());
				parent.element("age").setText(scanner.next());
				break;
			}
			i++;
			if(i==stulist.size()){
				System.out.println("查无此人");
				return;
			}
		}
		FileWriter fileWriter = new FileWriter(file);
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter xmlWriter = new XMLWriter(fileWriter,format);
		xmlWriter.write(doc);
		xmlWriter.close();
		System.out.println("修改成功");
	}
	/**
	 * 查询学生信息的方法
	 * @param id
	 * @throws IOException
	 * @throws DocumentException
	 */
	@SuppressWarnings("unchecked")
	public void find(String id) throws IOException, DocumentException{
		SAXReader saxReader = new SAXReader();
		Document doc = saxReader.read(file);
		List<Node> stulist = doc.selectNodes("//@id");
		int i=0;
		for (Node node : stulist) {
			if(node.getText().equals(id)){
				Element parent = node.getParent();
				System.out.println("该学生信息为:");
				System.out.println("学号:"+parent.attribute("id").getText());
				System.out.println("姓名:"+parent.element("name").getText());
				System.out.println("年龄:"+parent.element("age").getText());
				break;
			}
			i++;
			if(i==stulist.size()){
				System.out.println("查无此人");
				return;
			}
		}
	}
}

运行Main类

package cn.itsource._06Test;

import java.io.IOException;
import java.util.Scanner;

import org.dom4j.DocumentException;

public class Main {
	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException, DocumentException {
		int choice;
		Student stu = new Student();
		StudentDAO dao = new StudentDAO();
		while(true){
	        System.out.println("1:添加学生,2:删除学生,3:改变学生信息,4:查找学生,5:退出");
	        System.out.println("请输入操作类型:");
	        Scanner scanner=new Scanner(System.in);
	        choice=scanner.nextInt();
	        if(choice==5)break;
	        switch(choice){
		        case 1:
		        	dao.add(stu);break;
		        case 2:
		        	System.out.println("请输入学生的学号:");
		        	dao.delete(scanner.next());break;
		        case 3:
		        	System.out.println("请输入学生的学号:");
		        	dao.change(scanner.next());	break;
		        case 4:
		        	System.out.println("请输入学生的学号:");
		        	dao.find(scanner.next());break;
	        }
        }
	}
}

控制台输入的信息

XML文件学生管理小系统_第1张图片

XML文件输出信息



<student> 
  <stu id="1001"> 
    <name>小李name>  
    <age>18age> 
  stu>  
  <stu id="1002"> 
    <name>冉兄name>  
    <age>18age> 
  stu>  
  <stu id="1003"> 
    <name>昊兄name>  
    <age>18age> 
  stu>  
  <stu id="1004">
    <name>小何name>
    <age>18age>
  stu>
student>

控制台继续操作

XML文件学生管理小系统_第2张图片

XML文件输出信息



<student> 
  <stu id="1001"> 
    <name>小李name>  
    <age>18age> 
  stu>  
  <stu id="1002"> 
    <name>冉兄name>  
    <age>18age> 
  stu>  
  <stu id="1003"> 
    <name>昊兄name>  
    <age>18age> 
  stu> 
student>

控制台继续操作

根据控制台输出信息,我在代码上稍微修改了一下
修改change方法,最后增加了一个输出语句:

System.out.println("修改成功");

查询学生信息方法find,修改了一下打印出来的效果:

System.out.println("该学生信息为:");
System.out.println("学号:"+parent.attribute("id").getText());
System.out.println("姓名:"+parent.element("name").getText());
System.out.println("年龄:"+parent.element("age").getText());

XML文件学生管理小系统_第3张图片

XML文件输出信息



<student> 
  <stu id="1001"> 
    <name>小李name>  
    <age>18age> 
  stu>  
  <stu id="1002"> 
    <name>冉兄name>  
    <age>18age> 
  stu>  
  <stu id="1004"> 
    <name>小何name>  
    <age>20age> 
  stu> 
student>

你可能感兴趣的:(JAVA)