SAX解析xml文件

来源:互联网 发布:mac水漾轻盈粉底液色号 编辑:程序博客网 时间:2024/03/29 02:57

package com.motorola.snow.saxPaser;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;
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 MyDefaultHandler extends DefaultHandler {
   
    static Hashtable tags = new Hashtable();
   
    String name = null;
    String author = null;
    String time = null;
    String description = null;
   
    @Override
    /**
     * 该方法打印出每个节点的内容<name>tang</name>则打印出tang
     */
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        StringBuffer buffer = new StringBuffer();
        for (int i = start; i < start+length; i++) {
            buffer.append(ch[i]);
        }
        System.out.print(buffer);
    }

    @Override
    /**
     * 文件读到最后要采取的方法
     */
    public void endDocument() throws SAXException {
        System.out.println("-----paser end-----");
    }

    @Override
    /**
     * 每个节点结束后执行的方法
     */
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        System.out.print("<"+qName+">");
       
    }

    @Override
    public void startDocument() throws SAXException {
        System.out.println("-----paser begin-----");
    }

    @Override
    /**
     * 每当读到一个新节点所调用的方法,这里是统计每个节点出现的次数
     */
   
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        System.out.print("<"+qName+">");
        String key = qName;
        Object value = tags.get(key);
        if(value == null){
            tags.put(key, new Integer(1));
        }else{
            int count = ((Integer)value).intValue();
            count++;
            tags.put(key, count);
        }
    }

    public static void main(String[] args) {
        String fileName = "links.xml";
        //由SAXPaser工厂创建出一个工厂用来生成SAXPaser实例
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxPaser = null;
        try {
            //生成SAXPaser实例
            saxPaser = factory.newSAXParser();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
       
        try {
            //解析"links.xml"文件new MyDefaultHandler()自己写的继承类重写了部分的方法
            saxPaser.parse(new File(fileName), new MyDefaultHandler());
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
       
        //将统计所得的结果打印出来
        for(Iterator i = tags.keySet().iterator() ; i.hasNext() ; ){
            String key = i.next().toString();
            String value = tags.get(key).toString();
            System.out.println(key+"---"+value);
        }
    }
       
}


links.xml

 

<?xml version="1.0" encoding="UTF-8"?>
 <books  count="3" xmlns="http://test.org/books"> 
     <!--books's comment--> 
    <book id="1"> 
         <name>Thinking in JAVA</name>
         <author>Bruce</author>
         <time>2000522</time>
         <description>very good</description>
     </book> 
     <book id="2"> 
        <name>Core JAVA2</name>
        <author>Eric</author>
        <time>20010521</time>
    </book> 
    <book id="3"> 
         <name>C++ primer</name> 
         <author>Tomion</author>
         <description>very much good</description>
     </book> 
 </books> 

 

result:

 

-----paser begin-----
<books> 
      
    <book> 
         <name>Thinking in JAVA<name>
         <author>Bruce<author>
         <time>2000522<time>
         <description>very good<description>
     <book> 
     <book> 
        <name>Core JAVA2<name>
        <author>Eric<author>
        <time>20010521<time>
    <book> 
    <book> 
         <name>C++ primer<name> 
         <author>Tomion<author>
         <description>very much good<description>
     <book> 
 <books>-----paser end-----
name---3
book---3
author---3
description---2
time---2
books---1

 

原创粉丝点击