您好,登錄后才能下訂單哦!
本文研究的主要是java使用DOM對XML文檔進行增刪改查操作的相關代碼,具體實例如下所示。
源代碼:
package com.zc.homeWork18; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class XMLWriter { private static String xmlPath = "src\\com\\zc\\homeWork18\\MyXml.xml"; public static void getFamilyMemebers() { /* * 創建文件工廠實例 */ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 如果創建的解析器在解析XML文檔時必須刪除元素內容中的空格,則為true,否則為false dbf.setIgnoringElementContentWhitespace(true); try { /* * 創建文件對象 */ DocumentBuilder db = dbf.newDocumentBuilder();// 創建解析器,解析XML文檔 Document doc = db.parse(xmlPath); // 使用dom解析xml文件 /* * 歷遍列表,進行XML文件的數據提取 */ // 根據節點名稱來獲取所有相關的節點 NodeList sonlist = doc.getElementsByTagName("son"); for (int i = 0; i < sonlist.getLength(); i++) // 循環處理對象 { // 節點屬性的處理 Element son = (Element) sonlist.item(i); // 循環節點son內的所有子節點 for (Node node = son.getFirstChild(); node != null; node = node .getNextSibling()) { // 判斷是否為元素節點 if (node.getNodeType() == Node.ELEMENT_NODE) { String name = node.getNodeName(); String value = node.getFirstChild().getNodeValue(); System.out.println(name + " : " + value); } } } } catch (Exception e) { System.out.println(e.getMessage()); } } // 修改 public static void modifySon() { // 創建文件工廠實例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); try { // 從XML文檔中獲取DOM文檔實例 DocumentBuilder db = dbf.newDocumentBuilder(); // 獲取Document對象 Document xmldoc = db.parse(xmlPath); // 獲取根節點 Element root = xmldoc.getDocumentElement(); // 定位id為001的節點 Element per = (Element) selectSingleNode("/father/son[@id='001']", root); // 將age節點的內容更改為28 per.getElementsByTagName("age").item(0).setTextContent("28"); // 保存 TransformerFactory factory = TransformerFactory.newInstance(); Transformer former = factory.newTransformer(); former.transform(new DOMSource(xmldoc), new StreamResult(new File( xmlPath))); } catch (Exception e) { System.out.println(e.getMessage()); } } // 獲取目標節點,進行刪除,最后保存 public static void discardSon() { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); try { DocumentBuilder db = dbf.newDocumentBuilder(); Document xmldoc = db.parse(xmlPath); // 獲取根節點 Element root = xmldoc.getDocumentElement(); // 定位根節點中的id=002的節點 Element son = (Element) selectSingleNode("/father/son[@id='002']", root); // 刪除該節點 root.removeChild(son); // 保存 TransformerFactory factory = TransformerFactory.newInstance(); Transformer former = factory.newTransformer(); former.transform(new DOMSource(xmldoc), new StreamResult(new File( xmlPath))); } catch (Exception e) { System.out.println(e.getMessage()); } } // 新增節點 public static void createSon() { // 創建文件工廠實例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(false); try { DocumentBuilder db = dbf.newDocumentBuilder(); // 創建Document對象 Document xmldoc = db.parse(xmlPath); // 獲取根節點 Element root = xmldoc.getDocumentElement(); // 創建節點son,設置對應的id為004 Element son = xmldoc.createElement("son"); son.setAttribute("id", "004"); // 創建節點name Element name = xmldoc.createElement("name"); name.setTextContent("小兒子"); son.appendChild(name); // 創建節點age Element age = xmldoc.createElement("age"); age.setTextContent("0"); son.appendChild(age); // 把son添加到根節點中 root.appendChild(son); // 保存 TransformerFactory factory = TransformerFactory.newInstance(); Transformer former = factory.newTransformer(); former.transform(new DOMSource(xmldoc), new StreamResult(new File( xmlPath))); } catch (Exception e) { System.out.println(e.getMessage()); } } // 修改節點信息 public static Node selectSingleNode(String express, Element source) { Node result = null; //創建XPath工廠 XPathFactory xpathFactory = XPathFactory.newInstance(); //創建XPath對象 XPath xpath = xpathFactory.newXPath(); try { result = (Node) xpath.evaluate(express, source, XPathConstants.NODE); System.out.println(result); } catch (XPathExpressionException e) { System.out.println(e.getMessage()); } return result; } // 打印 public static void main(String[] args) { getFamilyMemebers(); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); modifySon(); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("修改數據"); getFamilyMemebers(); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); discardSon(); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("刪除數據"); getFamilyMemebers(); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); createSon(); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("添加數據"); getFamilyMemebers(); } }
XML文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <father> <son id="001"> <name>老大</name> <age>20</age> </son> <son id="002"> <name>老二</name> <age>18</age> </son> <son id="003"> <name>老三</name> <age>13</age> </son> </father>
總結
以上就是本文關于java使用DOM對XML文檔進行增刪改查操作實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。