// 1, 获取解析器工厂 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // 2, 获取解析器 DocumentBuilder parser = factory.newDocumentBuilder(); // 3, 解析xml文档,得到代表文档的document对象 Document document = parser.parse(new File("src/jaxp/dom/book.xml"));
// 写入内存 TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.transform(new DOMSource(document), new StreamResult(new File("src/jaxp/dom/book.xml")));
1, book.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <书架> <书 name="xxx"> <书名>Java就业培训教程</书名> <售价>11元</售价> <售价>59.00元</售价> <作者>张孝祥</作者> </书> <书> <书名>JavaScript网页开发</书名> <作者>张孝祥</作者> <售价>28.00元</售价> </书> </书架>
2, Demo1.java
package jaxp.dom; 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 org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * 用JAXP 以DOM方式操作 xml */ public class Demo1 { @Test public void setUp() throws Exception { // 1, 获取解析器工厂 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // 2, 产生解析器 DocumentBuilder parser = factory.newDocumentBuilder(); // 3, 解析xml文档,得到代表文档的document对象 Document document = parser.parse(new File("src/jaxp/dom/book.xml")); System.out.println(document); } @Test // 得到售价节点的值 public void read() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = factory.newDocumentBuilder(); Document document = parser.parse(new File("src/jaxp/dom/book.xml")); NodeList nodes = document.getElementsByTagName("售价"); Node price = nodes.item(0); String value = price.getTextContent(); System.out.println(value); } @Test // 修改结点的值: <售价>39.00元</售价> -> <售价>59.00元</售价> public void update() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/jaxp/dom/book.xml")); Node price = document.getElementsByTagName("售价").item(0); price.setTextContent("59.00元"); // 把内存中的document写到xml文档 // 1, 转换器工厂 TransformerFactory tFactory = TransformerFactory.newInstance(); // 2, 得到转换器 Transformer transformer = tFactory.newTransformer(); // 3, 将内存中的document写入指定文件 transformer.transform(new DOMSource(document) , new StreamResult(new File("src/jaxp/dom/book.xml")) ); } @Test // 末尾 添加新结点 <售价>11元</售价> public void add() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/jaxp/dom/book.xml"); // 创建新结点 <售价>11元</售价> Element price = document.createElement("售价"); price.setTextContent("11元"); // 得到需要增加结点的父亲 Element parent = (Element) document.getElementsByTagName("书").item(0); // 将 新结点 挂载到父节点上 parent.appendChild(price); // 写入内存 TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.transform(new DOMSource(document), new StreamResult(new File("src/jaxp/dom/book.xml"))); } @Test // 指定位置<售价>59.00元</售价> 之前, 添加新结点 <售价>11元</售价> public void add_2() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/jaxp/dom/book.xml")); // 创建新结点 <售价>11元</售价> Element newPrice = document.createElement("售价"); newPrice.setTextContent("11元"); // 获取父节点 Element parent = (Element) document.getElementsByTagName("书").item(0); // 获取指定结点 Node refChild = parent.getElementsByTagName("售价").item(0); // 添加到指定结点的后面 parent.insertBefore(newPrice, refChild); // 写到xml中 TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.transform(new DOMSource(document), new StreamResult(new File("src/jaxp/dom/book.xml"))); } @Test // 删除结点 <售价>11元</售价> public void delete() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/jaxp/dom/book.xml")); /* // 得到需要增加结点的父亲 Element parent = (Element) document.getElementsByTagName("书").item(0); // 得到需要删除的结点 Element child = (Element) parent.getElementsByTagName("售价").item(1); // 删除 <售价>11元</售价> parent.removeChild(child); */ // 得到需要删除的结点 Node node = document.getElementsByTagName("售价").item(0); // 删除 node.getParentNode().removeChild(node); // 写入内存 TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.transform(new DOMSource(document), new StreamResult(new File("src/jaxp/dom/book.xml"))); } @Test //更新 xml文档属性, <书 name="xxx"> -> <书 name="yyy"> public void updateAttribute() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/jaxp/dom/book.xml")); /* // 获取指定结点 Element book = (Element) document.getElementsByTagName("书").item(0); // 获取指定结点 的属性 Attr nameAttr = book.getAttributeNode("name"); // 更新 nameAttr.setTextContent("yyy");*/ // 获取指定结点 Element book = (Element) document.getElementsByTagName("书").item(0); // 更改 book.setAttribute("name", "yyy"); // 增加 book.setAttribute("password", "123"); // 删除 book.removeAttribute("name"); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.transform(new DOMSource(document), new StreamResult(new File("src/jaxp/dom/book.xml"))); } //遍历 private void list(Node node) throws Exception { if (node.getNodeType() == Node.ELEMENT_NODE) { System.out.println(node.getNodeName()); } if ( !node.hasChildNodes()) { if ("".equals(node.getTextContent().trim())) { return; } System.out.println(" " + node.getTextContent()); return; } NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { list(children.item(i)); } } @Test public void traverseXml() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("src/jaxp/dom/book.xml")); list(document); } }