本例以Windows, JDK1.7 为基础,来演示使用Java 采用DOM方式来修改XML文件。
XML文件
将下列的XML文件进行修改,主要包括修改id属性的值,添加一个address节点,删除age节点,同时更新performance节点的值。
修改前XML文件:
1 2 3 4 5 6 7 8
| <?xml version="1.0" encoding="UTF-8" standalone="no"?> <Class> <Student id="1"> <name>duliu</name> <age>26</age> <performance>99</performance> </Student> </Class>
|
修改后XML文件:
1 2 3 4 5 6 7 8
| <?xml version="1.0" encoding="UTF-8" standalone="no"?> <Class> <Student id="2"> <name>duliu</name> <performance>100</performance> <address>China</address> </Student> </Class>
|
示例代码
ModifyXML.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| package com.devnp.xml.dom;
import java.io.File; import java.io.IOException;
import javax.xml.parsers.DocumentBuilder; 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.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException;
public class ModifyXML {
public static void main(String[] args) { String filepath = "D:\\Person-Test\\temp\\class.xml" ; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = null; try { docBuilder = docFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } Document doc = null ; try { doc = docBuilder.parse(filepath); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Node student = doc.getElementsByTagName("Student").item(0); NamedNodeMap attr = student.getAttributes(); Node nodeAttr = attr.getNamedItem("id"); nodeAttr.setTextContent("2"); Element age = doc.createElement("address"); age.appendChild(doc.createTextNode("China")); student.appendChild(age); NodeList list = student.getChildNodes(); for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if ("performance".equals(node.getNodeName())) { node.setTextContent("100"); }
if ("age".equals(node.getNodeName())) { student.removeChild(node); }
} TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = transformerFactory.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(filepath)); try { transformer.transform(source, result); } catch (TransformerException e) { e.printStackTrace(); }
System.out.println("Update XML with DOM is Success."); }
}
|
相关
关于使用DOM方式来创建XML文件 :
Java XML DOM 创建XML文件
Author:
Darren Du
License:
Copyright (c) 2019 MIT LICENSE