• 四种解析和创建方式(DOM,SAX,DOM4J,JDOM)


    一、先导入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 // 通过reader对象的read方法加载book.xml文件 406 org.dom4j.Document document = reader.read(new File( 407 "src/com/sxt/test/bookes.xml")); 408 // 通过document对象获取根节点bookstore 409 org.dom4j.Element bookStore = document.getRootElement(); 410 // 通过element对象的elementIterator方法获取迭代器 411 Iterator<org.dom4j.Element> it = bookStore.elementIterator(); 412 // 遍历迭代器,获取根节点中的信息(书籍) 413 while (it.hasNext()) { 414 System.out.println("=========开始遍历某一本书==========="); 415 org.dom4j.Element book = (org.dom4j.Element) it.next(); 416 // 获取book的属性值和属性名 417 List<org.dom4j.Attribute> bookattrs = book.attributes(); 418 for (org.dom4j.Attribute attr : bookattrs) { 419 System.out.println("--节点名:" + attr.getName() + "--节点值:" 420 + attr.getValue()); 421 } 422 Iterator itt = book.elementIterator(); 423 while (itt.hasNext()) { 424 org.dom4j.Element bookChild = (org.dom4j.Element) itt.next(); 425 System.out.println("节点名" + bookChild.getName() + "---节点值" 426 + bookChild.getStringValue()); 427 } 428 System.out.println("=========结束遍历某一本书==========="); 429 } 430 } 431 432 @Test 433 public void testDemo() throws Exception { 434 System.out.println("性能测试"); 435 // 测试DOM的性能 436 long start1 = System.currentTimeMillis(); 437 testfun1(); 438 System.out.println("DOM: " + (System.currentTimeMillis() - start1)); 439 // 测试DOM的性能 440 long start2 = System.currentTimeMillis(); 441 testfun2(); 442 System.out.println("SAX: " + (System.currentTimeMillis() - start2)); 443 // 测试DOM的性能 444 long start3 = System.currentTimeMillis(); 445 testfun3(); 446 System.out.println("JDOM: " + (System.currentTimeMillis() - start3)); 447 // 测试DOM的性能 448 long start4 = System.currentTimeMillis(); 449 testfun4(); 450 System.out.println("DOM4J: " + (System.currentTimeMillis() - start4)); 451 } 452 }
     1 package com.sxt.test;
     2 
     3 import org.xml.sax.Attributes;
     4 import org.xml.sax.SAXException;
     5 import org.xml.sax.helpers.DefaultHandler;
     6 
     7 public  class MyContentHandler extends DefaultHandler{
     8     @Override
     9     public void startDocument() throws SAXException {
    10         // TODO Auto-generated method stub
    11         System.out.println("开始解析");
    12     }
    13     @Override
    14     public void endDocument() throws SAXException {
    15         // TODO Auto-generated method stub
    16         System.out.println("结束解析");
    17     }
    18     @Override
    19     public void startElement(String uri, String localName, String qName,
    20             Attributes attributes) throws SAXException {
    21         // TODO Auto-generated method stub
    22         System.out.println("元素解析开始");
    23     }
    24     @Override
    25     public void endElement(String uri, String localName, String qName)
    26             throws SAXException {
    27         // TODO Auto-generated method stub
    28         System.out.println("元素解析结束");
    29     }
    30     @Override
    31     public void characters(char[] ch, int start, int length)
    32             throws SAXException {
    33         // TODO Auto-generated method stub
    34         String s = new String(ch,start,length);
    35         if(s.trim().isEmpty()){
    36             return;
    37         }
    38         System.out.println("文本内容为"+s);
    39     }
    40     @Override
    41     public void ignorableWhitespace(char[] ch, int start, int length)
    42             throws SAXException {
    43         // TODO Auto-generated method stub
    44         System.out.println("文本内容为空");
    45     }
    46     @Override
    47     public void processingInstruction(String target, String data)
    48             throws SAXException {
    49         // TODO Auto-generated method stub
    50         System.out.println("处理指令");
    51     }
    52 }
  • 相关阅读:
    【整理】Linux 下 自己使用的 debug宏 printf
    STM32学习笔记:创建标准库工程模板
    【闲谈】第一份实习(下)
    Ceres-Solver库入门
    ceres-solver库使用示例
    ceres-solver库编译说明
    October 23, 2013
    Pollution over East China : Image of the Day
    谈谈PCI的GXL
    InfoQ访谈:Webkit和HTML5的现状和趋势
  • 原文地址:https://www.cnblogs.com/hgc-bky/p/5907186.html
Copyright © 2020-2023  润新知