XML文檔讀取-DOM
阿新 • • 發佈:2017-11-14
rst i++ 刪除 接口 comment extc oid tag org
DOM(Document Object Model),“文檔對象模型”早期是為了解決不用瀏覽器間數據兼容問題提出的解決方案,現在已經是W3C組織推薦的處理可擴展標誌語言的標準編程接口。
W3C DOM 被分為 3 個不同的部分/級別(parts / levels):
- 核心 DOM:用於任何結構化文檔
- XML DOM:用於 XML 文檔的標準模型
- HTML DOM:用於 HTML 文檔的標準模型
XML DOM 是:
- 用於 XML 的標準對象模型
- 用於 XML 的標準編程接口
- 中立於平臺和語言
- W3C 的標準
SUN公司的JAXP(Java API for XML Processing)提供了對dom的支持;
其解析步驟為:
- 創建 DOM 解析器的工廠
- 得到 DOM 解析器對象。
對整個XML文檔進行操作
package hom; 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.TransformerFactoryConfigurationError; 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.w3c.dom.NodeList; import org.xml.sax.SAXException; public class first { /** * 輸出特定節點的文本信息 * * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ private static void JfindElement() throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();// ???? DocumentBuilder builder = factory.newDocumentBuilder();// ???? Document doc = builder.parse(new File("./WebRoot/xml/t6.xml"));// 獲取頁面 NodeList list = doc.getElementsByTagName("title");// 獲取tittle節點 System.out.println(list.getLength());// 輸出title節點長度 Node node = list.item(1);// 第二個節點 System.out.println(node.getTextContent());// 輸出第二個節點的文本信息 } // 輸出特定節點特定屬性的屬性值 private static void getAttribute() throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("./WebRoot/xml/t6.xml")); Element node = (Element) doc.getElementsByTagName("comment").item(1);// 獲取comment節點集合 System.out.println(node.getAttribute("title"));// 輸出node節點的tittle屬性的值 } // 輸出所有節點的節點名稱 private static void loopNode() throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("./WebRoot/xml/t6.xml")); loop(doc);// 函數調用輸出doc所有子孫節點 } // 遍歷所有子孫節點()輸出節點名稱 private static void loop(Node doc)// Document繼承自Node,,,此處為向上轉型 { NodeList list = doc.getChildNodes();// 獲取孩子節點的集合 // 便利孩子節點集合 // 輸出每個節點的標簽名 // 遞歸調用,輸出每一層的子節點 for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); System.out.println(node.getNodeName()); loop(node); } } // 創建新節點 private static void createElement() throws ParserConfigurationException, SAXException, IOException, TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("./WebRoot/xml/t6.xml"); // 創建結點 Element e = doc.createElement("language"); // 為新創建的節點添加內容 e.setTextContent("Chinese"); // 為新創建的節點添加屬性 e.setAttribute("aa", "xxx"); // 為新創建的節點獲取父節點,並append新創建的結點 doc.getElementsByTagName("books").item(1).appendChild(e); // 使用Transformer將內存中更新過的xml文檔寫入實際的xml文檔中 TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(doc), new StreamResult(new File( "./WebRoot/xml/t6.xml"))); } // 刪除節點 private static void deleteElement() throws ParserConfigurationException, SAXException, IOException, TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("./WebRoot/xml/t6.xml")); // 得到待刪除的結點 Element e = (Element) doc.getElementsByTagName("language").item(0); // 獲取結點的父節點,然後刪除該子結點 e.getParentNode().removeChild(e); // 使用Transformer將內存中更新過的xml文檔寫入實際的xml文檔中 TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(doc), new StreamResult(new File( "./WebRoot/xml/t6.xml"))); } public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("./WebRoot/xml/t6.xml")); /* * findElement(); getAttribute(); loopNode(); */ deleteElement(); } }
代碼解讀
這個類內含七個函數(包括五個自定義功能函數,一個遞歸函數體,一個main函數)
五個功能函數:
JfindElement():發現節點(輸出特定節點的文本內容)
private static void JfindElement() throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();// ???? DocumentBuilder builder = factory.newDocumentBuilder();// ???? Document doc = builder.parse(new File("./WebRoot/xml/t6.xml"));// 獲取頁面 NodeList list = doc.getElementsByTagName("title");// 獲取tittle節點 System.out.println(list.getLength());// 輸出title節點長度 Node node = list.item(1);// 第二個節點 System.out.println(node.getTextContent());// 輸出第二個節點的文本信息 }
getAttribute():獲得屬性值(輸出特定節點的屬性值)
private static void getAttribute() throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("./WebRoot/xml/t6.xml")); Element node = (Element) doc.getElementsByTagName("comment").item(1);// 獲取comment節點集合 System.out.println(node.getAttribute("title"));// 輸出node節點的tittle屬性的值 }
loop(Node doc):遞歸主函數體
private static void loop(Node doc)// Document繼承自Node,,,此處為向上轉型 { NodeList list = doc.getChildNodes();// 獲取孩子節點的集合 // 便利孩子節點集合 // 輸出每個節點的標簽名 // 遞歸調用,輸出每一層的子節點 for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); System.out.println(node.getNodeName()); loop(node); } }
loopNode()遞歸輸出所有節點
// 輸出所有節點的節點名稱 private static void loopNode() throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("./WebRoot/xml/t6.xml")); loop(doc);// 函數調用輸出doc所有子孫節點 }
createElement():
創建新節點
把新節點添加到父節點後面
更新文檔(把修改後的xml文檔同步到本地)
private static void createElement() throws ParserConfigurationException, SAXException, IOException, TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("./WebRoot/xml/t6.xml"); // 創建結點 Element e = doc.createElement("language"); // 為新創建的節點添加內容 e.setTextContent("Chinese"); // 為新創建的節點添加屬性 e.setAttribute("aa", "xxx"); // 為新創建的節點獲取父節點,並append新創建的結點 doc.getElementsByTagName("books").item(1).appendChild(e); // 使用Transformer將內存中更新過的xml文檔寫入實際的xml文檔中 TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(doc), new StreamResult(new File( "./WebRoot/xml/t6.xml"))); }
deleteElement():刪除節點
找到他的父節點
父節點刪除要刪除的節點
更新文檔(把修改後的xml文檔同步到本地)
private static void deleteElement() throws ParserConfigurationException, SAXException, IOException, TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("./WebRoot/xml/t6.xml")); // 得到待刪除的結點 Element e = (Element) doc.getElementsByTagName("language").item(0); // 獲取結點的父節點,然後刪除該子結點 e.getParentNode().removeChild(e); // 使用Transformer將內存中更新過的xml文檔寫入實際的xml文檔中 TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer tf = tfactory.newTransformer(); tf.transform(new DOMSource(doc), new StreamResult(new File( "./WebRoot/xml/t6.xml"))); }
XML文檔讀取-DOM