java使用DOM对XML文档进行增删改查 java使用DOM对XML文档进行增删改查操作实例代码
zc2274460 人气:0想了解java使用DOM对XML文档进行增删改查操作实例代码的相关内容吗,zc2274460在本文为您仔细讲解java使用DOM对XML文档进行增删改查的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,dom解析xml,java,xml,dom,java,dom,生成xml,java,dom解析xml文件,java,dom,写xml,下面大家一起来学习吧。
本文研究的主要是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文档进行增删改查操作实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
加载全部内容