jdom 插入 修改 删除

创建XML文档

  XML文件是一种典型的树形文件,每个文档元素都是一个document元素的子节点。而每个子元素都是一个Element对象,对象可以向下包含。

  1 因此我们可以通过先创建元素再将元素添加到父元素中,最后将顶层元素添加到根元素中。

  2 创建完文档元素后,就可以把元素添加到document对象中,然后写入文件。

  主要使用的函数:

Element.setAttribute 为元素添加信息

Element.addContent(String,String) 为元素添加子元素内容,也可以直接添加另一个元素节点

Document.setRootElement(Element) 为文档添加根元素

XMLOutputter.output(Document,FileWriter) 将Docuemnt写入到FileWriter文件流中


  1. @SuppressWarnings("null")
  2. 2     public static void createXML() {
  3. 3         // 创建document
  4. 4         Document mydoc = new Document();
  5. 5
  6. 6         // 创建元素person1
  7. 7         Element person1 = new Element("person");
  8. 8         person1.setAttribute("id", "ID001");
  9. 9         // 添加注释
  10. 10         person1.addContent(new Comment("this is person1"));
  11. 11
  12. 12         person1.addContent(new Element("name").setText("xingoo"));
  13. 13         person1.addContent(new Element("age").setText("25"));
  14. 14         person1.addContent(new Element("sex").setText("M"));
  15. 15         // 可以嵌套添加子元素
  16. 16         Element address1 = new Element("address");
  17. 17         address1.setAttribute("zone", "province");
  18. 18         address1.addContent("LiaoNing");
  19. 19         person1.addContent(address1);
  20. 20
  21. 21         // 创建元素person2
  22. 22         Element person2 = new Element("person");
  23. 23         person2.setAttribute("id", "ID002");
  24. 24         // 添加注释
  25. 25         person2.addContent(new Comment("this is person2"));
  26. 26
  27. 27         person2.addContent(new Element("name").setText("xhalo"));
  28. 28         person2.addContent(new Element("age").setText("26"));
  29. 29         person2.addContent(new Element("sex").setText("M"));
  30. 30         // 可以嵌套添加子元素
  31. 31         Element address2 = new Element("address");
  32. 32         address2.setAttribute("zone", "province");
  33. 33         address2.addContent("JiLin");
  34. 34         person2.addContent(address2);
  35. 35
  36. 36         // 在doc中添加元素Person
  37. 37         Element info = new Element("information");
  38. 38         info.addContent(person1);
  39. 39         info.addContent(person2);
  40. 40         mydoc.setRootElement(info);
  41. 41        
  42. 42         saveXML(mydoc);
  43. 43     }

  44. saveXML()代码:

  45. 1     public static void saveXML(Document doc) {
  46. 2         // 将doc对象输出到文件
  47. 3         try {
  48. 4             // 创建xml文件输出流
  49. 5             XMLOutputter xmlopt = new XMLOutputter();
  50. 6
  51. 7             // 创建文件输出流
  52. 8             FileWriter writer = new FileWriter("person.xml");
  53. 9
  54. 10             // 指定文档格式
  55. 11             Format fm = Format.getPrettyFormat();
  56. 12             // fm.setEncoding("GB2312");
  57. 13             xmlopt.setFormat(fm);
  58. 14
  59. 15             // 将doc写入到指定的文件中
  60. 16             xmlopt.output(doc, writer);
  61. 17             writer.close();
  62. 18         } catch (Exception e) {
  63. 19             e.printStackTrace();
  64. 20         }
  65. 21     }

 



 执行后,刷新项目,就可以在项目下看到person.xml文件了


修改同理


删除 :


  1.     public static void removeXML() {
  2.         SAXBuilder sb = new SAXBuilder();
  3.         Document doc = null;
  4.         try {
  5.             doc = sb.build("person.xml");
  6.             Element root = doc.getRootElement();
  7.             List<Element> list = root.getChildren("person");
  8.             for (Element el : list) {
  9.                 if (el.getAttributeValue("id").equals("ID001")) {
  10.                     root.removeContent(el);
  11.                 }
  12.             }
  13.         } catch (Exception e) {
  14.             e.printStackTrace();
  15.         }
  16.         saveXML(doc);
  17.     }




原文地址:https://www.cnblogs.com/signheart/p/6597974.html