DOM的全称是Document Object Model,即文档对象模型。在应用程序中,基于DOM的XML解析器将一个XML文档转换成一个对象模型的集合(通常称DOM树),应用程序正是通过对这个对象模型的操作,来实现对XML文档数据的操作。通过DOM接口,应用程序可以在任何时候访问XML文档中的任何一部分数据,因此,这种利用DOM接口的机制也被称作随机访问机制。
DOM树所提供的随机访问方式给应用程序的开发带来了很大的灵活性,它可以任意地控制整个XML文档中的内容。然而,由于DOM分析器把整个XML文档转化成DOM树放在了内存中,因此,当文档比较大或者结构比较复杂时,对内存的需求就比较高。而且,对于结构复杂的树的遍历也是一项耗时的操作。所以,DOM分析器对机器性能的要求比较高,实现效率不十分理想。不过,由于DOM分析器所采用的树结构的思想与XML文档的结构相吻合,同时鉴于随机访问所带来的方便,因此,DOM分析器还是有很广泛的使用价值的。
xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
<?xml version= "1.0"
encoding= "UTF-8" ?> <exam> <student examid= "222" > <name>张三</name> <location>沈阳</location> <grade> 89 </grade> </student> <student idcard= "333" > <name>李四</name> <location>大连</location> <grade> 110 </grade> </student> </exam> |
实例:
1 package com.bcrj.dom; 2 3 import javax.xml.parsers.DocumentBuilder; 4 import javax.xml.parsers.DocumentBuilderFactory; 5 import javax.xml.parsers.ParserConfigurationException; 6 import javax.xml.transform.Transformer; 7 import javax.xml.transform.TransformerConfigurationException; 8 import javax.xml.transform.TransformerException; 9 import javax.xml.transform.TransformerFactory; 10 import javax.xml.transform.dom.DOMSource; 11 import javax.xml.transform.stream.StreamResult; 12 13 import org.w3c.dom.Document; 14 import org.w3c.dom.Element; 15 import org.w3c.dom.Node; 16 import org.w3c.dom.NodeList; 17 18 public class Test1 { 19 DocumentBuilderFactory dbf = null; 20 DocumentBuilder db = null; 21 private Document document = null; 22 23 public Document getDocument() { 24 return document; 25 } 26 27 public void setDocument(Document document) { 28 this.document = document; 29 } 30 31 // 1、得到某个具体的节点内容,第一个student的location 32 public String test1() { 33 NodeList nl = document.getElementsByTagName("location"); 34 int index = 0; 35 for (int i = 0; i < nl.getLength(); i++) { 36 Node node = nl.item(i); 37 String nodeName = node.getNodeName(); 38 if ("location".equals(nodeName)) { 39 index++; 40 } 41 if (index == 1) { 42 return node.getTextContent(); 43 } 44 } 45 return null; 46 } 47 48 // 2、遍历所有元素节点 49 public void test2(Node node) { 50 if (node instanceof Element) { 51 System.out.println(node.getNodeName()); 52 } 53 NodeList nl = node.getChildNodes(); 54 int len = nl.getLength(); 55 for (int i = 0; i < len; i++) { 56 Node n = nl.item(i); 57 test2(n); 58 } 59 } 60 61 // 3、修改某个元素节点的主体内容,把第二个studnent的grade改为100 62 public void test3() throws TransformerException { 63 // 根元素 64 int index = 0; 65 Node n = document.getFirstChild(); 66 NodeList nl = n.getChildNodes(); 67 int len = nl.getLength(); 68 69 for (int i = 0; i < nl.getLength(); i++) { 70 if (nl.item(i) instanceof Element) { 71 index++; 72 if (index == 2) { 73 NodeList nodelist = nl.item(i).getChildNodes(); 74 for (int j = 0; j < nodelist.getLength(); j++) { 75 Node node = nodelist.item(j); 76 if (node instanceof Element) { 77 if (node.getNodeName().equals("grade")) { 78 node.setTextContent("110"); 79 } 80 } 81 } 82 } 83 } 84 } 85 TransformerFactory facotry = TransformerFactory.newInstance(); 86 Transformer ts = facotry.newTransformer(); 87 ts.transform(new DOMSource(document), new StreamResult( 88 "src/students.xml")); 89 } 90 91 // 4、向指定元素节点中增加子元素节点,第一student中增加子元素<age>23</age> 92 public void test4() throws TransformerException { 93 NodeList nl = document.getElementsByTagName("student"); 94 Element element = document.createElement("age"); 95 element.setTextContent("23"); 96 nl.item(0).appendChild(element); 97 // 写入xml 98 TransformerFactory facotry = TransformerFactory.newInstance(); 99 Transformer ts = facotry.newTransformer(); 100 ts.transform(new DOMSource(document), new StreamResult( 101 "src/students.xml")); 102 } 103 104 // 5、向指定元素节点上增加同级元素节点,在第二个student的location后添加<age>23</age> 105 public void test5() throws Exception { 106 NodeList nl = document.getElementsByTagName("student"); 107 Element element = document.createElement("age"); 108 element.setTextContent("23"); 109 110 NodeList nll = nl.item(1).getChildNodes(); 111 for (int i = 0; i < nll.getLength(); i++) { 112 113 if ((nll.item(i) instanceof Element) 114 && "location".equals(nll.item(i).getNodeName())) { 115 nll.item(i).getParentNode().insertBefore(element, nll.item(i)); 116 } 117 } 118 TransformerFactory facotry = TransformerFactory.newInstance(); 119 Transformer ts = facotry.newTransformer(); 120 ts.transform(new DOMSource(document), new StreamResult( 121 "src/students.xml")); 122 } 123 124 // 6、删除指定元素节点,删除第二个student元素的age子元素 125 public void test6() throws Exception { 126 NodeList nl = document.getElementsByTagName("student"); 127 NodeList nll = nl.item(1).getChildNodes(); 128 for (int i = 0; i < nll.getLength(); i++) { 129 if ((nll.item(i) instanceof Element) 130 && "age".equals(nll.item(i).getNodeName())) { 131 //由父元素调用删除 132 nll.item(i).getParentNode().removeChild(nll.item(i)); 133 } 134 } 135 136 TransformerFactory facotry = TransformerFactory.newInstance(); 137 Transformer ts = facotry.newTransformer(); 138 ts.transform(new DOMSource(document), new StreamResult( 139 "src/students.xml")); 140 } 141 142 // 7、操作XML文件属性,打印第一学生的examid 143 public void test7(){ 144 NodeList nl = document.getElementsByTagName("student"); 145 Element nll = (Element)nl.item(0); 146 String examid = nll.getAttribute("examid"); 147 System.out.println(examid); 148 149 } 150 151 // 8、添加一个idcard属性给第二student 152 public void test8() throws Exception{ 153 NodeList nl = document.getElementsByTagName("student"); 154 Element nll = (Element)nl.item(1); 155 nll.setAttribute("idcard", "333"); 156 157 TransformerFactory facotry = TransformerFactory.newInstance(); 158 Transformer ts = facotry.newTransformer(); 159 ts.transform(new DOMSource(document), new StreamResult( 160 "src/students.xml")); 161 } 162 163 //9.删除第一个student的idcard 164 public void test9() throws Exception{ 165 NodeList nl = document.getElementsByTagName("student"); 166 Element nll = (Element)nl.item(0); 167 nll.removeAttribute("idcard"); 168 169 TransformerFactory facotry = TransformerFactory.newInstance(); 170 Transformer ts = facotry.newTransformer(); 171 ts.transform(new DOMSource(document), new StreamResult( 172 "src/students.xml")); 173 } 174 175 // Document 对象的一个新实例来生成一个 DOM 树 176 public Document getDomParser() throws Exception { 177 // DOM解析工厂 178 dbf = DocumentBuilderFactory.newInstance(); 179 // DOM对象数解析器 180 db = dbf.newDocumentBuilder(); 181 // 获取 DOM Document 对象的一个新实例来生成一个 DOM 树 182 document = db.parse("src/students.xml"); 183 return document; 184 } 185 186 }
单元测试
1 package com.bcrj.test; 2 3 import static org.junit.Assert.*; 4 5 import javax.xml.transform.TransformerException; 6 7 import org.junit.Assert; 8 import org.junit.Before; 9 import org.junit.BeforeClass; 10 import org.junit.Test; 11 import org.w3c.dom.Document; 12 13 import com.bcrj.dom.Test1; 14 15 public class Domtest { 16 17 static Test1 test = null; 18 19 @BeforeClass 20 public static void a(){ 21 test= new Test1(); 22 } 23 24 @Before 25 public void setUp() throws Exception { 26 test.getDomParser(); 27 28 } 29 30 // @Test 31 // public void testTest1() { 32 // String str = test.test1(); 33 // Assert.assertEquals("沈阳", str); 34 // } 35 36 // @Test 37 // public void testTest2(){ 38 // Document document = test.getDocument(); 39 // test.test2(document); 40 // } 41 // 42 // @Test 43 // public void testTest3() throws TransformerException { 44 // test.test3(); 45 // //Assert.assertEquals("沈阳", str); 46 // } 47 // 48 // @Test 49 // public void testTest4() throws TransformerException { 50 // test.test4(); 51 // //Assert.assertEquals("沈阳", str); 52 // } 53 54 // @Test 55 // public void testTest4() throws Exception { 56 // test.test5(); 57 // //Assert.assertEquals("沈阳", str); 58 // } 59 60 @Test 61 public void testTest4() throws Exception { 62 test.test9(); 63 //Assert.assertEquals("沈阳", str); 64 } 65 66 }
原文:http://www.cnblogs.com/dapeng520/p/3658794.html