sax解析

package mySaxDo;

import java.util.LinkedList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ConfigHandler extends DefaultHandler
{
    
    
    private List<String> list;
    
    public static int charCount;
    
    public static int startCount;
    
    public static int endCount;
    
    /**
     * 接收元素中字符数据的通知。
     * 
     * @param ch 读取的字节
     * @param start
     * @param length
     * @throws SAXException
     */
    
    @Override
    public void characters(char[] ch, int start, int length)
        throws SAXException
    {
        // TODO 获取栈数据,然后读取,不删除栈数据stack.peek();
        String contend = new String(ch, start, length);
        if ("".equals(contend.trim()))
            return;
        System.out.println("charCount:" + charCount++);
        System.out.println(contend);
        initMap(ch, start, length);
    }
    
    private void initMap(char[] ch, int start, int length)
    {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < list.size(); i++)
        {
            
            builder.append(list.get(i));
            if (i < list.size() - 1)
                builder.append("/");
        }
        System.out.printf("path :%s,value : %s", builder.toString(), new String(ch, start, length));
        ConfigUtil.getMap().put(builder.toString(), new String(ch, start, length));
    }
    
    /**
     * 接收元素结束的通知。
     * 
     * @param uri
     * @param localName
     * @param qName
     * @throws SAXException
     */
    @Override
    public void endElement(String uri, String localName, String qName)
        throws SAXException
    {
        System.out.println("endCount:" + endCount++);
        System.out.println(qName);
        list.remove(qName);
    }
    
    /**
     * 接收元素开始的通知。
     * 
     * @param uri
     * @param localName
     * @param qName
     * @param attributes
     * @throws SAXException 时间流读取异常
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException
    {
        System.out.println("startCount:" + startCount++);
        System.out.println(qName);
        list.add(qName);
        
    }
    
    public ConfigHandler()
    {
        list = new LinkedList<String>();
    }


}
 
package mySaxDo;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class ConfigUtil
{
    private static Map<String, String> map = new HashMap<String, String>();
    
    public static void main(String[] args)
    {
        File file = new File("template.xml");
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try
        {
            SAXParser parser = factory.newSAXParser();
            parser.parse(file, new ConfigHandler());
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    public static Map<String, String> getMap()
    {
        return map;
    }
    
}
 

你可能感兴趣的:(xml)