首页 > 其他 > 详细

xml案例(考生成绩管理系统)

时间:2014-06-28 13:58:43      阅读:610      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
  1 package itacst.dao;
  2 
  3 import org.w3c.dom.Document;
  4 import org.w3c.dom.Element;
  5 import org.w3c.dom.Node;
  6 import org.w3c.dom.NodeList;
  7 
  8 import itacst.domain.Student;
  9 import itacst.exception.StudentNotExistException;
 10 import itacst.utils.XmlUtils;
 11 
 12 public class StudentDao {
 13     
 14     public void add(Student s){
 15         
 16         try {
 17             Document document = XmlUtils.getDocument();
 18             
 19             //创建出封装学生信息的标签
 20             Element student_tag = document.createElement("student");
 21             student_tag.setAttribute("idcard", s.getIdcard());
 22             student_tag.setAttribute("examid", s.getExamid());
 23             
 24             //创建于封装学生姓名、所在地和成绩的标签
 25             Element name = document.createElement("name");
 26             Element location = document.createElement("location");
 27             Element grade = document.createElement("grade");
 28             
 29             name.setTextContent(s.getName());
 30             location.setTextContent(s.getLocation());
 31             grade.setTextContent(s.getGrade()+"");//任意东西加上字符串就变成字符串
 32             
 33             student_tag.appendChild(name);
 34             student_tag.appendChild(location);
 35             student_tag.appendChild(grade);
 36             
 37             //把封装了信息学生标签,挂到文档上
 38             document.getElementsByTagName("exam").item(0).appendChild(student_tag);
 39             
 40             //更新内存
 41             XmlUtils.write2Xml(document);
 42             
 43         } catch (Exception e) {
 44             throw new RuntimeException(e);
 45             //unchecked excpeiton(运行时异常)
 46         }//checked exception(编译时的异常 )
 47     }
 48     
 49     public Student find(String examid){
 50         
 51         try {
 52             Document document = XmlUtils.getDocument();
 53             NodeList list = document.getElementsByTagName("student");
 54             
 55             for (int i = 0; i < list.getLength(); i++) {
 56                 Element student_tag = (Element) list.item(i);
 57                 if(student_tag.getAttribute("examid").equals(examid)){
 58                     //找到与examid相匹配的学生,new出一个student对象封装这个学生的信息返回
 59                     Student s = new Student();
 60                     s.setExamid(examid);
 61                     s.setIdcard(student_tag.getAttribute("idcard"));
 62                     s.setName(student_tag.getElementsByTagName("name").item(0).getTextContent());
 63                     s.setLocation(student_tag.getElementsByTagName("location").item(0).getTextContent());
 64                     s.setGrade(Double.parseDouble(student_tag.getElementsByTagName("grade").item(0).getTextContent()));
 65                 
 66                     return s;
 67                 }
 68             }
 69             
 70             return null;
 71             
 72         } catch (Exception e) {
 73             throw new RuntimeException(e);
 74         }
 75         
 76     }
 77     
 78     public void delete(String name) throws StudentNotExistException{
 79         
 80         try {
 81             Document document = XmlUtils.getDocument();
 82             
 83             NodeList list = document.getElementsByTagName("name");
 84             
 85             for (int i = 0; i < list.getLength(); i++) {
 86                 if(list.item(i).getTextContent().equals(name)){
 87                     list.item(i).getParentNode().getParentNode().removeChild(list.item(i).getParentNode());
 88                     //更新内存
 89                     XmlUtils.write2Xml(document);
 90                     return;
 91                 }
 92             }
 93             
 94             
 95             
 96             throw new StudentNotExistException(name+"not exist");
 97         }catch(StudentNotExistException e){
 98             throw e;
 99         } catch (Exception e) {
100             throw new RuntimeException(e);
101         }
102         
103     }
104 }
View Code
bubuko.com,布布扣
 1 package itacst.utils;
 2 
 3 import java.io.FileOutputStream;
 4 
 5 import javax.xml.parsers.DocumentBuilder;
 6 import javax.xml.parsers.DocumentBuilderFactory;
 7 import javax.xml.parsers.ParserConfigurationException;
 8 import javax.xml.transform.Transformer;
 9 import javax.xml.transform.TransformerConfigurationException;
10 import javax.xml.transform.TransformerFactory;
11 import javax.xml.transform.dom.DOMSource;
12 import javax.xml.transform.stream.StreamResult;
13 
14 import org.w3c.dom.Document;
15 
16 public class XmlUtils {
17     
18     private static String filename="src/exam.xml";
19     
20     //获取xml
21     public static Document getDocument() throws Exception{
22         
23         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
24         DocumentBuilder builder = factory.newDocumentBuilder();
25         
26         return builder.parse(filename);
27     }
28     
29     //thinking in java
30     public static void write2Xml(Document document) throws Exception{
31         
32         TransformerFactory factory = TransformerFactory.newInstance();
33         Transformer tf = factory.newTransformer();
34         tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename)));
35     }
36  
37 }
View Code
bubuko.com,布布扣
 1 package itacst.test;
 2 
 3 import org.junit.Test;
 4 
 5 import itacst.dao.StudentDao;
 6 import itacst.domain.Student;
 7 import itacst.exception.StudentNotExistException;
 8 
 9 public class StudentDaoTest {
10     
11     @Test
12     public void testAdd(){
13         StudentDao dao = new StudentDao();
14         Student s = new Student();
15         s.setExamid("121");
16         s.setName("zero");
17         s.setIdcard("121");
18         s.setLocation("guangzhou");
19         s.setGrade(89);
20         dao.add(s);
21     }
22     
23     @Test
24     public void testFind(){
25         StudentDao dao = new StudentDao();
26         Student s = dao.find("121");
27         
28         System.out.println(s.getName()+s.getIdcard()+s.getExamid()+s.getLocation()+s.getGrade());
29     }
30     
31     @Test
32     public void testDelete() throws Exception{
33         StudentDao dao = new StudentDao();
34         dao.delete("zero");
35     }
36 
37 }
View Code
bubuko.com,布布扣
 1 package itacst.domain;
 2 
 3 public class Student {
 4 
 5     private String idcard;
 6     private String examid;
 7     private String name;
 8     private String location;
 9     private double grade;
10 
11     public String getIdcard() {
12         return idcard;
13     }
14 
15     public void setIdcard(String idcard) {
16         this.idcard = idcard;
17     }
18 
19     public String getExamid() {
20         return examid;
21     }
22 
23     public void setExamid(String examid) {
24         this.examid = examid;
25     }
26 
27     public String getName() {
28         return name;
29     }
30 
31     public void setName(String name) {
32         this.name = name;
33     }
34 
35     public String getLocation() {
36         return location;
37     }
38 
39     public void setLocation(String location) {
40         this.location = location;
41     }
42 
43     public double getGrade() {
44         return grade;
45     }
46 
47     public void setGrade(double grade) {
48         this.grade = grade;
49     }
50 }
View Code
bubuko.com,布布扣
 1 package itacst.UI;
 2 
 3 import itacst.dao.StudentDao;
 4 import itacst.domain.Student;
 5 import itacst.exception.StudentNotExistException;
 6 
 7 import java.io.BufferedReader;
 8 import java.io.IOException;
 9 import java.io.InputStreamReader;
10 
11 public class Main {
12 
13     /**
14      * @param args
15      * @throws IOException
16      */
17     public static void main(String[] args) {
18         while(true){
19         try {
20             System.out.println("添加学生(a)    删除学生(b)     查询学生(c)");
21             System.out.println("请输入操作类型:");
22 
23             BufferedReader br = new BufferedReader(new InputStreamReader(
24                     System.in));
25             String type = br.readLine();
26             if ("a".equals(type)) {
27 
28                 System.out.print("请输入学生姓名:");
29                 String name = br.readLine();
30                 System.out.print("请输入学生准考证号:");
31                 String examid = br.readLine();
32                 System.out.print("请输入学生身份证号:");
33                 String idcard = br.readLine();
34                 System.out.print("请输入学生所在地:");
35                 String location = br.readLine();
36                 System.out.print("请输入学生成绩:");
37                 String grade = br.readLine();
38 
39                 Student s = new Student();
40                 s.setName(name);
41                 s.setExamid(examid);
42                 s.setIdcard(idcard);
43                 s.setLocation(location);
44                 s.setGrade(Double.parseDouble(grade));
45 
46                 StudentDao dao = new StudentDao();
47                 dao.add(s);
48 
49                 System.out.println("添加成功");
50 
51             } else if ("b".equals(type)) {
52 
53                 System.out.print("请输入要删除的学生姓名:");
54                 String name = br.readLine();
55 
56                 StudentDao dao = new StudentDao();
57                 try {
58                     dao.delete(name);
59                     System.out.println("删除成功!");
60                 } catch (StudentNotExistException e) {
61 
62                     e.printStackTrace();
63                     System.out.println("your delete user not exist!!");
64                 }
65 
66             } else if ("c".equals(type)) {
67 
68             } else {
69                 System.out.println("unsupport your choice!!");
70             }
71 
72         } catch (IOException e) {
73 
74             System.out.println("sorry error....");
75         }
76 
77     }
78     }
79 
80 }
View Code
bubuko.com,布布扣
 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>
 2     <student examid="222" idcard="111">
 3         <name>zhangsan</name>
 4         <location>shenyang</location>
 5         <grade>89</grade>
 6     </student>
 7     <student examid="444" idcard="333">
 8         <name>lisi</name>
 9         <location>dalian</location>
10         <grade>97</grade>
11     </student>
12     
13     
14  <student examid="321" idcard="321"><name>zero</name><location>guangzhou</location><grade>87.9</grade></student></exam>
View Code

 

xml案例(考生成绩管理系统),布布扣,bubuko.com

xml案例(考生成绩管理系统)

原文:http://www.cnblogs.com/aineko/p/3798429.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!