DOM基於樹形,SAX基於事件,DOM4J和JDOM基於底層API ...
一、先導入jar包
DOM基於樹形,SAX基於事件,DOM4J和JDOM基於底層API
二、代碼如下
1 package com.sxt.test; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.InputStreamReader; 10 import java.util.ArrayList; 11 import java.util.Iterator; 12 import java.util.List; 13 14 import javax.naming.spi.DirStateFactory.Result; 15 import javax.xml.parsers.DocumentBuilder; 16 import javax.xml.parsers.DocumentBuilderFactory; 17 import javax.xml.parsers.ParserConfigurationException; 18 import javax.xml.parsers.SAXParser; 19 import javax.xml.parsers.SAXParserFactory; 20 import javax.xml.transform.OutputKeys; 21 import javax.xml.transform.Transformer; 22 import javax.xml.transform.TransformerConfigurationException; 23 import javax.xml.transform.TransformerException; 24 import javax.xml.transform.TransformerFactory; 25 import javax.xml.transform.dom.DOMSource; 26 import javax.xml.transform.sax.SAXTransformerFactory; 27 import javax.xml.transform.sax.TransformerHandler; 28 import javax.xml.transform.stream.StreamResult; 29 30 import org.dom4j.DocumentException; 31 import org.dom4j.io.OutputFormat; 32 import org.dom4j.io.SAXReader; 33 import org.dom4j.io.XMLWriter; 34 import org.jdom2.Attribute; 35 import org.jdom2.JDOMException; 36 import org.jdom2.input.SAXBuilder; 37 import org.jdom2.output.Format; 38 import org.jdom2.output.XMLOutputter; 39 import org.junit.Test; 40 import org.w3c.dom.Document; 41 import org.w3c.dom.Element; 42 import org.w3c.dom.Node; 43 import org.w3c.dom.NodeList; 44 import org.xml.sax.SAXException; 45 import org.xml.sax.helpers.AttributesImpl; 46 47 import com.sxt.bean.Book; 48 import com.sxt.bean.SAXParserHandler; 49 50 public class Demo1 { 51 52 private ArrayList<Book> booksList = new ArrayList<Book>(); 53 54 /* 55 * DOM解析 56 */ 57 @Test 58 public void testfun1() throws ParserConfigurationException, SAXException, 59 IOException { 60 // 創建DocumentBuilderFactory對象 61 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 62 // 創建DocumentBuilder對象 63 DocumentBuilder db = dbf.newDocumentBuilder(); 64 // 通過DocumentBuilder對象的parser方法載入books.xml文件到當前的目錄下 65 Document document = db.parse("src/com/sxt/test/bookes.xml"); 66 // 獲取所有的book節點集合 67 NodeList bookList = document.getElementsByTagName("book"); 68 // 通過nodelist的getLength()方法可以獲取bookList的長度 69 System.out.println("一共有" + bookList.getLength() + "本書"); 70 // 遍歷每一個book節點 71 for (int i = 0; i < bookList.getLength(); i++) { 72 System.out.println("============開始遍歷=================="); 73 /* 74 * //通過item(i)方法獲取一個book節點,nodelist的索引值從0開始 Node book = 75 * bookList.item(i); //獲取book節點的所有屬性集合 NamedNodeMap attr = 76 * book.getAttributes(); 77 * System.out.println("第"+(i+1)+"本書共有"+attr.getLength()+"個屬性"); 78 * //遍歷book屬性 for(int j=0;j<attr.getLength();j++){ 79 * //通過item(index)方法獲取book節點的某一個屬性 Node at = attr.item(j); //獲取屬性名 80 * System.out.println("屬性名:"+at.getNodeName()); //獲取屬性值 81 * System.out.println("屬性值:"+at.getNodeValue()); } 82 */ 83 Element book = (Element) bookList.item(i); 84 /* 85 * String attrValue = book.getAttribute("id"); 86 * System.out.println("屬性值為:"+attrValue); 87 */ 88 // 解析book節點的子節點 89 NodeList childnNode = book.getChildNodes(); 90 System.out.println("第" + (i + 1) + "本書共有" + childnNode.getLength() 91 + "個子節點"); 92 for (int k = 0; k < childnNode.getLength(); k++) { 93 // 區分出text類型的node以及element類型的node 94 if (childnNode.item(k).getNodeType() == Node.ELEMENT_NODE) { 95 // 獲取element類型節點的節點名 96 System.out.println(childnNode.item(k).getNodeName()); 97 // System.out.println(childnNode.item(k).getFirstChild().getNodeValue()); 98 System.out.println(childnNode.item(k).getTextContent()); 99 } 100 } 101 System.out.println("============結束遍歷=================="); 102 } 103 } 104 105 /* 106 * DOM創建XML文件 107 */ 108 @Test 109 public void testfun6() throws ParserConfigurationException, 110 TransformerException { 111 // 創建DocumentBuilderFactory對象 112 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 113 // 創建DocumentBuilder對象 114 DocumentBuilder db = dbf.newDocumentBuilder(); 115 Document document = db.newDocument(); 116 document.setXmlStandalone(true); 117 Element bookstore = document.createElement("bookStore"); 118 // 向bookstore根節點中添加子節點 119 Element book = document.createElement("book"); 120 Element name = document.createElement("name"); 121 // name.setNodeValue("小王子"); 122 name.setTextContent("小王子"); 123 book.appendChild(name); 124 book.setAttribute("id", "1"); 125 bookstore.appendChild(book); 126 document.appendChild(bookstore); 127 128 // 創建TransformerFactory對象 129 TransformerFactory tff = TransformerFactory.newInstance(); 130 // 創建Transformer對象 131 Transformer tf = tff.newTransformer(); 132 tf.setOutputProperty(OutputKeys.INDENT, "yes"); 133 tf.transform(new DOMSource(document), new StreamResult(new File( 134 "src/com/sxt/test/book.xml"))); 135 136 } 137 138 /* 139 * SAX解析 140 */ 141 @Test 142 public void testfun2() throws ParserConfigurationException, SAXException, 143 IOException { 144 // 獲取一個SAXParserFactory的實例 145 SAXParserFactory factory = SAXParserFactory.newInstance(); 146 // 通過factory獲取SAXParser實例 147 SAXParser parser = factory.newSAXParser(); 148 // 創建SAXParserHandler對象 149 SAXParserHandler handler = new SAXParserHandler(); 150 parser.parse("src/com/sxt/test/bookes.xml", handler); 151 System.out.println("~~~~~~共有" + handler.getBookList().size()); 152 for (Book book : handler.getBookList()) { 153 System.out.println(book.getId()); 154 System.out.println(book.getAuthor()); 155 System.out.println(book.getName()); 156 System.out.println(book.getLanguage()); 157 System.out.println(book.getPrice()); 158 System.out.println(book.getYear()); 159 } 160 161 } 162 163 public ArrayList<Book> parseXML() throws Exception { 164 // 獲取一個SAXParserFactory的實例 165 SAXParserFactory factory = SAXParserFactory.newInstance(); 166 // 通過factory獲取SAXParser實例 167 SAXParser parser = factory.newSAXParser(); 168 // 創建SAXParserHandler對象 169 SAXParserHandler handler = new SAXParserHandler(); 170 parser.parse("src/com/sxt/test/bookes.xml", handler); 171 172 return handler.getBookList(); 173 174 } 175 176 /* 177 * SAX創建XML文件 178 */ 179 @Test 180 public void testfun7() throws Exception { 181 ArrayList<Book> bookList = parseXML(); 182 //創建一個TransformerFactory類的對象 183 SAXTransformerFactory tff = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); 184 //通過SAXTransformerFactory對象創建一個TransformerHandler對象 185 TransformerHandler handler = tff.newTransformerHandler(); 186 //通過handler對象創建一個Transformer對象 187 Transformer tr = handler.getTransformer(); 188 //通過Transformer對象對生成的XML文件進行設置(編碼) 189 tr.setOutputProperty(OutputKeys.ENCODING,"UTF-8"); 190 //通過Transformer對象進行換行 191 tr.setOutputProperty(OutputKeys.INDENT,"yes"); 192 //創建一個Result對象 193 File f = new File("src/com/sxt/test/booke.xml"); 194 if(!f.exists()){ 195 f.createNewFile(); 196 } 197 //創建一個Result對象,並且使其與handler關聯 198 StreamResult result = new StreamResult(new FileOutputStream(f)); 199 handler.setResult(result); 200 //打開document 201 handler.startDocument(); 202 AttributesImpl attr = new AttributesImpl(); 203 handler.startElement("", "", "bookstore", attr); 204 for(Book book:bookList){ 205 attr.clear(); 206 attr.addAttribute("", "", "id", "", book.getId()); 207 handler.startElement("", "", "book", attr); 208 209 if(book.getName()!=null&& !book.getName().trim().equals("")){ 210 attr.clear(); 211 //創建name節點 212 handler.startElement("", "", "name", attr); 213 handler.characters(book.getName().toCharArray(), 0, book.getName().length()); 214 handler.endElement("", "", "name"); 215 } 216 if(book.getAuthor()!=null&& !book.getAuthor().trim().equals("")){ 217 attr.clear(); 218 //創建name節點 219 handler.startElement("", "", "author", attr); 220 handler.characters(book.getAuthor().toCharArray(), 0, book.getAuthor().length()); 221 handler.endElement("", "", "author"); 222 } 223 if(book.getLanguage()!=null&& !book.getLanguage().trim().equals("")){ 224 attr.clear(); 225 //創建name節點 226 handler.startElement("", "", "language", attr); 227 handler.characters(book.getLanguage().toCharArray(), 0, book.getLanguage().length()); 228 handler.endElement("", "", "language"); 229 } 230 if(book.getPrice()!=null&& !book.getPrice().trim().equals("")){ 231 attr.clear(); 232 //創建name節點 233 handler.startElement("", "", "price", attr); 234 handler.characters(book.getPrice().toCharArray(), 0, book.getPrice().length()); 235 handler.endElement("", "", "price"); 236 } 237 if(book.getYear()!=null&& !book.getYear().trim().equals("")){ 238 attr.clear(); 239 //創建name節點 240 handler.startElement("", "", "year", attr); 241 handler.characters(book.getYear().toCharArray(), 0, book.getYear().length()); 242 handler.endElement("", "", "year"); 243 } 244 245 } 246 handler.endElement("", "", "bookstore"); 247 //關閉document 248 handler.endDocument(); 249 } 250 251 /* 252 * 創建JDOM 253 */ 254 @Test 255 public void createXML() throws Exception{ 256 //生成一個根節點 257 org.jdom2.Element rss = new org.jdom2.Element("rss"); 258 //為節點添加屬性 259 rss.setAttribute("version", "2.0"); 260 //生成一個document對象 261 org.jdom2.Document document = new org.jdom2.Document(rss); 262 org.jdom2.Element channel = new org.jdom2.Element("channel"); 263 rss.addContent(channel); 264 org.jdom2.Element title = new org.jdom2.Element("title"); 265 title.setText("國內國際新聞"); 266 channel.addContent(title); 267 268 Format format = Format.getPrettyFormat(); 269 format.setIndent(""); 270 format.setEncoding("GBK"); 271 272 //創建XMLOutputter的對象 273 XMLOutputter outputter = new XMLOutputter(format); 274 //利用outputer將document對象轉換為xml文檔 275 outputter.output(document, new FileOutputStream(new File("src/com/sxt/test/res.xml"))); 276 } 277 278 279 /* 280 * JDOM解析 281 */ 282 @Test 283 public void testfun3() throws JDOMException, IOException { 284 // 創建一個SAXBuilder的對象 285 SAXBuilder saxBuilder = new SAXBuilder(); 286 // 創建一個輸入流,將XML文件載入到輸入流中 287 InputStream in = new FileInputStream("src/com/sxt/test/bookes.xml"); 288 InputStreamReader isr = new InputStreamReader(in); 289 // 通過saxBuilder的build方法,將輸入流載入到saxBuilder中 290 org.jdom2.Document document = saxBuilder.build(isr); 291 // 通過docment對象獲取xml文件的根節點 292 org.jdom2.Element rootElement = document.getRootElement(); 293 // 獲取根節點下麵的位元組點 294 List<org.jdom2.Element> bookList = rootElement.getChildren(); 295 // 繼續進行解析 296 for (org.jdom2.Element book : bookList) { 297 Book bookEntity = new Book(); 298 System.out.println("=====開始解析第" + (bookList.indexOf(book) + 1) 299 + "書====="); 300 // 解析book的屬性 301 List<Attribute> attrList = book.getAttributes(); 302 // 遍歷attribute(針對不清楚book節點下屬性名字及數量) 303 for (Attribute attr : attrList) { 304 // 獲取屬性名 305 String attrName = attr.getName(); 306 // 獲取屬性值 307 String attrvValue = attr.getValue(); 308 System.out.println("屬性名:" + attrName + "----屬性值:" + attrvValue); 309 if (attrName.equals("id")) { 310 bookEntity.setId(attrvValue); 311 } 312 } 313 List<org.jdom2.Element> bookchilds = book.getChildren(); 314 for (org.jdom2.Element child : bookchilds) { 315 System.out.println("節點名:" + child.getName() + "----節點值:" 316 + child.getValue()); 317 if (child.getName().equals("name")) { 318 bookEntity.setName(child.getValue()); 319 } else if (child.getName().equals("author")) { 320 bookEntity.setAuthor(child.getValue()); 321 } else if (child.getName().equals("year")) { 322 bookEntity.setYear(child.getValue()); 323 } else if (child.getName().equals("price")) { 324 bookEntity.setPrice(child.getValue()); 325 } else if (child.getName().equals("language")) { 326 bookEntity.setLanguage(child.getValue()); 327 } 328 } 329 System.out.println("=====結束解析第" + (bookList.indexOf(book) + 1) 330 + "書====="); 331 booksList.add(bookEntity); 332 bookEntity = null; 333 System.out.println(bookList.size()); 334 System.out.println("=======" + booksList.get(0).getId()); 335 System.out.println("=======" + booksList.get(0).getAuthor()); 336 } 337 } 338 339 /* 340 * 創建DOM4J 341 */ 342 @Test 343 public void parseXML4J() throws DocumentException { 344 // 創建SAXReader的對象reader 345 SAXReader reader = new SAXReader(); 346 // 通過reader對象的read方法載入book.xml文件 347 org.dom4j.Document document = reader.read(new File( 348 "src/com/sxt/test/bookes.xml")); 349 // 通過document對象獲取根節點bookstore 350 org.dom4j.Element bookStore = document.getRootElement(); 351 // 通過element對象的elementIterator方法獲取迭代器 352 Iterator<org.dom4j.Element> it = bookStore.elementIterator(); 353 // 遍歷迭代器,獲取根節點中的信息(書籍) 354 while (it.hasNext()) { 355 System.out.println("=========開始遍歷某一本書==========="); 356 org.dom4j.Element book = (org.dom4j.Element) it.next(); 357 // 獲取book的屬性值和屬性名 358 List<org.dom4j.Attribute> bookattrs = book.attributes(); 359 for (org.dom4j.Attribute attr : bookattrs) { 360 System.out.println("--節點名:" + attr.getName() + "--節點值:" 361 + attr.getValue()); 362 } 363 Iterator itt = book.elementIterator(); 364 while (itt.hasNext()) { 365 org.dom4j.Element bookChild = (org.dom4j.Element) itt.next(); 366 System.out.println("節點名" + bookChild.getName() + "---節點值" 367 + bookChild.getStringValue()); 368 } 369 System.out.println("=========結束遍歷某一本書==========="); 370 } 371 } 372 373 /* 374 * 創建DOM4J 375 */ 376 @Test 377 public void testfun8() throws Exception { 378 //創建document對象,代表整個xml文檔 379 org.dom4j.Document document = org.dom4j.DocumentHelper.createDocument(); 380 //創建根節點rss 381 org.dom4j.Element rss = document.addElement("rss"); 382 //向rss節點中添加version屬性 383 rss.addAttribute("version", "2.0"); 384 //生成子節點及節點內容 385 org.dom4j.Element channel = rss.addElement("channel"); 386 org.dom4j.Element title = channel.addElement("title"); 387 title.setText("國內最新新聞"); 388 //設置生成xml格式 389 OutputFormat format = OutputFormat.createPrettyPrint(); 390 format.setEncoding("GBK"); 391 //生成xml文件 392 File file = new File("src/com/sxt/test/rssnews.xml"); 393 XMLWriter writer = new XMLWriter(new FileOutputStream(file),format); 394 writer.write(document); 395 writer.close(); 396 } 397 398 /* 399 * DOM4J解析 400 */ 401 @Test 402 public void testfun4() throws DocumentException { 403 // 創建SAXReader的對象reader 404 SAXReader reader = new SAXReader(); 405 <