package day03.xml; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import javax.xml.crypto.dsig.Transform; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; 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.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; //使用dom方式对xml进行crud public class Demo3 { @Test public void read() throws ParserConfigurationException, SAXException, IOException{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=(Document) builder.parse("src/2.xml"); NodeList list=document.getElementsByTagName("soft"); Node node = list.item(1); String content = node.getTextContent(); System.out.println(content); } @Test public void read1() throws ParserConfigurationException, SAXException, IOException{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/2.xml"); //获得根结点 Node root=document.getElementsByTagName("soft").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); } } @Test //得到name属性内容 public void read3() throws ParserConfigurationException, SAXException, IOException{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/2.xml"); Element bookname=(Element) document.getElementsByTagName("soft").item(0); String value =bookname.getAttribute("name"); System.out.println(value); } @Test //向xml中添加节点<雪>22222<雪> public void add1() throws ParserConfigurationException, SAXException, IOException, TransformerException{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/2.xml"); //创建节点 Element price=document.createElement("雪"); price.setTextContent("22222"); //把创建的节点挂到第一本书上 Element book=(Element) document.getElementsByTagName("a").item(0); book.appendChild(price); //把更新的内容重新写入xml文件里 TransformerFactory transform =TransformerFactory.newInstance(); Transformer tf=transform.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/2.xml"))); } @Test //向xml中添加节点在<a2>22222</a2>前 public void add2() throws ParserConfigurationException, SAXException, IOException, TransformerException{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/2.xml"); //创建节点 Element price=document.createElement("a3"); price.setTextContent("a222222before"); //参考节点 Element renode=(Element) document.getElementsByTagName("a2").item(0); //得到要挂的节点 Element book=(Element) document.getElementsByTagName("a").item(0); //往book节点的制定位置插入 book.insertBefore(price,renode); //把更新的内容重新写入xml文件里 TransformerFactory transform =TransformerFactory.newInstance(); Transformer tf=transform.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/2.xml"))); } @Test //向xml中<a2>22222</a2>节点添加name属性=“xxxxx” public void add3() throws ParserConfigurationException, SAXException, IOException, TransformerException{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse("src/2.xml"); //取得节点 Element bookname =(Element) document.getElementsByTagName("a2").item(0); bookname.setAttribute("name","XXXXXX"); //把更新的内容重新写入xml文件里 TransformerFactory transform =TransformerFactory.newInstance(); Transformer tf=transform.newTransformer(); tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/2.xml"))); } }
<?xml version="1.0" encoding="UTF-8" standalone="no"?><soft name="snowing"> <a> <a1>中国</a1> <a3>a222222before</a3> <a2 name="XXXXXX">ssssss</a2> <雪>22222</雪> </a> <b> <b1/> <b2/> <雪莹>879</雪莹> </b> </soft>
既往不恋,未来不迎,当下不杂