zoukankan      html  css  js  c++  java
  • Java 内置的读取XML方法

    URL url = new URL(urlString);
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    XMLReader xmlReader = parser.getXMLReader();
    //RssHandler:XML 节点读取方式实现 RssHandler rssHandler
    = new RssHandler(); xmlReader.setContentHandler(rssHandler); InputSource inputSource = new InputSource(url.openStream()); xmlReader.parse(inputSource); return rssHandler.getRssFeed();

     RssHandler 帮助类

    public class RssHandler extends DefaultHandler {
        //多个Item集合
        RSSFeed rssFeed ;
        //单个Item实例
        RssItem rssItem; 
        CurrentState currentState = CurrentState.None;
        
        public RssHandler(){}
        
        public RSSFeed getRssFeed(){
            return this.rssFeed;
        }
        
        public void startDocument()throws SAXException {
            this.rssFeed = new RSSFeed();
            this.rssItem = new RssItem();
        }
        
        public void startElement(String _URI,String localName,String qNameString,Attributes atts)throws SAXException 
        {
            if(localName.equals("channel")){
                currentState = CurrentState.None; 
            }
            else if(localName.equals("item")){
                this.rssItem = new RssItem(); 
            }
            else if(localName.equals("title")){
                currentState = CurrentState.TITLE; 
            }
            else if(localName.equals("description")){
                currentState = CurrentState.DESCRIPTION; 
            }
            else if(localName.equals("link")){
                currentState = CurrentState.LINK; 
            }
            else if(localName.equals("category")){
                currentState = CurrentState.CATEGORY; 
            }
            else if(localName.equals("pubDate")){
                currentState = CurrentState.PUBDATE; 
            } 
        }
        
        public void endElement(String _URI,String localName,String qName)throws SAXException
        {
            if(localName.equals("item")){
                this.rssFeed.addItem(this.rssItem);
            }
        }
        
        public void characters(char ch[],int start,int length)
        {
            String theString = new String(ch,start,length);
            switch (currentState) {
            case TITLE:
                this.rssItem.title(theString);
                currentState = CurrentState.None;
                break;
            case LINK:
                this.rssItem.link(theString);
                currentState = CurrentState.None;
                break;
            case DESCRIPTION:
                this.rssItem.description(theString);
                currentState = CurrentState.None;
                break;
            case CATEGORY:
                this.rssItem.category(theString);
                currentState = CurrentState.None;
                break;
            case PUBDATE:
                this.rssItem.publicDate(theString);
                currentState = CurrentState.None;
                break; 
            default:
                break;
            }
        } 
        /*
         * 节点枚举
         */
         public enum CurrentState{
            /*
             * 无
             */
            None,
            TITLE,
            LINK,
            DESCRIPTION,
            CATEGORY,
            PUBDATE 
        }
    } 
  • 相关阅读:
    AMD 开源照片级渲染引擎 Radeon ProRender
    删除集群mds
    删除集群mds
    一次旅途她和豆浆结缘,如今拥有70多家加盟店
    小伙居然开网店卖花,整整一年时间他做出了400万元的业绩
    带着800元现金开始创业,居然开出了十几家连锁超市
    北大学霸从小米离职,靠眼镜打开亿万市场
    广西农民靠养猪发家致富,采用新模式既有效益又保护生态
    在母婴产品领域他独领风骚,利润已经突破了1亿
    80后小伙白手起家做照明生意,为他创造了六千万元的业绩
  • 原文地址:https://www.cnblogs.com/you000/p/2797514.html
Copyright © 2011-2022 走看看