黑马程序员技术交流社区
标题:
JavaEE的13种核心技术规范-XML(3)
[打印本页]
作者:
li745547
时间:
2018-3-25 14:13
标题:
JavaEE的13种核心技术规范-XML(3)
c、设计JavaBean
[java]
view plain
copy
public class Student {
private String idcard;
private String examid;
private String name;
private String location;
private float grade;
public Student(){}
public Student(String idcard, String examid, String name, String location,
float grade) {
super();
this.idcard = idcard;
this.examid = examid;
this.name = name;
this.location = location;
this.grade = grade;
}
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public String getExamid() {
return examid;
}
public void setExamid(String examid) {
this.examid = examid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public float getGrade() {
return grade;
}
public void setGrade(float grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Student [idcard=" + idcard + ", examid=" + examid + ", name="
+ name + ", location=" + location + ", grade=" + grade + "]";
}
}
d、开发DAO
数据访问对象
[java]
view plain
copy
public class StudentDao {
/**
* 保存学生信息到XML文件中
* @param student 封装要保存的信息
* @return 成功返回true,否则false
* @throws Exception
*/
public boolean save(Student student) throws Exception{
if(student==null)
throw new IllegalArgumentException("学生参数不能为null");
boolean result = false;
/*
* <student idcard="111" examid="222">
<name>刘丰</name>
<location>湖北</location>
<grade>100</grade>
</student>
*/
//得到Document
Document document = JaxpUtil.getDocument();
//创建一个student元素:设置属性
Element studentE = document.createElement("student");//<student></student>
studentE.setAttribute("idcard", student.getIdcard());
studentE.setAttribute("examid", student.getExamid());//<student idcard="111" examid="222"></student>
//创建name,location,grade元素,挂到student上
Element nameE = document.createElement("name");
nameE.setTextContent(student.getName());//<name>刘丰</name>
Element locationE = document.createElement("location");
locationE.setTextContent(student.getLocation());//<location>湖北</location>
Element gradeE = document.createElement("grade");
gradeE.setTextContent(student.getGrade()+"");//<grade>100</grade>
studentE.appendChild(nameE);
studentE.appendChild(locationE);
studentE.appendChild(gradeE);
//把student挂接到exam上
Node examNode = document.getElementsByTagName("exam").item(0);
examNode.appendChild(studentE);
//写到xml中
JaxpUtil.wirte2xml(document);
//更改result的取值为true
result = true;
return result;
}
/**
* 根据姓名删除信息
* @param name
* @return 成功返回true,否则false
*/
public boolean delete(String name){
boolean result = false;
try {
Document document = JaxpUtil.getDocument();
//得到所有的name元素
NodeList nl = document.getElementsByTagName("name");
//遍历:比对文本内容是否和参数一样
for(int i=0;i<nl.getLength();i++){
if(nl.item(i).getTextContent().equals(name)){
//如果找到了一样的:爷爷干掉爸爸
nl.item(i).getParentNode().getParentNode().removeChild(nl.item(i).getParentNode());
//写回xml
JaxpUtil.wirte2xml(document);
break;
}
}
result = true;
} catch (Exception e) {
throw new RuntimeException(e);//异常转译
}
return result;
}
/**
* 根据准考证号查询学生信息
* @param examid
* @return 没有返回null
*/
public Student findByExamId(String examid){
Student student = null;
try {
Document document = JaxpUtil.getDocument();
//得到所有的student元素
NodeList nl = document.getElementsByTagName("student");
//遍历:比对examid属性
for(int i=0;i<nl.getLength();i++){
Element e = (Element) nl.item(i);
if(e.getAttribute("examid").equals(examid)){
// 找到了:创建student对象,并设置相应的值
student = new Student();
student.setIdcard(e.getAttribute("idcard"));
student.setExamid(examid);
student.setName(e.getElementsByTagName("name").item(0).getTextContent());
student.setLocation(e.getElementsByTagName("location").item(0).getTextContent());
student.setGrade(Float.parseFloat(e.getElementsByTagName("grade").item(0).getTextContent()));
break;
}
}
} catch (Exception e) {
throw new RuntimeException(e);//异常转译
}
return student;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2