1. 程式人生 > >Java 操縱XML之修改XML文件

Java 操縱XML之修改XML文件

dom nsf class port 源代碼 and transform 輸出 system

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>

技術分享圖片

Java 操縱XML之修改XML文件