先看一下XML文档
<?xml version="1.0" encoding="gb2312"?>
<exam>
<student idcard="111" examid="222">
<name>张三</name>
<location>深圳</location>
<score>89</score>
</student>
<student idcard="777" examid="666">
<name>李四</name>
<location>上海</location>
<score>99</score>
</student>
</exam>
主要是对此文档读取,添加,删除,保存(刷新)等处理实现下面的功能
首先要建立一个学生的实体类
public class Student {
private String name;
private String examid;//准考证号
private String idcard;//身份证号
private String location;//地址
private String score; //分数 public Student(){ //无参构造
} public Student(String name, String examid, String idcard, String location,
String score) { //有参构造
super();
this.name = name;
this.examid = examid;
this.idcard = idcard;
this.location = location;
this.score = score;
}
//下面是set get 方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getExamid() {
return examid;
}
public void setExamid(String examid) {
this.examid = examid;
}
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
} }//类的
然后建立一个接口类,里面添加的方法分别是(添加,删除,读取)
public interface AddStu {
/**
* 添加学生
*/
public boolean addStudent(Student stu);
/**
* 删除学生
*/
public boolean removeStudent(String name);
/**
* 查询学生
*/
public Student caxunStudent(String examid);
}
然后是实现接口的类
public class AddStuimpl implements AddStu{ @Override
public boolean addStudent(Student stu) {
boolean flay=false;
Document dom=null;
//1.创建解析器
SAXReader reader = new SAXReader();
//2.解析文档
try {
dom = reader.read(new File("src/student.xml"));
Element rootElement=dom.getRootElement();
//添加元素
Element stuEle=rootElement.addElement("student");
stuEle.addAttribute("idcard", stu.getIdcard());
stuEle.addAttribute("examid", stu.getExamid());
//添加子节点
stuEle.addElement("name").setText(stu.getName());
stuEle.addElement("location").setText(stu.getLocation());
stuEle.addElement("score").setText(stu.getScore());
//更新XML (保存信息)
OutputFormat of=OutputFormat.createPrettyPrint();//格式化器
of.setEncoding("gb2312");
XMLWriter xw=new XMLWriter(new FileWriter("src/student.xml"),of);
xw.write(dom);
xw.close(); flay=true;
} catch (Exception e) {
e.printStackTrace();
} return flay; } @Override
public boolean removeStudent(String name) {
boolean flay=false;
Document dom=null;
//1.创建解析器
SAXReader reader = new SAXReader();
//2.解析文档 try {
dom = reader.read(new File("src/student.xml"));
Element root=dom.getRootElement(); //获取根节点
List<Element> list=root.elements("student"); //选择要对哪个元素进行操作
for (Element e : list) {
Element nameEle=e.element("name");
if(nameEle.getText().equals(name)){
root.remove(e);
flay=true;
} }
//更新
OutputFormat of=OutputFormat.createPrettyPrint();//格式化器
of.setEncoding("gb2312");
XMLWriter xw=new XMLWriter(new FileWriter("src/student.xml"),of);
xw.write(dom);
xw.close(); } catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return flay;
} @Override
public Student caxunStudent(String examid) {
Student s=null;
Document dom=null;
//1.创建解析器
SAXReader reader = new SAXReader();
//2.解析文档
try {
dom = reader.read(new File("src/student.xml"));
Element root=dom.getRootElement(); //获取根节点
List<Element> list=root.elements("student");
Element ele=null;
for (Element e : list) {
String str2=e.attribute("examid").getText();
//String str2=e.attributeValue("examid"); 与上一条意思一样
if(str2.equals(examid)){
ele=e;
break;
}
}
if(ele!=null){
String name=ele.elementText("name");
String location=ele.element("location").getText();
String score=ele.elementText("score");
String idcard=ele.attributeValue("idcard");
String examid1=ele.attributeValue("examid");
s=new Student(name, examid1, idcard, location, score);
}
} catch (DocumentException e) { e.printStackTrace();
}
return s;
} }
最后则是测试类及对添加等方法的测试
/**
* 测试
* 2017-5-3
*下午2:40:47
*/
public class StudentBiz {
//static Student stu=new Student();
static Scanner input=new Scanner(System.in);
static AddStuimpl asi=new AddStuimpl();
public static void main(String[] args) {
System.out.println("添加学生(1)\t删除学生(2)\t查询成绩(3)\t退出(4)");
do{
System.out.print("请选择有效序号: ");
int a=input.nextInt();
switch(a){
case 1:
System.out.println("请输入学生的姓名");
String name=input.next();
System.out.println("请输入学生的准考证号");
String examid=input.next();
System.out.println("请输入学生的身份证号");
String idcard=input.next();
System.out.println("请输入学生的所在地");
String location=input.next();
System.out.println("请输入学生的成绩");
String score=input.next();
Student stu=new Student(name, examid, idcard, location, score); boolean bool=asi.addStudent(stu);
if(bool){
System.out.println("学生添加成功");
}else{
System.out.println("添加失败");
} continue;
case 2:
System.out.println("请输入要删除的学生姓名");
String removeName=input.next();
Student stu2=new Student();
//stu2.setName(removeName); RemoveStuImpl re=new RemoveStuImpl();
//boolean bool2=re.removeStu(stu2);
boolean bool2=asi.removeStudent(removeName);
if(bool2){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
continue;
case 3:
System.out.println("请输入学生考号");
String examid1=input.next();
Student stu3=asi.caxunStudent(examid1);
if(stu3!=null){
System.out.println("学生的身份证是"+stu3.getIdcard()+",准考证"+stu3.getExamid()+
",姓名"+stu3.getName()+",分数是: "+stu3.getScore()+",地址"+stu3.getLocation());
}else{
System.out.println("查无此人");
}
continue;
case 4:
System.out.println("系统退出");
break;
}
break;
}while(true); }
}
本文没有使用工具类,后续可自行添加!!!!!