package com.lee.dao;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.lee.domain.Student;
import com.lee.exception.StudentNotExistException;
import com.lee.xmlutils.XmlUtils;
public class StudentDao {
// 按姓名添加学生
public void add(Student s){
Document document ;
try {
document = XmlUtils.getDocument();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
// 创建自定义的封装的标签
Element student_tag = document.createElement("student");
student_tag.setAttribute("examid", s.getExamid());
student_tag.setAttribute("idcard", s.getIdcard());
Element name = document.createElement("name");
// name.setNodeValue(s.getName());
name.setTextContent(s.getName());
Element location = document.createElement("location");
// location.setNodeValue(s.getLocation());
location.setTextContent(s.getLocation());
Element grade = document.createElement("grade");
// grade.setNodeValue(Double.parseDouble(s.getGrade()));
grade.setTextContent(String.valueOf(s.getGrade()));
student_tag.appendChild(name);
student_tag.appendChild(location);
student_tag.appendChild(grade);
document.getElementsByTagName("exam").item(0).appendChild(student_tag);
try {
XmlUtils.write2Xml(document);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
// 按身份证号删除学生
// 从name标签中查找遍历,是否存在于与传过来的参数一致的值
// 找到后,因为idcard是student的子节点,student是exam的子节点,需要删除的是student
// 因此,拿到idcard的父节点的父节点exam,删除子节点student
public void delete(String idcard) throws StudentNotExistException{
Document document ;
boolean isDelete = false;
try {
document = XmlUtils.getDocument();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException();
}
// nodeList是得到所有的name标签的集合
// nodeList.item是从name标签的集合中取出第几个
// nodeList.item.getTextContent拿到此节点的文本内容<name>文本内容</name>
// 如何同时删除多个数据?????如果删除后,找不到idcard了呢,此时出错??????
// 删除的方法出错了
// 找到错误的原因:
// nodeList原集合中有5个元素,a,b,c,d,e
// 遍历nodeList.item(i)得到student_tag
// 1.1 当nodeList.item(0)时,删除5个元素中的第1个元素a,剩余b,c,d,e
// 1.2 当nodeList.item(1)时,删除4个元素中的第2个元素c,剩余b,d,e
// 由上可以发现,遍历的时候,应该需要减去被删除的个数
// 即修改的正确方式为:
// nodeList.item(i - j)得到student_tag
// 1.1 当nodeList.item(0 - 0)时,删除5个元素中的第1个元素a,剩余b,c,d,e
// 因为删除掉一个元素,所以j++
// 1.2 当nodeList.item(1 - 1)时,删除4个元素中的第1个元素b,剩余c,d,e
// 于是不会出现空指针,修正了上述的错误
NodeList nodeList = document.getElementsByTagName("student");
for(int i = 0, len = nodeList.getLength(), j = 0; i < len; i++){
System.out.println("Length(): " + nodeList.getLength());
System.out.println("i: " + i);
System.out.println("i - j: " + (i - j));
Element student_tag = (Element) nodeList.item(i - j);
System.out.println(student_tag.getAttribute("idcard"));
if(student_tag.getAttribute("idcard").equals(idcard)){
student_tag.getParentNode().removeChild(nodeList.item(i - j));
isDelete = true;
j++;
}
}
if(isDelete){
try {
XmlUtils.write2Xml(document);
return;
}catch (StudentNotExistException e) {
// TODO Auto-generated catch block
throw e;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//
// if(isDelete){
// try {
// XmlUtils.write2Xml(document);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// throw new RuntimeException(e);
// }
// }else{
// try {
// throw new StudentNotExistException(idcard + "不存在");
// } catch (StudentNotExistException e) {
// // TODO Auto-generated catch block
// throw e;
// }
// }
}
public void delete2(String idcard) throws StudentNotExistException{
Document document ;
boolean isDelete = false;
try {
document = XmlUtils.getDocument();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException();
}
// nodeList是得到所有的name标签的集合
// nodeList.item是从name标签的集合中取出第几个
// nodeList.item.getTextContent拿到此节点的文本内容<name>文本内容</name>
NodeList nodeList = document.getElementsByTagName("name");
for(int i = 0, len = nodeList.getLength(); i < len; i++){
if(nodeList.item(i).getTextContent().equals(idcard)){
nodeList.item(i).getParentNode().getParentNode().removeChild(nodeList.item(i).getParentNode());
isDelete = true;
}
}
if(isDelete){
try {
XmlUtils.write2Xml(document);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}else{
try {
throw new StudentNotExistException(idcard + "不存在");
} catch (StudentNotExistException e) {
// TODO Auto-generated catch block
throw e;
}
}
}
// 按考试编号查询学生
public Student find(String examid){
Document document ;
try {
document = XmlUtils.getDocument();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
// nodeList.itemt().getAttributes(),无法获得指定的属性
// 所以要用element.item().getAttributes("指定属性")
// 记忆练习:从节点中拿到指定的子节点,的文本内容
// 记忆练习:从节点中拿到指定的属性值
NodeList nodeList = document.getElementsByTagName("student");
for(int i = 0, len = nodeList.getLength(); i < len; i++){
Element student_tag = (Element) nodeList.item(i);
if(student_tag.getAttribute("examid").equals(examid)){
Student s = new Student();
s.setExamid(examid);
s.setGrade(Double.parseDouble(student_tag.getElementsByTagName("grade").item(0).getTextContent()));
s.setIdcard(student_tag.getAttribute("idcard"));
s.setLocation(student_tag.getElementsByTagName("location").item(0).getTextContent());
s.setName(student_tag.getElementsByTagName("name").item(0).getTextContent());
return s;
}
}
return null;
}
public void update(){
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>
<student examid="121" idcard="121">
<name>aa</name>
<location>北京</location>
<grade>98.0</grade>
</student>
<student examid2="111" idcard="333"><name>a</name><location>bj</location><grade>222.0</grade></student>
<student examid2="111" idcard="333"><name>a</name><location>bj</location><grade>222.0</grade></student>
<student examid2="111" idcard="333"><name>a</name><location>bj</location><grade>222.0</grade></student>
<student examid="111" idcard="2"><name>a</name><location>bj</location><grade>222.0</grade></student>
<student examid2="111" idcard="333"><name>a</name><location>bj</location><grade>222.0</grade></student>
</exam>
package com.lee.test;
import org.junit.Test;
import com.lee.dao.StudentDao;
import com.lee.domain.Student;
import com.lee.exception.StudentNotExistException;
public class StudentDaoTest {
@Test
public void addTest(){
StudentDao dao = new StudentDao();
Student s = new Student();
String examid = "111";
double grade = 222;
String idcard = "333";
String location = "bj";
String name = "a";
s.setExamid(examid);
s.setGrade(grade);
s.setIdcard(idcard);
s.setLocation(location);
s.setName(name);
dao.add(s);
}
@Test
public void deleteTest() throws StudentNotExistException{
StudentDao dao = new StudentDao();
String idcard = "333";
dao.delete(idcard);
}
@Test
public void findTest(){
StudentDao dao = new StudentDao();
String examid = "1";
Student s = dao.find(examid);
System.out.println(s.getExamid());
}
}
分享到:
相关推荐
jdom处理xml例子
mybatis中文文档与xml例子
DOM解析XML文件例子DOM解析XML文件例子DOM解析XML文件例子
使用jaxb生成XML例子,含有例子和注解解析
java程序解析xml的例子
解析XML文件例子
mfc 操作 xml 小例子mfc 操作 xml 小例子mfc 操作 xml 小例子mfc 操作 xml 小例子
vc6.0下的 tinyxml使用例子,附带tinyxml源代码,简单易学,一看就明白哦
Xml转换为集合list实例代码.rar Xml转换为集合list实例代码.rar Xml转换为集合list实例代码.rar
这个是xml用书的完全例子,里面包含了书上的所有例子,方便读者更好的学习xml
XML语法规则,例子简单XML文件 XML 结构 XML 文档形成了一种树结构,它从“根部”开始,然后扩展到“枝叶”。
xml和xsl的练习例子xml和xsl的练习例子xml和xsl的练习例子xml和xsl的练习例子xml和xsl的练习例子xml和xsl的练习例子
程序员用得上的测试例子,简单实用xml例子
dtd例子xmldtd
<P>map集合转化成xml字符串</P> <P>xml字符串转化成map集合 <P>list集合转化成xml字符串 <P>xml字符串转化成list集合</P>
C#例子代码 A0200_XMLC#例子代码 A0200_XMLC#例子代码 A0200_XMLC#例子代码 A0200_XMLC#例子代码 A0200_XMLC#例子代码 A0200_XMLC#例子代码 A0200_XMLC#例子代码 A0200_XMLC#例子代码 A0200_XMLC#例子代码 A0200_XML...
Android 使用 XML 菜单例子,更多例子自己到http://bbs.droidstouch.com下载。都是不要积分的
php 与XML的例子 一些基本的应用 例子比较简单
xml的多个例子,以前下载的可惜没有用到。
一个公认的实事,即关系型数据库在结构化的集合处理方面表现优秀,而数据库与编程语言之间通常通过各种参数来进行交互,而XML刚好是它们之间的一个桥梁,把编程语中向面对象的数据序列化为XML,传递到数据库,数据库...