SAX操作

package com.main;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class SAXTest extends DefaultHandler {
    
    private class Item{
        private String title ;
        private String description;
        private String converter;
        
        public String getConverter() {
            return converter;
        }
        public void setConverter(String converter) {
            this.converter = converter;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
    }
    
    private Map<String, Item> itemMap = new HashMap<String, Item>();
    private Item item ;
    private int currentstate = 0;
    private final int TITLE = 2;
    private final int DESCRIPTION = 3;
    
    public Map<String, Item> readUrl(String uri){
        try{
            URL url = new URL(uri);
            
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader xmlReader = parser.getXMLReader();
            
            xmlReader.setContentHandler(this);
            InputSource is = new InputSource(url.openStream());
            xmlReader.parse(is);
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return itemMap;
    }
    
    
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        String str  = new String(ch, start, length);
//        System.out.println("+++++++++++++++++++currStr:"+str);
        if(this.item==null){
            return;
        }
        switch(this.currentstate){
            case TITLE:
                this.item.setTitle(str);
                this.currentstate = 0;
                break;
            case DESCRIPTION:
                this.item.setDescription(str);
                this.currentstate = 0;
                break;
            default:
                return;
        }
    }


    public void endDocument() throws SAXException {
        
    }


    @Override
    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
//        System.out.println("localName:"+localName+"------- name:"+name);
        if("item".equals(name)){
            if(this.item!=null){
                this.itemMap.put(this.item.getTitle(), this.item);
            }
            this.item = new Item();
            currentstate = 1;
            return;
        }
        if("title".equals(name)){
            currentstate = 2;
        }
        if("description".equals(name)){
            currentstate = 3;
        }
    }
    
    
    public static String getConverter(String description){
        String converter = "";
        String find = "//d{1,}(//.//d+|)";
        Pattern p = Pattern.compile(find);
        Matcher m = p.matcher(description);
        if(m.find()){
            converter = m.group();
        }
        return converter;
    }
    
    
    
    public static void main(String[] args) {
        SAXTest sax = new SAXTest();
        Map<String, Item> tempMap = sax.readUrl("http://themoneyconverter.com/AED/rss.xml");
        int i = 0;
        for(Entry<String, Item> entrySet : tempMap.entrySet()){
            String key = entrySet.getKey();
            Item item = entrySet.getValue();
            item.setConverter(SAXTest.getConverter(item.getDescription().substring(item.getDescription().indexOf("=")==-1?0:item.getDescription().indexOf("="),item.getDescription().length())));
            System.out.println("Description:"+item.getDescription());
            System.out.println("转换:"+key+"---汇率:"+item.getConverter());
            i++;
        }
        System.out.println("共有数据:"+i);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值