先看一下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>
主要是对此文档读取,添加,删除,保存(刷新)等处理实现下面的功能
首先要建立一个学生的实体类
1 public class Student { 2 private String name; 3 private String examid;//准考证号 4 private String idcard;//身份证号 5 private String location;//地址 6 private String score; //分数 7 8 9 public Student(){ //无参构造 10 } 11 12 public Student(String name, String examid, String idcard, String location, 13 String score) { //有参构造 14 super(); 15 this.name = name; 16 this.examid = examid; 17 this.idcard = idcard; 18 this.location = location; 19 this.score = score; 20 } 21 //下面是set get 方法 22 public String getName() { 23 return name; 24 } 25 public void setName(String name) { 26 this.name = name; 27 } 28 public String getExamid() { 29 return examid; 30 } 31 public void setExamid(String examid) { 32 this.examid = examid; 33 } 34 public String getIdcard() { 35 return idcard; 36 } 37 public void setIdcard(String idcard) { 38 this.idcard = idcard; 39 } 40 public String getLocation() { 41 return location; 42 } 43 public void setLocation(String location) { 44 this.location = location; 45 } 46 public String getScore() { 47 return score; 48 } 49 public void setScore(String score) { 50 this.score = score; 51 } 52 53 54 55 }//类的
然后建立一个接口类,里面添加的方法分别是(添加,删除,读取)
1 public interface AddStu { 2 /** 3 * 添加学生 4 */ 5 public boolean addStudent(Student stu); 6 /** 7 * 删除学生 8 */ 9 public boolean removeStudent(String name); 10 /** 11 * 查询学生 12 */ 13 public Student caxunStudent(String examid); 14 }
然后是实现接口的类
1 public class AddStuimpl implements AddStu{ 2 3 @Override 4 public boolean addStudent(Student stu) { 5 boolean flay=false; 6 Document dom=null; 7 //1.创建解析器 8 SAXReader reader = new SAXReader(); 9 //2.解析文档 10 try { 11 dom = reader.read(new File("src/student.xml")); 12 Element rootElement=dom.getRootElement(); 13 //添加元素 14 Element stuEle=rootElement.addElement("student"); 15 stuEle.addAttribute("idcard", stu.getIdcard()); 16 stuEle.addAttribute("examid", stu.getExamid()); 17 //添加子节点 18 stuEle.addElement("name").setText(stu.getName()); 19 stuEle.addElement("location").setText(stu.getLocation()); 20 stuEle.addElement("score").setText(stu.getScore()); 21 //更新XML (保存信息) 22 OutputFormat of=OutputFormat.createPrettyPrint();//格式化器 23 of.setEncoding("gb2312"); 24 XMLWriter xw=new XMLWriter(new FileWriter("src/student.xml"),of); 25 xw.write(dom); 26 xw.close(); 27 28 flay=true; 29 } catch (Exception e) { 30 e.printStackTrace(); 31 } 32 33 return flay; 34 35 } 36 37 @Override 38 public boolean removeStudent(String name) { 39 boolean flay=false; 40 Document dom=null; 41 //1.创建解析器 42 SAXReader reader = new SAXReader(); 43 //2.解析文档 44 45 try { 46 dom = reader.read(new File("src/student.xml")); 47 Element root=dom.getRootElement(); //获取根节点 48 List<Element> list=root.elements("student"); //选择要对哪个元素进行操作 49 for (Element e : list) { 50 Element nameEle=e.element("name"); 51 if(nameEle.getText().equals(name)){ 52 root.remove(e); 53 flay=true; 54 } 55 56 } 57 //更新 58 OutputFormat of=OutputFormat.createPrettyPrint();//格式化器 59 of.setEncoding("gb2312"); 60 XMLWriter xw=new XMLWriter(new FileWriter("src/student.xml"),of); 61 xw.write(dom); 62 xw.close(); 63 64 65 } catch (DocumentException e) { 66 // TODO Auto-generated catch block 67 e.printStackTrace(); 68 } catch (IOException e) { 69 // TODO Auto-generated catch block 70 e.printStackTrace(); 71 } 72 73 return flay; 74 } 75 76 @Override 77 public Student caxunStudent(String examid) { 78 Student s=null; 79 Document dom=null; 80 //1.创建解析器 81 SAXReader reader = new SAXReader(); 82 //2.解析文档 83 try { 84 dom = reader.read(new File("src/student.xml")); 85 Element root=dom.getRootElement(); //获取根节点 86 List<Element> list=root.elements("student"); 87 Element ele=null; 88 for (Element e : list) { 89 String str2=e.attribute("examid").getText(); 90 //String str2=e.attributeValue("examid"); 与上一条意思一样 91 if(str2.equals(examid)){ 92 ele=e; 93 break; 94 } 95 } 96 if(ele!=null){ 97 String name=ele.elementText("name"); 98 String location=ele.element("location").getText(); 99 String score=ele.elementText("score"); 100 String idcard=ele.attributeValue("idcard"); 101 String examid1=ele.attributeValue("examid"); 102 s=new Student(name, examid1, idcard, location, score); 103 } 104 } catch (DocumentException e) { 105 106 e.printStackTrace(); 107 } 108 return s; 109 } 110 111 }
最后则是测试类及对添加等方法的测试
1 /** 2 * 测试 3 * 2017-5-3 4 *下午2:40:47 5 */ 6 public class StudentBiz { 7 //static Student stu=new Student(); 8 static Scanner input=new Scanner(System.in); 9 static AddStuimpl asi=new AddStuimpl(); 10 public static void main(String[] args) { 11 System.out.println("添加学生(1)\t删除学生(2)\t查询成绩(3)\t退出(4)"); 12 do{ 13 System.out.print("请选择有效序号: "); 14 int a=input.nextInt(); 15 switch(a){ 16 case 1: 17 System.out.println("请输入学生的姓名"); 18 String name=input.next(); 19 System.out.println("请输入学生的准考证号"); 20 String examid=input.next(); 21 System.out.println("请输入学生的身份证号"); 22 String idcard=input.next(); 23 System.out.println("请输入学生的所在地"); 24 String location=input.next(); 25 System.out.println("请输入学生的成绩"); 26 String score=input.next(); 27 Student stu=new Student(name, examid, idcard, location, score); 28 29 boolean bool=asi.addStudent(stu); 30 if(bool){ 31 System.out.println("学生添加成功"); 32 }else{ 33 System.out.println("添加失败"); 34 } 35 36 continue; 37 case 2: 38 System.out.println("请输入要删除的学生姓名"); 39 String removeName=input.next(); 40 Student stu2=new Student(); 41 //stu2.setName(removeName); 42 43 RemoveStuImpl re=new RemoveStuImpl(); 44 //boolean bool2=re.removeStu(stu2); 45 boolean bool2=asi.removeStudent(removeName); 46 if(bool2){ 47 System.out.println("删除成功"); 48 }else{ 49 System.out.println("删除失败"); 50 } 51 continue; 52 case 3: 53 System.out.println("请输入学生考号"); 54 String examid1=input.next(); 55 Student stu3=asi.caxunStudent(examid1); 56 if(stu3!=null){ 57 System.out.println("学生的身份证是"+stu3.getIdcard()+",准考证"+stu3.getExamid()+ 58 ",姓名"+stu3.getName()+",分数是: "+stu3.getScore()+",地址"+stu3.getLocation()); 59 }else{ 60 System.out.println("查无此人"); 61 } 62 continue; 63 case 4: 64 System.out.println("系统退出"); 65 break; 66 } 67 break; 68 }while(true); 69 70 } 71 }
本文没有使用工具类,后续可自行添加!!!!!