Skip to content

Commit 1a8d6c1

Browse files
author
ltwell
committed
Add day02_exam
1 parent 17f6a4f commit 1a8d6c1

File tree

12 files changed

+495
-0
lines changed

12 files changed

+495
-0
lines changed

day02_exam/lib/dom4j-1.6.1.jar

307 KB
Binary file not shown.
239 KB
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.itcast.dao;
2+
3+
import cn.itcast.domain.Student;
4+
5+
public interface IStudentDao {
6+
7+
/**
8+
* 添加学生信息到XML中
9+
* @param s
10+
* @return
11+
*/
12+
boolean createStudent(Student s);
13+
14+
/**
15+
* 根据准考证号查询学生信息
16+
* @param examid
17+
* @return 如果学生不存在,返回null
18+
*/
19+
Student findStudent(String examid);
20+
21+
/**
22+
* 根据学生姓名删除学生
23+
* @param name
24+
* @return 如果人不存在也返回false
25+
*/
26+
boolean deleteStudent(String name);
27+
28+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package cn.itcast.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 cn.itcast.domain.Student;
9+
import cn.itcast.util.DocumentUtil;
10+
//异常:抛的话,上层能解决才行
11+
//上一层如果处理不了,自己必须处理。
12+
13+
public class StudentDao implements IStudentDao {
14+
/**
15+
* 添加学生信息到XML中
16+
* @param s
17+
* @return
18+
*/
19+
public boolean createStudent(Student s){
20+
System.out.println("JAXP");
21+
//目标:在根元素exam中添加student子元素
22+
boolean result = false;
23+
try {
24+
Document document = DocumentUtil.getDocument();
25+
//创建name、location、grade元素并设置其主体内容
26+
Element nameE = document.createElement("name");
27+
nameE.setTextContent(s.getName());
28+
Element locationE = document.createElement("location");
29+
locationE.setTextContent(s.getLocation());
30+
Element gradeE = document.createElement("grade");
31+
gradeE.setTextContent(s.getGrade()+"");
32+
//创建student元素,并设置其属性
33+
Element studentE = document.createElement("student");
34+
studentE.setAttribute("idcard", s.getIdcard());
35+
studentE.setAttribute("examid", s.getExamid());//CTRL+ALT+ARROW
36+
37+
studentE.appendChild(nameE);
38+
studentE.appendChild(locationE);
39+
studentE.appendChild(gradeE);
40+
//得到exam元素,把student挂接上去
41+
Node node = document.getElementsByTagName("exam").item(0);
42+
node.appendChild(studentE);
43+
//写回XML文件中
44+
DocumentUtil.write2xml(document);
45+
result = true;
46+
} catch (Exception e) {
47+
throw new RuntimeException(e);//异常转义。异常链
48+
}
49+
return result;
50+
}
51+
/**
52+
* 根据准考证号查询学生信息
53+
* @param examid
54+
* @return 如果学生不存在,返回null
55+
*/
56+
public Student findStudent(String examid){
57+
Student s = null;
58+
59+
try{
60+
//得到Document对象
61+
Document document = DocumentUtil.getDocument();
62+
//得到所有的student元素
63+
NodeList nl = document.getElementsByTagName("student");
64+
//遍历student元素,判断他的examid属性的取值是否与参数匹配
65+
for(int i=0;i<nl.getLength();i++){
66+
Node node = nl.item(i);
67+
// if(node.getNodeType()==Node.ELEMENT_NODE){
68+
// Element e = (Element)node;
69+
if(node instanceof Element){
70+
Element e = (Element)node;
71+
if(e.getAttribute("examid").equals(examid)){
72+
//如果匹配:说明找到了学生;创建学生对象
73+
s = new Student();
74+
//设置学生对象的各个属性取值
75+
s.setExamid(examid);
76+
s.setIdcard(e.getAttribute("idcard"));
77+
s.setName(e.getElementsByTagName("name").item(0).getTextContent());
78+
s.setLocation(e.getElementsByTagName("location").item(0).getTextContent());
79+
s.setGrade(Float.parseFloat(e.getElementsByTagName("grade").item(0).getTextContent()));
80+
}
81+
}
82+
}
83+
}catch(Exception e){
84+
throw new RuntimeException(e);
85+
}
86+
87+
return s;
88+
}
89+
/**
90+
* 根据学生姓名删除学生
91+
* @param name
92+
* @return 如果人不存在也返回false
93+
*/
94+
public boolean deleteStudent(String name){
95+
boolean result = false;
96+
try{
97+
//得到Document对象
98+
Document document = DocumentUtil.getDocument();
99+
//得到所有的name元素
100+
NodeList nl = document.getElementsByTagName("name");
101+
//遍历name元素,判断其主体内容是否与参数一致
102+
for(int i=0;i<nl.getLength();i++){
103+
//如果一致:找到他的爸爸的爸爸,删除它的爸爸
104+
Node n = nl.item(i);
105+
if(n.getTextContent().equals(name)){
106+
n.getParentNode().getParentNode().removeChild(n.getParentNode());
107+
//写回XML文档
108+
DocumentUtil.write2xml(document);
109+
result = true;
110+
break;
111+
}
112+
}
113+
}catch(Exception e){
114+
throw new RuntimeException(e);
115+
}
116+
return result;
117+
}
118+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package cn.itcast.dao;
2+
3+
import java.util.List;
4+
5+
import org.dom4j.Document;
6+
import org.dom4j.Element;
7+
import org.dom4j.Node;
8+
9+
import cn.itcast.domain.Student;
10+
import cn.itcast.util.Dom4JUtil;
11+
12+
public class StudentDom4JDao implements IStudentDao{
13+
14+
@Override
15+
public boolean createStudent(Student s) {
16+
System.out.println("Dom4j");
17+
boolean result = false;
18+
19+
try{
20+
//得到Document对象
21+
Document document = Dom4JUtil.getDocument();
22+
//得到根元素
23+
Element root = document.getRootElement();
24+
//加数据即可<student>
25+
Element studentE = root.addElement("student")
26+
.addAttribute("examid", s.getExamid())
27+
.addAttribute("idcard", s.getIdcard());
28+
studentE.addElement("name").setText(s.getName());
29+
studentE.addElement("location").setText(s.getLocation());
30+
studentE.addElement("grade").setText(s.getGrade()+"");
31+
//写回XML文档
32+
Dom4JUtil.write2xml(document);
33+
result = true;
34+
}catch(Exception e){
35+
throw new RuntimeException(e);
36+
}
37+
38+
return result;
39+
}
40+
41+
@Override
42+
public boolean deleteStudent(String name) {
43+
boolean result = false;
44+
try{
45+
//得到Document
46+
Document document = Dom4JUtil.getDocument();
47+
//选择所有的name元素
48+
List<Node> nodes = document.selectNodes("//name");
49+
//遍历:判断元素的内容是否与参数一致
50+
for(Node n:nodes){
51+
if(n.getText().equals(name))
52+
//用他的爷爷删除它的爸爸
53+
n.getParent().getParent().remove(n.getParent());
54+
}
55+
//写回XML文档
56+
Dom4JUtil.write2xml(document);
57+
result = true;
58+
}catch(Exception e){
59+
throw new RuntimeException(e);
60+
}
61+
return result;
62+
}
63+
64+
@Override
65+
public Student findStudent(String examid) {
66+
Student s = null;
67+
try{
68+
Document document = Dom4JUtil.getDocument();
69+
Node node = document.selectSingleNode("//student[@examid='"+examid+"']");
70+
if(node!=null){
71+
Element e = (Element)node;
72+
s = new Student();
73+
s.setExamid(examid);
74+
s.setIdcard(e.valueOf("@idcard"));
75+
s.setName(e.element("name").getText());
76+
s.setLocation(e.elementText("location"));
77+
s.setGrade(Float.parseFloat(e.elementText("grade")));
78+
}
79+
}catch(Exception e){
80+
throw new RuntimeException(e);
81+
}
82+
return s;
83+
}
84+
85+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.itcast.domain;
2+
3+
public class Student {
4+
private String idcard;
5+
private String examid;
6+
private String name;
7+
private String location;
8+
private float grade;
9+
public String getIdcard() {
10+
return idcard;
11+
}
12+
public void setIdcard(String idcard) {
13+
this.idcard = idcard;
14+
}
15+
public String getExamid() {
16+
return examid;
17+
}
18+
public void setExamid(String examid) {
19+
this.examid = examid;
20+
}
21+
public String getName() {
22+
return name;
23+
}
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
public String getLocation() {
28+
return location;
29+
}
30+
public void setLocation(String location) {
31+
this.location = location;
32+
}
33+
public float getGrade() {
34+
return grade;
35+
}
36+
public void setGrade(float grade) {
37+
this.grade = grade;
38+
}
39+
@Override
40+
public String toString() {
41+
return "Student [examid=" + examid + ", grade=" + grade + ", idcard="
42+
+ idcard + ", location=" + location + ", name=" + name + "]";
43+
}
44+
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.itcast.test;
2+
3+
import cn.itcast.dao.IStudentDao;
4+
import cn.itcast.dao.StudentDao;
5+
6+
public class StudentDaoTest {
7+
8+
public static void main(String[] args) {
9+
IStudentDao dao = new StudentDao();
10+
//
11+
// Student s = new Student();
12+
// s.setIdcard("555");
13+
// s.setExamid("666");
14+
// s.setName("Àî×ÚÈð");
15+
// s.setLocation("̨±±");
16+
// s.setGrade(100);
17+
//
18+
// dao.createStudent(s);
19+
// Student s = dao.findStudent("666");
20+
// System.out.println(s);
21+
boolean b = dao.deleteStudent("Àî×ÚÈð");
22+
System.out.println(b);
23+
}
24+
25+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cn.itcast.test;
2+
3+
import cn.itcast.dao.IStudentDao;
4+
import cn.itcast.dao.StudentDom4JDao;
5+
import cn.itcast.domain.Student;
6+
import junit.framework.Assert;
7+
import org.junit.AfterClass;
8+
import org.junit.BeforeClass;
9+
import org.junit.Test;
10+
11+
public class StudentDom4JDaoTest {
12+
private static IStudentDao dao;
13+
@BeforeClass
14+
public static void init(){
15+
dao = new StudentDom4JDao();
16+
}
17+
@AfterClass
18+
public static void destory(){
19+
dao = null;
20+
}
21+
@Test
22+
public void testCreateStudent() {
23+
Student s = new Student();
24+
s.setExamid("438");
25+
s.setIdcard("250");
26+
s.setName("ÕŰ׳Õ");
27+
s.setLocation("ÈÕ±¾");
28+
s.setGrade(100);
29+
boolean b = dao.createStudent(s);
30+
Assert.assertTrue(b);
31+
}
32+
@Test
33+
public void testFindStudent() {
34+
Student s = dao.findStudent("438");
35+
Assert.assertNotNull(s);
36+
s = dao.findStudent("834");
37+
Assert.assertNull(s);
38+
}
39+
@Test
40+
public void testDeleteStudent() {
41+
boolean b = dao.deleteStudent("ÕŰ׳Õ");
42+
Assert.assertTrue(b);
43+
44+
}
45+
46+
47+
48+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.itcast.util;
2+
3+
import javax.xml.parsers.DocumentBuilder;
4+
import javax.xml.parsers.DocumentBuilderFactory;
5+
import javax.xml.transform.Transformer;
6+
import javax.xml.transform.TransformerFactory;
7+
import javax.xml.transform.dom.DOMSource;
8+
import javax.xml.transform.stream.StreamResult;
9+
10+
import org.w3c.dom.Document;
11+
//操作XML的工具类
12+
//工具类中的异常可以抛也可以处理
13+
public class DocumentUtil {
14+
public static Document getDocument() throws Exception{
15+
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
16+
return builder.parse("src/exam.xml");
17+
}
18+
public static void write2xml(Document document)throws Exception{
19+
Transformer ts = TransformerFactory.newInstance().newTransformer();
20+
ts.transform(new DOMSource(document), new StreamResult("src/exam.xml"));
21+
}
22+
}

0 commit comments

Comments
 (0)