你的位置:首页 > Java教程

[Java教程]四种解析和创建方式(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. 16 import javax. 17 import javax. 18 import javax. 19 import javax. 20 import javax. 21 import javax. 22 import javax. 23 import javax. 24 import javax. 25 import javax. 26 import javax. 27 import javax. 28 import javax. 29 30 import org.dom4j.DocumentException; 31 import org.dom4j.io.OutputFormat; 32 import org.dom4j.io.SAXReader; 33 import org.dom4j.io. 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. 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. 45 import org. 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. 65 Document document = db.parse("src/com/sxt/test/bookes."); 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创建107 */108 @Test109 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.settrue);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.")));135 136 }137 138 /*139 * SAX解析140 */141 @Test142 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.", 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> parse164 // 获取一个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.", handler);171 172 return handler.getBookList();173 174 }175 176 /*177 * SAX创建XML文件178 */179 @Test180 public void testfun7() throws Exception {181 ArrayList<Book> bookList = parse182 //创建一个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对象对生成的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.");194 if(!f.exists()){195 f.createNewFile();196 }197 //创建一个Result对象,并且使其与handler关联198 StreamResult result = new StreamResult(new FileOutputStream(f));199 handler.setResult(result);200 //打开document201 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 //关闭document248 handler.endDocument();249 }250 251 /*252 * 创建JDOM253 */254 @Test255 public void create256 //生成一个根节点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 //创建273 new 274 //利用outputer将document对象转换为275 outputter.output(document, new FileOutputStream(new File("src/com/sxt/test/res.")));276 }277 278 279 /*280 * JDOM解析281 */282 @Test283 public void testfun3() throws JDOMException, IOException {284 // 创建一个SAXBuilder的对象285 SAXBuilder saxBuilder = new SAXBuilder();286 // 创建一个输入流,将287 InputStream in = new FileInputStream("src/com/sxt/test/bookes.");288 InputStreamReader isr = new InputStreamReader(in);289 // 通过saxBuilder的build方法,将输入流加载到saxBuilder中290 org.jdom2.Document document = saxBuilder.build(isr);291 // 通过docment对象获取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 * 创建DOM4J341 */342 @Test343 public void parse344 // 创建SAXReader的对象reader345 SAXReader reader = new SAXReader();346 // 通过reader对象的read方法加载book.347 org.dom4j.Document document = reader.read(new File(348 "src/com/sxt/test/bookes."));349 // 通过document对象获取根节点bookstore350 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 * 创建DOM4J375 */376 @Test377 public void testfun8() throws Exception {378 //创建document对象,代表整个379 org.dom4j.Document document = org.dom4j.DocumentHelper.createDocument();380 //创建根节点rss381 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 //设置生成389 OutputFormat format = OutputFormat.createPrettyPrint();390 format.setEncoding("GBK");391 //生成392 File file = new File("src/com/sxt/test/rssnews.");393 new new FileOutputStream(file),format);394 writer.write(document);395 writer.close();396 }397 398 /*399 * DOM4J解析400 */401 @Test402 public void testfun4() throws DocumentException {403 // 创建SAXReader的对象reader404 SAXReader reader = new SAXReader();405 // 通过reader对象的read方法加载book.406 org.dom4j.Document document = reader.read(new File(407 "src/com/sxt/test/bookes."));408 // 通过document对象获取根节点bookstore409 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 @Test433 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. 4 import org. 5 import org. 6  7 public class MyContentHandler extends DefaultHandler{ 8   @Override 9   public void startDocument() throws SAXException {10     // TODO Auto-generated method stub11     System.out.println("开始解析");12   }13   @Override14   public void endDocument() throws SAXException {15     // TODO Auto-generated method stub16     System.out.println("结束解析");17   }18   @Override19   public void startElement(String uri, String localName, String qName,20       Attributes attributes) throws SAXException {21     // TODO Auto-generated method stub22     System.out.println("元素解析开始");23   }24   @Override25   public void endElement(String uri, String localName, String qName)26       throws SAXException {27     // TODO Auto-generated method stub28     System.out.println("元素解析结束");29   }30   @Override31   public void characters(char[] ch, int start, int length)32       throws SAXException {33     // TODO Auto-generated method stub34     String s = new String(ch,start,length);35     if(s.trim().isEmpty()){36       return;37     }38     System.out.println("文本内容为"+s);39   }40   @Override41   public void ignorableWhitespace(char[] ch, int start, int length)42       throws SAXException {43     // TODO Auto-generated method stub44     System.out.println("文本内容为空");45   }46   @Override47   public void processingInstruction(String target, String data)48       throws SAXException {49     // TODO Auto-generated method stub50     System.out.println("处理指令");51   }52 }