DOM生成XML文档与解析XML文档(JUNIT测试)
1 package cn.liuning.test; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 import javax.xml.parsers.DocumentBuilder; 7 import javax.xml.parsers.DocumentBuilderFactory; 8 import javax.xml.parsers.ParserConfigurationException; 9 import javax.xml.transform.Result; 10 import javax.xml.transform.Source; 11 import javax.xml.transform.Transformer; 12 import javax.xml.transform.TransformerException; 13 import javax.xml.transform.TransformerFactory; 14 import javax.xml.transform.dom.DOMSource; 15 import javax.xml.transform.stream.StreamResult; 16 17 import org.junit.Test; 18 import org.w3c.dom.Document; 19 import org.w3c.dom.Element; 20 import org.w3c.dom.Node; 21 import org.w3c.dom.NodeList; 22 23 public class Demo1 24 { 25 26 @Test//dom生成XML文档 27 public void createdXmlbyDom() throws ParserConfigurationException, TransformerException 28 { 29 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 30 DocumentBuilder builder = factory.newDocumentBuilder(); 31 Document document = builder.newDocument(); 32 //加入根结点 33 Element rootElement = document.createElement("xml"); 34 //新建子结点 35 Element toUserName = document.createElement("ToUserName"); 36 //设置属性 37 toUserName.setAttribute("id", "id_1"); 38 //设置内容 39 String str = "<![CDATA[gh_1c586f95bd68]]>"; 40 toUserName.setTextContent(str); 41 Element fromUserName = document.createElement("FromUserName"); 42 fromUserName.setAttribute("id", "id_2"); 43 String str1 = "<![CDATA[ogNtkuMTj4uOx-DCvLfu8mUDmubM]]>"; 44 fromUserName.setTextContent(str1); 45 46 Element createTime = document.createElement("CreateTime"); 47 createTime.setAttribute("id", "id_2"); 48 String str2 = "1403313142"; 49 createTime.setTextContent(str2); 50 51 Element createTime1 = document.createElement("CreateTime"); 52 createTime1.setAttribute("id", "id_2"); 53 String str3 = "Test"; 54 createTime1.setTextContent(str3); 55 56 rootElement.appendChild(toUserName); 57 rootElement.appendChild(fromUserName); 58 rootElement.appendChild(createTime); 59 rootElement.appendChild(createTime1); 60 61 document.appendChild(rootElement); 62 63 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 64 Transformer transformer = transformerFactory.newTransformer(); 65 Source xmlSource = new DOMSource(document); 66 67 Result outputTarget = new StreamResult(new File("src/book1.xml")); 68 transformer.transform(xmlSource, outputTarget); 69 System.out.println("成功"); 70 } 71 @Test//dom解析XML文档 72 public void read() throws ParserConfigurationException, Exception, IOException 73 { 74 //1.新建工厂 75 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 76 //2.得到dom解析器 77 DocumentBuilder builder = factory.newDocumentBuilder(); 78 //3.得到代表文档的document上 79 Document document = builder.parse("src/book1.xml"); 80 81 System.out.println(document.getInputEncoding());//得到文档编码 82 83 //通过标签名获取标签,得到的是一个list集合 84 //NodeList list = document.getElementsByTagName("CreateTime"); 85 NodeList list = document.getElementsByTagName("xml"); 86 87 //常用方法 88 System.out.println(list.getLength());//可以输出list的长度 89 Node node = list.item(0); //得到第几个结点 90 91 //node方法 92 System.out.println(node.getNodeName());//得到结点名称,输出CreateTime 93 System.out.println(node.hasAttributes());//是否有属性,输出true 94 System.out.println(node.hasChildNodes());//是否有孩子,输出true 95 String content = node.getTextContent();//获取结点内容 96 System.out.println(content); 97 98 } 99 }