<student>
student>
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;
}
}
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;
}
}
}
}
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;
}
}
}
}
<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>
<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());
<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>