上一篇文章我介绍了SAX方法解析XML文件的过程,这篇文章讲解的内容是利用DOM方法修改XML文件的节点内容。
下面依然是首先贴出XML文件:
<?xml version="1.0" encoding="UTF-8"?> <EPClientDemo> <WebService_IP>172.16.6.17</WebService_IP> <WebService_PORT>8080</WebService_PORT> <Dest>最高人民检查院 </Dest> <RootPath_Receive>E:\rec</RootPath_Receive> <Receiver>测试 </Receiver> </EPClientDemo>
好了,不多说废话了,直接贴出解析类,说明我在这里就不写了,代码中都有
package com.brucezhang.xmltest; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.xml.sax.SAXException; public class MyDom_Update { /** * @param index 修改值的标签索引 * @param count 修改的是第几个值 * @param value 修改的目标值 * @param outOrno 是否输出XML文件内容 * * @author DLUTBruceZhang * */ public void update(String index, int count, String value, boolean outOrno) { Document document = null; try { document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("my_xml.xml"); Element root = document.getDocumentElement(); root.getElementsByTagName(index).item(count).setTextContent(value); output(root, "my_xml.xml"); if (outOrno) { System.out.println("-------------------------使用DOM方法输出-------------------------"); output(root, null); } } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //写回XML文件,保存修改或打印到控制台 public static void output(Node node, String filename) { TransformerFactory transFactory = TransformerFactory.newInstance(); try { Transformer transformer = transFactory.newTransformer(); // 设置各种输出属性 transformer.setOutputProperty("encoding", "gb2312"); transformer.setOutputProperty("indent", "yes"); DOMSource source = new DOMSource(); // 将待转换输出节点赋值给DOM源模型的持有者(holder) source.setNode(node); StreamResult result = new StreamResult(); if (filename == null) { // 设置标准输出流为transformer的底层输出目标 result.setOutputStream(System.out); } else { result.setOutputStream(new FileOutputStream(filename)); } // 执行转换从源模型到控制台输出流 transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
package com.brucezhang.xmltest; public class Main_Test { /** * @param args * Author:DLUTBruceZhang */ public static void main(String[] args){ // TODO Auto-generated method stub //修改XML文件 // 目标文件 // <EPClientDemo> // <WebService_IP>201.55.46.79</WebService_IP> // <WebService_PORT>3389</WebService_PORT> // <Dest>DLUT</Dest> // <RootPath_Receive>F:\rec</RootPath_Receive> // <Receiver>研发 </Receiver> // </EPClientDemo> update(); } public static void update(){ MyDom_Update mUpdate = new MyDom_Update(); System.out.println("\n\n尝试修改节点内容中。。。"); mUpdate.update("WebService_IP", 0, "201.55.46.79", false); mUpdate.update("WebService_PORT", 0, "3389", false); mUpdate.update("Dest", 0, "DLUT", false); mUpdate.update("RootPath_Receive", 0, "F:\\rec", false); mUpdate.update("Receiver", 0, "研发", true); System.out.println("修改节点内容成功。。。"); } }
尝试修改节点内容中。。。 -------------------------使用DOM方法输出------------------------- <?xml version="1.0" encoding="GB2312"?> <EPClientDemo> <WebService_IP>201.55.46.79</WebService_IP> <WebService_PORT>3389</WebService_PORT> <Dest>DLUT</Dest> <RootPath_Receive>F:\rec</RootPath_Receive> <Receiver>研发</Receiver> </EPClientDemo> 修改节点内容成功。。。