DOM 操作XML(CRUD)

时间:2023-11-19 17:34:56
<?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
<书>
<书名 name="dddd">C语言程序设计</书名>
<作者>张孝祥</作者>
<售价>40</售价>
</书>
<书>
<书名>C++教程</书名>
<作者>自己</作者>
<售价>50</售价>
</书>
</书架>

  

package com.gbx.it;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StreamCorruptedException; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; public class XMLDemo {
public String path = "src/book.xml";
/*
* 获得指定的Document
*/
public Document getDocument() throws ParserConfigurationException, SAXException, IOException {
//1: 获得dom解析工厂
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
//2:获得dom解析
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
//获得Document
Document document = documentBuilder.parse(path); return document;
}
/*
* 将XML文件由内存写入硬盘
*/
public void refreshXML(Source xmlSource, Result outputTarget) throws TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(xmlSource, outputTarget);
}
// ----------R------------------
/*
* 遍历DOM树
*/
@Test
public void read1() throws ParserConfigurationException, SAXException, IOException {
Document document = getDocument();
NodeList nodeList = document.getElementsByTagName("书架");
for (int i = 0; i < nodeList.getLength(); ++i) {
listNodes(nodeList.item(i));
}
}
private void listNodes(Node item) {
if (item instanceof Element) {
System.out.println(item.getNodeName());
}
NodeList nodeList = item.getChildNodes();
for (int i = 0; i < nodeList.getLength(); ++i) {
listNodes(nodeList.item(i));
}
}
/*
* 读取标签内的内容 <书名 name="dddd">java web就业</书名>
*/
@Test
public void read2() throws ParserConfigurationException, SAXException, IOException {
Document document = getDocument();
Element book = (Element) document.getElementsByTagName("书名").item(0);
String value = book.getTextContent();
System.out.println("书名: " + value);
}
/*
* 读取标签的属性
*/
@Test
public void read3() throws ParserConfigurationException, SAXException, IOException {
Document document = getDocument();
Element book = (Element) document.getElementsByTagName("书名").item(0);
if (book.hasAttributes()) {
NamedNodeMap nodeMap = book.getAttributes();
for (int i = 0; i < nodeMap.getLength(); ++i) {
String attrName = nodeMap.item(i).getNodeName();
String attrValue = nodeMap.item(i).getNodeValue();
System.out.println("name : " + attrName + " value :" + attrValue);
}
} String value = book.getAttribute("name");
System.out.println(value);
}
// ----------------C------------------- /*
* 添加标签 C
*/
//在指定标签的最后边添加标签
@Test
public void add1() throws ParserConfigurationException, SAXException, IOException, TransformerException {
Document document = getDocument();
Element book = (Element) document.getElementsByTagName("书").item(0);
Element newChild = document.createElement("你行");
newChild.setTextContent("嘿嘿");
book.appendChild(newChild); refreshXML(new DOMSource(document), new StreamResult(new FileOutputStream(path)));
}
//在指定标签的指定位置添加标签
@Test
public void add2() throws ParserConfigurationException, SAXException, IOException, TransformerException {
Document document = getDocument();
Element book = (Element) document.getElementsByTagName("书").item(0); Element newChild = document.createElement("你行");
newChild.setTextContent("嘿嘿"); Element refChild = (Element) document.getElementsByTagName("售价").item(0); book.insertBefore(newChild, refChild); refreshXML(new DOMSource(document), new StreamResult(new FileOutputStream(path)));
}
//添加属性
@Test
public void add3() throws ParserConfigurationException, SAXException, IOException, TransformerException {
Document document = getDocument(); Element e = (Element) document.getElementsByTagName("售价").item(0);
e.setAttribute("value", "RMB");
refreshXML(new DOMSource(document), new StreamResult(new FileOutputStream(path)));
}
//----------------D----------------
//删除标签
@Test
public void delElement() throws ParserConfigurationException, SAXException, IOException, TransformerException {
Document document = getDocument();
Element element = (Element) document.getElementsByTagName("你行").item(0);
element.getParentNode().removeChild(element);
refreshXML(new DOMSource(document), new StreamResult(new FileOutputStream(path)));
}
//删除标签的属性 @Test
public void delAttr() throws ParserConfigurationException, SAXException, IOException, TransformerException {
Document document = getDocument();
Element element = (Element) document.getElementsByTagName("售价").item(0);
element.removeAttribute("value");
refreshXML(new DOMSource(document), new StreamResult(new FileOutputStream(path)));
}
//-------------U------------
@Test
public void update() throws ParserConfigurationException, SAXException, IOException, TransformerException {
Document document = getDocument();
Element element = (Element) document.getElementsByTagName("书名").item(0);
element.setTextContent("C语言程序设计");
refreshXML(new DOMSource(document), new StreamResult(new FileOutputStream(path)));
}
}

  

参考:方立勋老师视频