Java 操纵XML之修改XML文件
一、JAVA DOM PARSER
DOM interfaces
The DOM defines several Java interfaces. Here are the most common interfaces:
Node - The base datatype of the DOM.
Element - The vast majority of the objects you'll deal with are Elements.
Attr Represents an attribute of an element.
Text The actual content of an Element or Attr.
Document Represents the entire XML document. A Document object is often referred to as a DOM tree.
Common DOM methods
When you are working with the DOM, there are several methods you'll use often:
Document.getDocumentElement() - Returns the root element of the document.
Node.getFirstChild() - Returns the first child of a given Node.
Node.getLastChild() - Returns the last child of a given Node.
Node.getNextSibling() - These methods return the next sibling of a given Node.
Node.getPreviousSibling() - These methods return the previous sibling of a given Node.
Node.getAttribute(attrName) - For a given Node, returns the attribute with the requested name.
二、源代码:ModifyXmlFile.java
1 package cn.com.zfc.lesson26.xml; 2 3 import java.io.File; 4 5 import javax.xml.parsers.DocumentBuilder; 6 import javax.xml.parsers.DocumentBuilderFactory; 7 import javax.xml.transform.Transformer; 8 import javax.xml.transform.TransformerFactory; 9 import javax.xml.transform.dom.DOMSource; 10 import javax.xml.transform.stream.StreamResult; 11 12 import org.w3c.dom.Document; 13 import org.w3c.dom.Element; 14 import org.w3c.dom.NamedNodeMap; 15 import org.w3c.dom.Node; 16 17 /** 18 * 使用JAVA DOM PARSER:修改 XML 文件 19 * 20 * @author zfc 21 * @date 2017年12月7日 下午8:31:55 22 */ 23 public class ModifyXmlFile { 24 public static void main(String[] args) { 25 try { 26 String filePath = "I:\\code_workspace\\JavaSE_workspace\\JavaSE\\src\\cn\\com\\zfc\\lesson26\\xml\\ModifyXmlFile.xml"; 27 // 1、创建 File 对象,映射 XML 文件 28 File file = new File(filePath); 29 // 2、创建 DocumentBuilderFactory 对象,用来创建 DocumentBuilder 对象 30 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 31 // 3、创建 DocumentBuilder 对象,用来将 XML 文件 转化为 Document 对象 32 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 33 // 4、创建 Document 对象,解析 XML 文件 34 Document document = documentBuilder.parse(file); 35 // 修改第一个 student 36 // 5、获取第一个 student 结点 37 Node student = document.getElementsByTagName("student").item(0); 38 // 获取结点 student 结点的所有属性 39 NamedNodeMap namedNodeMap = student.getAttributes(); 40 // 获取界定 student 的 属性 id 41 Node id = namedNodeMap.getNamedItem("id"); 42 // 给属性 id 重新设置值 43 id.setTextContent("student11"); 44 // 6、获取根结点 students 的第一个直接子结点 student 45 student = document.getElementsByTagName("student").item(0); 46 Element studentElement = (Element) student; 47 // 7、获取结点 student 的直接子结点 name、sex 48 Node name = studentElement.getElementsByTagName("name").item(0); 49 Node sex = studentElement.getElementsByTagName("sex").item(0); 50 Element nameElement = (Element) name; 51 Element sexElement = (Element) sex; 52 // 8、给节点进行设置值 53 nameElement.setTextContent("TomTom"); 54 sexElement.setTextContent("FemaleFemale"); 55 // 9、创建 TransformerFactory 对象 56 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 57 // 10、创建 Transformer 对象 58 Transformer transformer = transformerFactory.newTransformer(); 59 // 11、创建 DOMSource 对象 60 DOMSource domSource = new DOMSource(document); 61 // 12、创建 StreamResult 对象 62 StreamResult reStreamResult = new StreamResult(file); 63 transformer.transform(domSource, reStreamResult); 64 65 // 输出测试结果 66 StreamResult consoleResult = new StreamResult(System.out); 67 transformer.transform(domSource, consoleResult); 68 69 } catch (Exception e) { 70 e.printStackTrace(); 71 } 72 } 73 }
ModifyXmlFile.xml
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 2 <students> 3 <student id="student1"> 4 <name>Tom</name> 5 <sex>Female</sex> 6 </student> 7 <student id="student2"> 8 <name>Lucy</name> 9 <sex>Male</sex> 10 </student> 11 </students>
三、运行效果:
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?><students> 2 <student id="student11"> 3 <name>TomTom</name> 4 <sex>FemaleFemale</sex> 5 </student> 6 <student id="student2"> 7 <name>Lucy</name> 8 <sex>Male</sex> 9 </student> 10 </students>