(补)Java解析XML之dom4j
上次记得忘了记最流行的dom4j方法了,用的还是上次的那个XML文件
注:需要添加dom4j.jar文件
1 package com.xujingyang.dom4j; 2 3 4 import org.dom4j.Document; 5 import org.dom4j.DocumentHelper; 6 import org.dom4j.Element; 7 import org.dom4j.Node; 8 import org.junit.Test; 9 10 import com.xujingyang.utils.dom4jUtil; 11 12 public class dom4jTest { 13 14 /** 15 * 获取指定节点的内容 16 */ 17 @Test 18 public void testDom4j() { 19 try { 20 Document document = dom4jUtil.getDocument("src/books.xml"); 21 // 获取根节点 22 Element rootElement = document.getRootElement(); 23 // 获取根节点的子节点集合的第一个节点 24 Element element = (Element) rootElement.elements().get(0); 25 // 获取对应子节点的文本 26 String text = element.element("price").getText(); 27 System.out.println(text); 28 } catch (Exception e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 } 33 34 //==================递归遍历所有子节点的文本开始====================================== 35 @Test 36 public void test2() { 37 try { 38 Element rootElement = dom4jUtil.getDocument("src/books.xml") 39 .getRootElement(); 40 treeWalk(rootElement); 41 } catch (Exception e) { 42 // TODO Auto-generated catch block 43 e.printStackTrace(); 44 } 45 } 46 47 public static void treeWalk(Element element) { 48 //获取子节点个数 49 int nodeCount = element.nodeCount(); 50 51 //遍历子节点 52 for (int i = 0; i < nodeCount; i++) { 53 //获取第i个子节点 54 Node node = element.node(i); 55 if (node instanceof Element) { 56 //如果仍然是节点就递归 57 treeWalk((Element) node); 58 } else { 59 //不是就打印节点内容 60 String text = node.getStringValue(); 61 System.out.println(text); 62 } 63 } 64 } 65 66 //===================递归遍历所有子节点的文本结束================================================ 67 68 /** 69 * 添加新子节点 70 */ 71 @Test 72 public void test3(){ 73 try { 74 Document document = dom4jUtil.getDocument("src/books.xml"); 75 // 获取根节点 76 Element rootElement = document.getRootElement(); 77 Element element = (Element) rootElement.elements("book").get(0); 78 //添加子节点信息 79 element.addElement("特价").setText("21"); 80 //写入源文件 81 dom4jUtil.writeDocument(document); 82 83 84 } catch (Exception e) { 85 // TODO Auto-generated catch block 86 e.printStackTrace(); 87 } 88 } 89 90 /** 91 * 添加同级新节点 92 */ 93 @Test 94 public void test4(){ 95 try { 96 Document document = dom4jUtil.getDocument("src/books.xml"); 97 // 获取根节点 98 Element rootElement = document.getRootElement(); 99 Element element = (Element) rootElement.elements("book").get(0); 100 101 Element createElement = DocumentHelper.createElement("新价"); 102 createElement.setText("31"); 103 //向子节点的指定位置添加新节点 104 element.elements().add(2, createElement); 105 //写入源文件 106 dom4jUtil.writeDocument(document); 107 108 109 } catch (Exception e) { 110 // TODO Auto-generated catch block 111 e.printStackTrace(); 112 } 113 } 114 115 /** 116 * 删除指定的节点 117 */ 118 @Test 119 public void test5(){ 120 try { 121 Document document = dom4jUtil.getDocument("src/books.xml"); 122 // 获取根节点 123 Element rootElement = document.getRootElement(); 124 //找到子节点 125 Element element = (Element) rootElement.elements("book").get(0); 126 //找到要删除的节点 127 Element tejia = element.element("特价"); 128 //删除 129 element.remove(tejia); 130 //写入文件 131 dom4jUtil.writeDocument(document); 132 } catch (Exception e) { 133 // TODO Auto-generated catch block 134 e.printStackTrace(); 135 } 136 } 137 138 139 140 }
程序用到的工具类:
1 package com.xujingyang.utils; 2 3 import java.io.FileOutputStream; 4 5 import org.dom4j.Document; 6 import org.dom4j.io.OutputFormat; 7 import org.dom4j.io.SAXReader; 8 import org.dom4j.io.XMLWriter; 9 10 public class dom4jUtil { 11 12 /** 13 * 得到Document对象 14 * 15 * @param path 16 * @return 17 * @throws Exception 18 */ 19 public static Document getDocument(String path) throws Exception { 20 SAXReader reader = new SAXReader(); 21 return reader.read(path); 22 } 23 24 /** 25 * 写入XML 26 * @param document 27 * @throws Exception 28 */ 29 public static void writeDocument(Document document) throws Exception { 30 OutputFormat format = OutputFormat.createPrettyPrint(); 31 format.setEncoding("UTF-8"); 32 XMLWriter xmlWriter = new XMLWriter(new FileOutputStream( 33 "src/books.xml"), format); 34 xmlWriter.write(document); 35 } 36 }