?? saxtagcount.java
字號(hào):
import javax.xml.parsers.*;import org.xml.sax.*;import org.xml.sax.helpers.*;import java.util.*;import java.io.*;public class SAXTagCount extends DefaultHandler { private Hashtable tagsList; // XML文件的開(kāi)始 public void startDocument() throws SAXException { // 建立標(biāo)記的Hashtable對(duì)象 tagsList = new Hashtable(); } // 元素的開(kāi)始 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { String tagName = qName; int count; // 檢查是否有此標(biāo)記 Object value = tagsList.get(tagName); if (value == null) { // 不存在 count = 1; } else { // 存在, 加一 count = ((Integer)value).intValue(); count++; } tagsList.put(tagName, new Integer(count)); } // 元素的內(nèi)容 public void characters(String chars) throws SAXException { } // 元素的結(jié)束 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { } // 文件結(jié)束 public void endDocument() throws SAXException { Enumeration enum = tagsList.keys(); while (enum.hasMoreElements()) { String tagName = (String)enum.nextElement(); // 獲取計(jì)數(shù) int count = ((Integer)tagsList.get(tagName)).intValue(); System.out.println(tagName + " (" + count + ")"); } } // 將文件名轉(zhuǎn)換成URL private static String convertToURL(String name) { String path = new File(name).getAbsolutePath(); if (File.separatorChar != '/') { path = path.replace(File.separatorChar, '/'); } if (!path.startsWith("/")) { path = "/" + path; } return "file:" + path; } static public void main(String[] args) { String filename = null; boolean validation = false; filename = "08_01.xml"; SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(validation); XMLReader xmlReader = null; try { // 創(chuàng)建一個(gè)JAXP SAXParser SAXParser saxParser = spf.newSAXParser(); // 得到封裝的SAX XMLReader xmlReader = saxParser.getXMLReader(); } catch (Exception ex) { System.err.println(ex); System.exit(1); } // 設(shè)置XMLReader的ContentHandler xmlReader.setContentHandler(new SAXTagCount()); // 在分析之前設(shè)置ErrorHandler xmlReader.setErrorHandler(new MyErrorHandler()); try { // 告訴XMLReader分析XML文檔 xmlReader.parse(convertToURL(filename)); } catch (SAXException se) { System.err.println(se.getMessage()); System.exit(1); } catch (IOException ioe) { System.err.println(ioe); System.exit(1); } } // 報(bào)告錯(cuò)誤和警告 private static class MyErrorHandler implements ErrorHandler { //得到分析異常的細(xì)節(jié) private String getParseExceptionInfo(SAXParseException spe) { String systemId = spe.getSystemId(); if (systemId == null) { systemId = "null"; } String info = "URI=" + systemId + " Line=" + spe.getLineNumber() + ": " + spe.getMessage(); return info; } public void warning(SAXParseException spe) throws SAXException { System.out.println("Warning: " + getParseExceptionInfo(spe)); } public void error(SAXParseException spe) throws SAXException { String message = "Error: " + getParseExceptionInfo(spe); throw new SAXException(message); } public void fatalError(SAXParseException spe) throws SAXException { String message = "Fatal Error: " + getParseExceptionInfo(spe); throw new SAXException(message); } }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -