基于Xpath的简单的学生信息管理系统

时间:2023-01-17 15:53:04

基于Xpath所做的简单的学生信息管理系统,没有设计现实的窗体部分,较为交单粗暴。
首页在编写代码之前,首先需要引入两个myEclipse的插件包,加入到项目中,具体方法是,新建lib文件夹在java项目中,将压缩包复制粘贴进入lib文件夹,在创建到项目中去即可,最终结果如下图所示:
基于Xpath的简单的学生信息管理系统
接下来开始java代码的编写:
1、首先在src下编写XML文件,代码如下:

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

<student>
<stu>
<id>002</id>
<name>李四</name>
<age>20</age>
</stu>
<stu>
<id>003</id>
<name>王五</name>
<age>23</age>
</stu>
</student>

写完后,接下来编写所需要的具体操作函数的代码StudentOper.java,具体代码如下:

package studentOper;

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 studentSource.Student;

public class StudentOper {

//添加操作(003,王五,23)

public static void addstu(Student student) throws Exception{

SAXReader saxReader = new SAXReader();

Document doc = saxReader.read("src/studentsrc.xml");

Element root = doc.getRootElement();

Element stu = root.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 = OutputFormat.createPrettyPrint();

XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/studentsrc.xml"), format);

xmlWriter.write(doc);

xmlWriter.close();


}

//删除id=001的学生
public static void deleteStu(String id ) throws Exception
{
SAXReader saxReader = new SAXReader();

Document doc = saxReader.read("src/studentsrc.xml");

List<Node> list = doc.selectNodes("//id");

for (Node node : list) {
String nodeValue = node.getText();

if (nodeValue.equals(id)) {
Element stu = node.getParent();
Element student = stu.getParent();
student.remove(stu);


}
}
OutputFormat format = OutputFormat.createPrettyPrint();

XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/studentsrc.xml"), format);

xmlWriter.write(doc);

xmlWriter.close();
}


//查找id=002的学生信息
public static Student searchstu(String id) throws Exception
{
Student student = new Student();

SAXReader saxReader = new SAXReader();

Document doc = saxReader.read("src/studentsrc.xml");

List<Node> list = doc.selectNodes("//id");

for (Node node : list) {
String nodeValue = node.getText();

if (nodeValue.equals(id)) {

Element stu = node.getParent();

String nameValue = stu.element("name").getText();
String ageValue = stu.element("age").getText();

student.setId(id);
student.setName(nameValue);
student.setAge(ageValue);

}

}
return student;

}

}

而我们在创建这个代码是,发现因为student的属性参数较多,于是在创建一个student类来存储这些属性,代码如下:

package studentSource;

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 + "]";
}


}

接下来,编写studentmain代码。调用这些功能函数即可:

package student;

import studentOper.StudentOper;
import studentSource.Student;

public class StudentMain {

/**
* @param args
* @throws Exception
*/

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//添加操作(003,王五,23)
//addStudent();
//删除id=001的学生
//deleteStu();
//查找id=002的学生信息
searchStu();

}

private static void searchStu() throws Exception {
// TODO Auto-generated method stub
Student student = StudentOper.searchstu("002");
System.out.println(student.toString());

}

private static void deleteStu() throws Exception {
// TODO Auto-generated method stub
StudentOper.deleteStu("001");
}

private static void addStudent() throws Exception {
// TODO Auto-generated method stub
Student stu = new Student();
stu.setId("003");
stu.setName("王五");
stu.setAge("23");
StudentOper.addstu(stu);
}

}