实现有价值的IT服务
全部博文(709)
分类: Java
2006-12-10 19:04:09
遇到的项目 |
方法回调 |
{文档开始} | startDocument() |
startElement(null,"POEM",null,{Attributes}) | |
"\n" | characters(" |
startElement(null,"AUTHOR",null,{Attributes}) | |
"Ogden Nash" | characters(" |
endElement(null,"AUTHOR",null) | |
"\n" | characters(" |
startElement(null,"TITLE",null,{Attributes}) | |
"Fleas" | characters(" |
endElement(null,"TITLE",null) | |
"\n" | characters(" |
startElement(null,"LINE",null,{Attributes}) | |
"Adam" | characters(" |
endElement(null,"LINE",null) | |
"\n" | characters(" |
endElement(null,"POEM",null) | |
{文档结束} | endDocument() |
void characters(char[] ch, int start, int length): |
void startDocument(): |
void endDocument(): |
void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, Attributes atts) |
void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName) |
import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; import java.util.*; import java.io.*; |
public class SAXCounter extends DefaultHandler { private Hashtable tags; //这个Hashtable用来记录tag出现的次数 // 处理文档前的工作 public void startDocument() throws SAXException { tags = new Hashtable();//初始化Hashtable } //对每一个开始元属进行处理 public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException { String key = localName; 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)); } } //解析完成后的统计工作 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"); } } //程序入口,用来完成解析工作 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 { // 创建一个解析器SAXParser对象 saxParser = spf.newSAXParser(); // 得到SAXParser中封装的SAX XMLReader xmlReader = saxParser.getXMLReader(); } catch (Exception ex) { System.err.println(ex); System.exit(1); } try { //使用指定的ContentHandler,解析给XML文件,这儿要注意的是,为了 //程序的简单起见,这儿将主程序和ContentHandler放在了一起。实际上 //main方法中所作的所有事情,都与ContentHandler无关。 xmlReader.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); } } } |
public class SAXReader extends DefaultHandler { java.util.Stack tags=new java.util.Stack(); //--------------XML Content------------- 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("------Parse End--------"); } public void startDocument() throws SAXException { System.out.println("------Parse Begin--------"); } public void startElement(String p0, String p1, String p2, Attributes p3) throws SAXException { tags.push(p1); } public void endElement(String p0, String p1, String p2) throws SAXException { tags.pop(); //一个link节点的信息收集齐了,将其格式化输出 if (p1.equals("link")) printout(); } 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")) year=new String(p0,p1,p2); } private void printout(){ 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 = null; boolean validation = false; filename="links.xml"; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxParser=null; try { saxParser = spf.newSAXParser(); } catch (Exception ex) { System.err.println(ex); System.exit(1); } try { saxParser.parse(new File(filename),new SAXReader()); } catch (SAXException se) { System.err.println(se.getMessage()); System.exit(1); } catch (IOException ioe) { System.err.println(ioe); System.exit(1); } } } |