SAX解析實例 – JAVA編程語言程序開發技術文章

//以下代碼統計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(); 
    // 用於保存解析出來的信息www.aiwalls.com  
    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(); 
        } 
    } 

 摘自  Java教程
 

發佈留言