Dom4j 操作, 节点查找 添加 删除 修改 。。。xPath

转:

 

Dom4j 操作, 节点查找 添加 删除 修改 。。。xPath

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xj626852095/article/details/17002321

Dom4j 说明文档:http://blog.csdn.net/xj626852095/article/details/17000741

xPath说明文档: http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html

要解析的xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <书架>
  4. <name="yyyyyyy">
  5. <书名>Java</书名>
  6. <作者>张三</作者>
  7. <打折价>9.9折</打折价>
  8. <售价>只要998</售价>
  9. </书>
  10. <memony="有优惠价">
  11. <书名 name="xxxxx">JavaScript网页开发</书名>
  12. <作者>李四</作者>
  13. <售价>28.00元</售价>
  14. <优惠价>8折</优惠价>
  15. <优惠价>8折</优惠价>
  16. <优惠价>8折</优惠价>
  17. </书>
  18. <user username="kevin" passwd="123456"/>
  19. <user username="xiang" passwd="234567"/>
  20. </书架>

 

  1. package com.kevin.java;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.io.OutputStreamWriter;
  10. import java.util.List;
  11.  
  12. import org.dom4j.Document;
  13. import org.dom4j.DocumentException;
  14. import org.dom4j.DocumentHelper;
  15. import org.dom4j.Element;
  16. import org.dom4j.Node;
  17. import org.dom4j.io.OutputFormat;
  18. import org.dom4j.io.SAXReader;
  19. import org.dom4j.io.XMLWriter;
  20. import org.junit.Test;
  21. import org.xml.sax.DocumentHandler;
  22.  
  23. public class Dom4j_Test {
  24.  
  25. //Xpath使用
  26. @Test
  27. public void test4() throws DocumentException, IOException {
  28. SAXReader reader = new SAXReader();
  29. Document document = reader.read(new File("src/book1.xml"));
  30. String bookName = document.selectSingleNode("//书名").getText();
  31. System.out.println("bookName: " + bookName);
  32.  
  33. //查找符合条件的User //BBB[@id='b1'] 注意要有单引号
  34. String username = "kevin";
  35. String passwd = "123456";
  36. Node node = document.selectSingleNode
  37. ("//user[@username='"+username+"' and @passwd='"+passwd+"']");
  38. if(node==null)
  39. System.out.println("不是注册用户");
  40. else
  41. System.out.println("注册用户");
  42.  
  43. }
  44.  
  45. //获得第二本数的书名, 属性值 , 添加售价节点
  46. @Test
  47. public void test1() throws DocumentException, IOException {
  48. SAXReader reader = new SAXReader();
  49. Document document = reader.read(new File("src/book1.xml"));
  50. Element root = document.getRootElement();
  51. Element book = root.elements("书").get(1);
  52. String bookName = book.element("书名").getText();
  53. String bookAttr = book.element("书名").attributeValue("name");
  54. System.out.println("bookName: " + bookName);
  55. System.out.println("bookAttr: " + bookAttr);
  56.  
  57. //添加售价节点
  58. book.addAttribute("memony", "有优惠价");
  59. book.addElement("优惠价").setText("8折");
  60. OutputFormat format = OutputFormat.createPrettyPrint();
  61. format.setEncoding("UTF-8");
  62. XMLWriter writer = new XMLWriter(
  63. new OutputStreamWriter(new FileOutputStream("src/book1.xml")), format);
  64. writer.write(document);
  65. writer.close();
  66. }
  67.  
  68. //指定位置添加
  69. @Test
  70. public void test2() throws DocumentException, IOException {
  71. SAXReader reader = new SAXReader();
  72. Document document = reader.read(new File("src/book1.xml"));
  73. Element book = document.getRootElement().element("书");
  74. List list = book.elements();
  75.  
  76. Element price = DocumentHelper.createElement("打折价");
  77. price.setText("9.9折");
  78. list.add(2, price);
  79.  
  80. OutputFormat format = OutputFormat.createPrettyPrint();
  81. format.setEncoding("UTF-8");
  82. XMLWriter writer = new XMLWriter(
  83. new OutputStreamWriter(new FileOutputStream("src/book1.xml")), format);
  84. writer.write(document);
  85. writer.close();
  86. }
  87.  
  88. //删除节点 更新节点
  89. @Test
  90. public void test3() throws DocumentException, IOException {
  91. SAXReader reader = new SAXReader();
  92. Document document = reader.read(new File("src/book1.xml"));
  93. Element priceTmp = document.getRootElement().element("书").element("打折价");
  94. //删除节点
  95. priceTmp.getParent().remove(priceTmp);
  96.  
  97. Element price = document.getRootElement().element("书").element("售价");
  98. //更新节点
  99. price.setText("只要998");
  100.  
  101. OutputFormat format = OutputFormat.createPrettyPrint();
  102. format.setEncoding("UTF-8");
  103. XMLWriter writer = new XMLWriter(
  104. new OutputStreamWriter(new FileOutputStream("src/book1.xml")), format);
  105. writer.write(document);
  106. writer.close();
  107. }
  108.  
posted @ 2018-12-14 17:06  戈博折刀  阅读(450)  评论(0编辑  收藏  举报