android RSS讀取

SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            XMLReader xmlReader = saxParser.getXMLReader();
            xmlReader.setContentHandler(new DefaultHandler() {
                @Override
                public void startDocument() throws SAXException {
                    super.startDocument();
                    Log.d(TAG, "開始解析整個xml文檔");
                }

                @Override
                public void endDocument() throws SAXException {
                    super.endDocument();
                    Log.d(TAG, "結束解析整個xml文檔");
                }

                @Override
                public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                    super.startElement(uri, localName, qName, attributes);
                    Log.d(TAG, "開始解析元素==>>" + qName);
                }

                @Override
                public void endElement(String uri, String localName, String qName) throws SAXException {
                    super.endElement(uri, localName, qName);
                    Log.d(TAG, "結束解析元素==>>" + qName);
                }

                @Override
                public void characters(char[] ch, int start, int length) throws SAXException {
                    super.characters(ch, start, length);
                    String text = new String(ch, start, length);
                    Log.i(TAG, "內容:" + text);
                }
            });
            //http://go.rss.sina.com.cn/redirect.php?url=http://news.sina.com.cn/c/2018-09-23/doc-ihkmwytn7224643.shtml
            URL url = new URL("http://rss.sina.com.cn/news/china/focus15.xml");
            //URL url = new URL("http://go.rss.sina.com.cn/redirect.php?url=http://news.sina.com.cn/c/2018-09-23/doc-ihkmwytn7224643.shtml");
            InputSource is = new InputSource(url.openStream());
            xmlReader.parse(is);

權限:

<uses-permission android:name="android.permission.INTERNET"/>

參考:

https://www.cnblogs.com/lizhangyong/p/8969938.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章