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()); } }