package cn.tyz.dao;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import cn.tyz.bean.Student;
import cn.tyz.exception.StudentNotExcepton;
import cn.tyz.utils.XmlUtils;
public class StudentDao
{
//增加考试信息
public void add(Student stu)
{
try
{
Document document = XmlUtils.getDocument();
//创建出封装学生属性的标签
Element student_tag = document.createElement("student");
student_tag.setAttribute("idcard", stu.getIdCard());
student_tag.setAttribute("examid", stu.getExamId());
//创建用于封装学生姓名,所在地和成绩的标签
Element name = document.createElement("name");
Element location = document.createElement("location");
Element grade = document.createElement("grade");
//为标签添加值
name.setTextContent(stu.getName());
location.setTextContent(stu.getLocation());
grade.setTextContent("" + stu.getGrade());
//为各个标签元素建立父子关系
student_tag.appendChild(name);
student_tag.appendChild(location);
student_tag.appendChild(grade);
//把封装了信息学生标签,挂到文档上
document.getElementsByTagName("exam").item(0).appendChild(student_tag);
//调用自定义工具方法将更新的内容从内存中写入文件
XmlUtils.writeToXml(document);
}
catch (Exception e)
{
//将异常转型为运行时异常,再抛给上层,避免给上层带来麻烦
throw new RuntimeException(e);
}
}
//查找考生信息
public Student find(String examId)
{
Student stu = new Student();
try
{
Document document = XmlUtils.getDocument();
//得到XML文档所有学生节点
NodeList list = document.getElementsByTagName("student");
//遍历
for(int i=0; i<list.getLength(); i++)
{
Element student_tag = (Element) list.item(i);
if(student_tag.getAttribute("examid").equals(examId))
{
stu.setExamId(examId);
stu.setIdCard(student_tag.getAttribute("idcard"));
stu.setName(student_tag.getElementsByTagName("name").item(0).getTextContent());
stu.setLocation(student_tag.getElementsByTagName("location").item(0).getTextContent());
stu.setGrade(Double.parseDouble(student_tag.getElementsByTagName("grade").item(0).getTextContent()));
}
}
}
catch (Exception e)
{
//将异常转型为运行时异常,再抛给上层,避免给上层带来麻烦
throw new RuntimeException(e);
}
return stu;
}
//删除考生信息
public void delete(String name) throws StudentNotExcepton
{
try
{
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("name");
for(int i=0; i<list.getLength(); i++)
{
if(list.item(i).getTextContent().equals(name))
{
list.item(i).getParentNode().getParentNode().removeChild(list.item(i).getParentNode());
//调用自定义工具方法将更新的内容从内存中写入文件
XmlUtils.writeToXml(document);
return;
}
}
throw new StudentNotExcepton(name + "不存在!");
}
catch (StudentNotExcepton e)
{
throw e;
}
catch (Exception e)
{
//将异常转型为运行时异常,再抛给上层,避免给上层带来麻烦
throw new RuntimeException();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Exam.rar (14个子文件)
Exam
src
cn
tyz
dao
StudentDao.java 3KB
exception
StudentNotExcepton.java 526B
utils
XmlUtils.java 1KB
UI
Main.java 2KB
bean
Student.java 806B
exam.xml 321B
bin
cn
tyz
dao
StudentDao.class 3KB
exception
StudentNotExcepton.class 744B
utils
XmlUtils.class 2KB
UI
Main.class 3KB
bean
Student.class 1KB
exam.xml 321B
.classpath 232B
.project 380B
共 14 条
- 1
资源评论
- woshishenjinlei2014-05-29终于找到一个能运行的了 难道是之前我运行的方式不对?反正对我还是有帮助的 谢了
- 孙歌2012-11-28内容较少,还是可以看看的!
- menggeyili2014-01-05dom解析太麻烦了,下了个dom4j,挺方便的
- jingberber2013-08-07这个对我很有用,通过exam的例子,把信息写入xml中··不过不是通过服务器···但是也很有用····
- enduoxuan2012-12-10很好,对我帮助很大,是我要找的
tyz19900909
- 粉丝: 4
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功