一:将内存中写好的xml文件读取到硬盘上
二:DOM方式对xml文件进行增删改查
1:添加节点(默认是在最后的子节点后面添加)
1 @Test 2 // 向元素中添加节点<version>1.0</version> 3 public void test5() throws Exception { 4 Document doc = getDoc(); 5 Node node = doc.getElementsByTagName("book").item(0); 6 7 // 创建节点 8 Element newChild = doc.createElement("version"); 9 newChild.setTextContent("1.3"); 10 // 添加节点 11 node.appendChild(newChild); 12 13 // 将内存中xml读取到硬盘中 14 TransformerFactory factory = TransformerFactory.newInstance(); 15 Transformer tf = factory.newTransformer(); 16 tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream( 17 "xml/books.xml"))); 18 }
2:在指定位置添加节点
1 @Test 2 // 2:在指定位置添加子节点 3 public void test6() throws Exception { 4 Document doc = getDoc(); 5 // 获取要添加节点的父节点 6 Node node = doc.getElementsByTagName("book").item(1); 7 8 // 获取参考节点 9 Node refNode = doc.getElementsByTagName("author").item(1); 10 11 // 创建节点 12 Node childNode = doc.createElement("version"); 13 childNode.setTextContent("2.34"); 14 15 // 将节点添加到指定位置 16 node.insertBefore(childNode, refNode); 17 18 // 将内存中xml读取到硬盘中 19 TransformerFactory factory = TransformerFactory.newInstance(); 20 Transformer tf = factory.newTransformer(); 21 tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream( 22 "xml/books.xml"))); 23 }
3:删除节点(方式一)
1 @Test 2 // 3:删除节点 3 public void test7() throws Exception { 4 Document doc = getDoc(); 5 // 获取要删除节点的父节点 6 Node node = doc.getElementsByTagName("book").item(1); 7 Node childNode = doc.getElementsByTagName("version").item(0); 8 node.removeChild(childNode); 9 10 // 将内存中xml读取到硬盘中 11 TransformerFactory factory = TransformerFactory.newInstance(); 12 Transformer tf = factory.newTransformer(); 13 tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream( 14 "xml/books.xml"))); 15 }
4:删除节点(方式二)
1 @Test 2 public void test8() throws Exception { 3 Document doc = getDoc(); 4 // 获取要删除的节点 5 Node childNode = doc.getElementsByTagName("version").item(0); 6 childNode.getParentNode().removeChild(childNode); 7 8 // 将内存中xml读取到硬盘中 9 TransformerFactory factory = TransformerFactory.newInstance(); 10 Transformer tf = factory.newTransformer(); 11 tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream( 12 "xml/books.xml"))); 13 }
5:修改节点内容
1 @Test 2 // 修改节点内容 3 public void test9() throws Exception { 4 Document doc = getDoc(); 5 // 获取要删除的节点 6 Node node = doc.getElementsByTagName("price").item(0); 7 node.setTextContent("200"); 8 9 // 将内存中xml读取到硬盘中 10 TransformerFactory factory = TransformerFactory.newInstance(); 11 Transformer tf = factory.newTransformer(); 12 tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream( 13 "xml/books.xml"))); 14 }
6:添加节点属性
1 @Test 2 // 添加节点属性 3 public void test10() throws Exception { 4 Document doc = getDoc(); 5 // 获取要删除的节点 6 Element ele = (Element) doc.getElementsByTagName("name").item(0); 7 ele.setAttribute("style", "80px;"); 8 9 // 将内存中xml读取到硬盘中 10 TransformerFactory factory = TransformerFactory.newInstance(); 11 Transformer tf = factory.newTransformer(); 12 tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream( 13 "xml/books.xml"))); 14 }
7:删除节点属性
1 @Test 2 // 删除节点属性 3 public void test11() throws Exception { 4 Document doc = getDoc(); 5 // 获取要删除属性的节点 6 Element ele = (Element) doc.getElementsByTagName("name").item(0); 7 ele.removeAttribute("style"); 8 9 // 将内存中xml读取到硬盘中 10 TransformerFactory factory = TransformerFactory.newInstance(); 11 Transformer tf = factory.newTransformer(); 12 tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream( 13 "xml/books.xml"))); 14 }