?? saxcounter.java
字號:
package xmlwriter;import java.io.File;import java.io.IOException;import java.util.Enumeration;import java.util.Hashtable;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;public class SAXCounter extends DefaultHandler { private Hashtable tags; // Parser calls this once at the beginning of a document public void startDocument() throws SAXException { tags = new Hashtable(); } public void endDocument() throws SAXException { Enumeration e = tags.keys(); while (e.hasMoreElements()) { String tag = (String)e.nextElement(); int count = ((Integer)tags.get(tag)).intValue(); System.out.println("Tag <" + tag + "> occurs " + count + " times"); } } // Parser calls this for each element in a document public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException { String key = localName; Object value = tags.get(key); if (value == null) { // Add a new entry tags.put(key, new Integer(1)); } else { // Get the current count and increment it int count = ((Integer)value).intValue(); count++; tags.put(key, new Integer(count)); } } static public void main(String[] args) { String filename = null; boolean validation = false; filename="links.xml"; SAXParserFactory spf = SAXParserFactory.newInstance();// XMLReader xmlReader = null; SAXParser saxParser=null; try { // Create a JAXP SAXParser saxParser = spf.newSAXParser(); // Get the encapsulated SAX XMLReader //xmlReader = saxParser.getXMLReader(); } catch (Exception ex) { System.err.println(ex); System.exit(1); } try { saxParser.parse(new File(filename),new SAXCounter()); } catch (SAXException se) { System.err.println(se.getMessage()); System.exit(1); } catch (IOException ioe) { System.err.println(ioe); System.exit(1); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -