SAX解析实例
//以下代码统计url.xml文件中每个标签出现的次数 package com.app; import java.io.File; 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 TagCounter extends DefaultHandler { // Hashtable用来记录tag出现的次数 private Hashtable tags; 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"); } } // 对每一个开始元属进行处理 public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException { String key = rawName; Object value = tags.get(key); if (value == null) { // 如果是新标签,把它添加在Hastable中 tags.put(key, new Integer(1)); } else { // 如果以前碰到过,得到其计数值,并加1 int count = ((Integer) value).intValue(); count++; tags.put(key, new Integer(count)); } } static public void main(String[] args) { String filename = null; filename = "url.xml"; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxParser = null; try { // 创建解析器SAXParser对象 saxParser = spf.newSAXParser(); saxParser.parse(new File(filename), new TagCounter()); } catch (Exception ex) { ex.printStackTrace(); } } } //以下程序解析url.xml package com.app; import java.io.File; import java.io.IOException; import java.util.Stack; 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 SAXXMLReader extends DefaultHandler { Stack tags = new Stack(); // 用于保存解析出来的信息 String text = null; String url = null; String author = null; String description = null; String day = null; String year = null; String month = null; public void endDocument() throws SAXException { System.out.println("------解析结束--------"); } public void startDocument() throws SAXException { System.out.println("------解析开始--------"); } public void startElement(String p0, String p1, String p2, Attributes p3) throws SAXException { tags.push(p2); } public void endElement(String p0, String p1, String p2) throws SAXException { tags.pop(); if (p2.equals("link")) parser(); } public void characters(char[] p0, int p1, int p2) throws SAXException { // 察看栈顶元素,根据元素名称给对应的变量赋值 String tag = (String) tags.peek(); if (tag.equals("text")) text = new String(p0, p1, p2); else if (tag.equals("url")) url = new String(p0, p1, p2); else if (tag.equals("author")) author = new String(p0, p1, p2); else if (tag.equals("day")) day = new String(p0, p1, p2); else if (tag.equals("month")) month = new String(p0, p1, p2); else if (tag.equals("year")) year = new String(p0, p1, p2); else if (tag.equals("description")) description = new String(p0, p1, p2); } private void parser() { System.out.print("Content: "); System.out.println(text); System.out.print("URL: "); System.out.println(url); System.out.print("Author: "); System.out.println(author); System.out.print("Date: "); System.out.println(day + "-" + month + "-" + year); System.out.print("Description: "); System.out.println(description); System.out.println(); } static public void main(String[] args) { String filename = "url.xml"; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxParser = null; try { saxParser = spf.newSAXParser(); saxParser.parse(new File(filename), new SAXXMLReader()); } catch (Exception ex) { ex.printStackTrace(); } } }
http://www.5itjob.com
http://www.rjpx.net