使用xml实现studentCRUD的小案例

前端之家收集整理的这篇文章主要介绍了使用xml实现studentCRUD的小案例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

环境:

myeclipse 10

window 10

导入包:dom4j-1.6.1.jar jaxen-1.1-beta-6.jar

代码实现:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <student>
  4. <stu>
  5. <id>100</id>
  6. <name>张三</name>
  7. <age>20</age>
  8. </stu>
  9. <stu>
  10. <id>101</id>
  11. <name>李四</name>
  12. <age>30</age>
  13. </stu>
  14. </student>
  1. package com.java.xml;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import java.util.List;
  8.  
  9. import org.dom4j.Document;
  10. import org.dom4j.DocumentException;
  11. import org.dom4j.Element;
  12. import org.dom4j.Node;
  13. import org.dom4j.io.OutputFormat;
  14. import org.dom4j.io.SAXReader;
  15. import org.dom4j.io.XMLWriter;
  16.  
  17. public class StudentService {
  18. /**
  19. * 增加 添加学生信息
  20. * @param student
  21. * @throws DocumentException
  22. * @throws IOException
  23. */
  24. public static void addStudent(Student student) throws DocumentException,IOException {
  25. /*
  26. * 1.创建解析器
  27. * 2.得到document
  28. * 3.获取根节点
  29. * 4.在根节点上面创建stu标签
  30. * 5.在stu标签上面创建id name age
  31. * 6.分别给id name age元素赋值
  32. * 7.回写xml
  33. */
  34. //创建解析器
  35. SAXReader saxReader = new SAXReader();
  36. //得到Document
  37. Document document = saxReader.read("src/student.xml");
  38. //获取根节点
  39. Element root = document.getRootElement();
  40. //在根节点上面添加stu标签
  41. Element stu = root.addElement("stu");
  42. //在stu上面添加id name age
  43. Element id = stu.addElement("id");
  44. Element name = stu.addElement("name");
  45. Element age = stu.addElement("age");
  46. //分别给id name age元素赋值
  47. id.setText(student.getId());
  48. name.setText(student.getName());
  49. age.setText(String.valueOf(student.getAge()));
  50. //回写xml
  51. OutputFormat format = OutputFormat.createPrettyPrint();
  52. XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"),format);
  53. xmlWriter.write(document);
  54. xmlWriter.close();
  55. }
  56. /**
  57. * 删除 根据id删除学生
  58. * @param id
  59. * @throws DocumentException
  60. * @throws IOException
  61. */
  62. public static void deleteStudent(String id) throws DocumentException,IOException {
  63. /*
  64. * 1.创建解析器
  65. * 2.得到document
  66. * 3.获取所有的id,使用xpath //id 返回 list集合
  67. * 4.遍历list集合
  68. * 5.判断集合里面的id和传递的id是否相同
  69. * 6.如果相同把id所在的stu删除
  70. * 7.回写xml
  71. */
  72. //创建解析器
  73. SAXReader saxReader = new SAXReader();
  74. //得到document
  75. Document document = saxReader.read("src/student.xml");
  76. //获取所有的id
  77. List<Node> list = document.selectNodes("//id");
  78. //遍历list集合
  79. for (Node node : list) {
  80. //node是每一个id的元素,得到id的值
  81. String value = node.getText();
  82. //判断value与传递的id是否相同
  83. if(value.equals(id)) {
  84. //id相同,得到stu标签
  85. Element stu = node.getParent();
  86. //获取stu的父节点
  87. Element student = stu.getParent();
  88. //删除stu
  89. student.remove(stu);
  90. }
  91. }
  92. //回写xml
  93. OutputFormat format = OutputFormat.createPrettyPrint();
  94. XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"),format);
  95. xmlWriter.write(document);
  96. xmlWriter.close();
  97. }
  98. /**
  99. * 查询 根据id查询学生信息
  100. * @param id
  101. * @throws DocumentException
  102. */
  103. public static Student getStudent(String id) throws DocumentException {
  104. /*
  105. * 1.创建解析器
  106. * 2.得到document
  107. * 3.获取所有的id,返回list集合
  108. * 4.遍历list集合
  109. * 5.得到每一个id的节点
  110. * 6.得到id的节点值
  111. * 7.判断id的值与传递的值是否相同
  112. * 8.如果相同,先获取到父节点stu
  113. * 9.通过stu获取到name,age
  114. */
  115. //创建解析器
  116. SAXReader saxReader = new SAXReader();
  117. //得到document
  118. Document document = saxReader.read("src/student.xml");
  119. //获取所有的id,得到集合list
  120. List<Node> list = document.selectNodes("//id");
  121. //创建Student对象
  122. Student student = new Student();
  123. //遍历集合list
  124. for (Node node : list) {
  125. //node是每一个id节点,得到id节点的值
  126. String value = node.getText();
  127. //判读值是否与传入的相同
  128. if(value.equals(id)) {
  129. //得到id的父节点
  130. Element stu = node.getParent();
  131. //通过stu获取name,age
  132. String name = stu.element("name").getText();
  133. String age = stu.element("age").getText();
  134. student.setId(value);
  135. student.setName(name);
  136. student.setAge(Integer.parseInt(age));
  137. }
  138. }
  139. return student;
  140. }
  141. }
  1. package com.java.xml;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.dom4j.DocumentException;
  6.  
  7. public class StudentCRUD {
  8.  
  9. public static void main(String[] args) throws DocumentException,IOException {
  10. // addStudent();
  11. // deleteStudent("102");
  12. getStudent("101");
  13. }
  14. public static void addStudent() throws DocumentException,IOException {
  15. Student student = new Student();
  16. student.setId("102");
  17. student.setName("王五");
  18. student.setAge(35);
  19. StudentService.addStudent(student);
  20. }
  21. public static void deleteStudent(String id) throws DocumentException,IOException {
  22. StudentService.deleteStudent(id);
  23. }
  24. public static void getStudent(String id) throws DocumentException {
  25. Student student = StudentService.getStudent(id);
  26. System.out.println(student);
  27. }
  28. }

猜你在找的XML相关文章