import java.io.FileOutputStream; import javax.swing.event.DocumentListener; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; 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; //使用DOM方式对XML文档进行CRUD public class Demo2 { //查询数据 @Test public void read() throws Exception{ //1:创建工厂 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //2:得到DOM解析器 DocumentBuilder builder = factory.newDocumentBuilder(); //3:解析XML文档,得到代表文档的document Document document = builder.parse("src/book.xml"); NodeList list = document.getElementsByTagName("书名"); Node node = list.item(1); String content = node.getTextContent(); System.out.println(content); } //添加数据 @Test public void add() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); //创建节点 Element price = document.createElement("售价"); price.setTextContent("3.15元"); //把创建的节点挂在第一本书上 Element book =(Element) document.getElementsByTagName("书").item(0); book.appendChild(price); //把更新后内存写会到XML文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //向指定位置添加节点 @Test public void add1() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); //创建节点 Element price = document.createElement("售价"); price.setTextContent("5.25元"); //得到参考节点 Element refNode=(Element)document.getElementsByTagName("售价").item(0); //得到要挂载的节点 Element book = (Element) document.getElementsByTagName("书").item(0); //往book节点的指定位置插入 book.insertBefore(price, refNode); //把更新后内存写会到XML文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //向XML文档节点上添加属性 @Test public void addattr() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); //得到参考节点 Element bookName=(Element)document.getElementsByTagName("书名"); bookName.setAttribute("name", "XXXXX"); //把更新后内存写会到XML文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //删除单个节点 @Test public void delete() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); //先得到要删除的节点 Element delE = (Element) document.getElementsByTagName("售价").item(0); //得到要删除节点的爸爸 Element book = (Element) document.getElementsByTagName("书").item(0); //爸爸删除 book.removeChild(delE); } //删除全部节点 @Test public void deleteAll() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); //先得到要删除的节点 Element book = (Element) document.getElementsByTagName("售价").item(0); book.getParentNode().getParentNode().getParentNode().removeChild(book.getParentNode().getParentNode()); //把更新后内存写会到XML文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } //更新售价 @Test public void update() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/book.xml"); Element book = (Element) document.getElementsByTagName("售价").item(0); book.setTextContent("105"); //把更新后内存写会到XML文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml"))); } }
书的XML
<书架> <书> <书名>Java培训书名> <作者>张扬作者> <售价>5.25元售价> 书> <书> <书名>Java培训2书名> <作者>张扬作者> <售价>21.5售价> 书> 书架>