本文研究的主要是java使用DOM對(duì)XML文檔進(jìn)行增刪改查操作的相關(guān)代碼,具體實(shí)例如下所示。
我們擁有十載網(wǎng)頁(yè)設(shè)計(jì)和網(wǎng)站建設(shè)經(jīng)驗(yàn),從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁(yè)設(shè)計(jì)師為您提供的解決方案。為企業(yè)提供網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、微信開(kāi)發(fā)、小程序制作、手機(jī)網(wǎng)站制作、H5開(kāi)發(fā)、等業(yè)務(wù)。無(wú)論您有什么樣的網(wǎng)站設(shè)計(jì)或者設(shè)計(jì)方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計(jì)服務(wù)并滿足您的需求。
源代碼:
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() { /* * 創(chuàng)建文件工廠實(shí)例 */ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 如果創(chuàng)建的解析器在解析XML文檔時(shí)必須刪除元素內(nèi)容中的空格,則為true,否則為false dbf.setIgnoringElementContentWhitespace(true); try { /* * 創(chuàng)建文件對(duì)象 */ DocumentBuilder db = dbf.newDocumentBuilder();// 創(chuàng)建解析器,解析XML文檔 Document doc = db.parse(xmlPath); // 使用dom解析xml文件 /* * 歷遍列表,進(jìn)行XML文件的數(shù)據(jù)提取 */ // 根據(jù)節(jié)點(diǎn)名稱來(lái)獲取所有相關(guān)的節(jié)點(diǎn) NodeList sonlist = doc.getElementsByTagName("son"); for (int i = 0; i < sonlist.getLength(); i++) // 循環(huán)處理對(duì)象 { // 節(jié)點(diǎn)屬性的處理 Element son = (Element) sonlist.item(i); // 循環(huán)節(jié)點(diǎn)son內(nèi)的所有子節(jié)點(diǎn) for (Node node = son.getFirstChild(); node != null; node = node .getNextSibling()) { // 判斷是否為元素節(jié)點(diǎn) 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() { // 創(chuàng)建文件工廠實(shí)例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); try { // 從XML文檔中獲取DOM文檔實(shí)例 DocumentBuilder db = dbf.newDocumentBuilder(); // 獲取Document對(duì)象 Document xmldoc = db.parse(xmlPath); // 獲取根節(jié)點(diǎn) Element root = xmldoc.getDocumentElement(); // 定位id為001的節(jié)點(diǎn) Element per = (Element) selectSingleNode("/father/son[@id='001']", root); // 將age節(jié)點(diǎn)的內(nèi)容更改為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()); } } // 獲取目標(biāo)節(jié)點(diǎn),進(jìn)行刪除,最后保存 public static void discardSon() { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); try { DocumentBuilder db = dbf.newDocumentBuilder(); Document xmldoc = db.parse(xmlPath); // 獲取根節(jié)點(diǎn) Element root = xmldoc.getDocumentElement(); // 定位根節(jié)點(diǎn)中的id=002的節(jié)點(diǎn) Element son = (Element) selectSingleNode("/father/son[@id='002']", root); // 刪除該節(jié)點(diǎn) 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()); } } // 新增節(jié)點(diǎn) public static void createSon() { // 創(chuàng)建文件工廠實(shí)例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(false); try { DocumentBuilder db = dbf.newDocumentBuilder(); // 創(chuàng)建Document對(duì)象 Document xmldoc = db.parse(xmlPath); // 獲取根節(jié)點(diǎn) Element root = xmldoc.getDocumentElement(); // 創(chuàng)建節(jié)點(diǎn)son,設(shè)置對(duì)應(yīng)的id為004 Element son = xmldoc.createElement("son"); son.setAttribute("id", "004"); // 創(chuàng)建節(jié)點(diǎn)name Element name = xmldoc.createElement("name"); name.setTextContent("小兒子"); son.appendChild(name); // 創(chuàng)建節(jié)點(diǎn)age Element age = xmldoc.createElement("age"); age.setTextContent("0"); son.appendChild(age); // 把son添加到根節(jié)點(diǎn)中 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()); } } // 修改節(jié)點(diǎn)信息 public static Node selectSingleNode(String express, Element source) { Node result = null; //創(chuàng)建XPath工廠 XPathFactory xpathFactory = XPathFactory.newInstance(); //創(chuàng)建XPath對(duì)象 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("修改數(shù)據(jù)"); getFamilyMemebers(); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); discardSon(); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("刪除數(shù)據(jù)"); getFamilyMemebers(); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); createSon(); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("添加數(shù)據(jù)"); getFamilyMemebers(); } }
XML文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>老大 20 老二 18 老三 13
總結(jié)
以上就是本文關(guān)于java使用DOM對(duì)XML文檔進(jìn)行增刪改查操作實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!