使用JAXP对xml文档进行Dom解析
package cn.xue.xml;import java.io.FileOutputStream;import java.io.IOException;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;import org.xml.sax.SAXException;//使用dom方式对xml文档进行crudpublic class Test2 {//读取xml文档中:<书名>JavaScript网页开发</书名> 节点中的值@Testpublic void read1() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse("src/book.xml");NodeList list = document.getElementsByTagName("书名");Node node = list.item(1);String content = node.getTextContent();System.out.println(content);}//得到xml文档中所有的标签@Testpublic void read2() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse("src/book.xml");//得到根节点Node root = document.getElementsByTagName("书架").item(0);list(root);}private void list(Node node) {if(node instanceof Element){System.out.println(node.getNodeName());}NodeList list = node.getChildNodes();for(int i = 0; i < list.getLength(); i++){Node child = list.item(i);list(child);}}//得到xml文档中标签属性的值:<书名 name = "xxxx">Java就业培训教程</书名>@Testpublic void read3() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse("src/book.xml");Element bookname = (Element) document.getElementsByTagName("书名").item(0);String value = bookname.getAttribute("name");System.out.println(value);}//向xml文档中添加节点:<售价>59.00元</售价>@Testpublic 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("59.00元");//把节点挂在第一本书上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")));}//向xml文档中指定位置插入节点:<售价>59.00元</售价>@Testpublic void add2() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse("src/book.xml");//创建节点Element price = document.createElement("售价");price.setTextContent("59.00元");//得到参考节点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文档节点上添加属性:<书名>Java就业培训教程</书名>上添加name="xxxx"属性@Testpublic void addAttr() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse("src/book.xml");Element bookname = (Element) document.getElementsByTagName("书名").item(0);bookname.setAttribute("name", "xxxx");//把更新后的内存写回到xml文档TransformerFactory tffactory = TransformerFactory.newInstance();Transformer tf = tffactory.newTransformer();tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));}@Testpublic void delete1() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse("src/book.xml");//得到要删除的节点Element e = (Element) document.getElementsByTagName("售价").item(0);//得到要删除节点的爸爸Element book = (Element) document.getElementsByTagName("书").item(0);//爸爸再删崽book.removeChild(e);//把更新后的内存写回到xml文档TransformerFactory tffactory = TransformerFactory.newInstance();Transformer tf = tffactory.newTransformer();tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));}//删除整个xml文档@Testpublic void delete2() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse("src/book.xml");//得到要删除的节点Element e = (Element) document.getElementsByTagName("售价").item(0);e.getParentNode().getParentNode().getParentNode().removeChild(e.getParentNode().getParentNode());//把更新后的内存写回到xml文档TransformerFactory tffactory = TransformerFactory.newInstance();Transformer tf = tffactory.newTransformer();tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));}@Testpublic void update() throws Exception{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse("src/book.xml");Element e = (Element) document.getElementsByTagName("售价").item(0);e.setTextContent("109.00元");//把更新后的内存写回到xml文档TransformerFactory tffactory = TransformerFactory.newInstance();Transformer tf = tffactory.newTransformer();tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));}}