SAX RSS FEED解析器

时间:2012-10-16 03:30:12

标签: android

我正在尝试使用SAX解析器解析RSS提要

这是我的代码:

public class MainActivity extends Activity {
    ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
    ListView list;
    ArrayList<String> sinlgeItem = null;
    ProgressDialog pd;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list=(ListView)findViewById(R.id.list);
        boolean b=parseData();
        Log.v("result", "value"+b);
        for(int i=0;i<data.size();i++){
            Log.e("ITEM",data.get(i).get(0)+"__"+data.get(i).get(1));
        }
    }
    /**
     * method parse the data
     */
    private boolean parseData() {
        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            URL url = new URL("https://itunes.apple.com/WebObjects/MZStore.woa/wpa/MRSS/newreleases/sf=143441/limit=25/rss.xml");
            xr.setContentHandler(new MyHandler());
            xr.parse(new InputSource(url.openStream()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public class MyHandler extends DefaultHandler{
        Boolean ITEM=false;
        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            super.characters(ch, start, length);
        }
        @Override
        public void endElement(String uri, String localName, String name) throws SAXException {
            super.endElement(uri, localName, name);
            if (ITEM){
                RootElement root=new RootElement("rss");
                Element chan=root.getChild("channel");
                Element itms=chan.getChild("item");
                Element title=itms.getChild("title");
                Element artist=itms.getChild("http://phobos.apple.com/rss/1.0/modules/itms/", "artist");
                title.setEndTextElementListener(new EndTextElementListener() {
                    public void end(String body) {
                        sinlgeItem.add(body);
                        Log.v("title",body);
                    }
                 });
                 artist.setEndTextElementListener(new EndTextElementListener() {
                     public void end(String body) {
                         sinlgeItem.add(body);
                         Log.v("artist", body);
                     }
                 });
                 ITEM=false;
                 data.add(sinlgeItem);
             }
         }

         @Override
    public void startDocument() throws SAXException {
        super.startDocument();

    }

    @Override
    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
             super.startElement(uri, localName, name, attributes);
             if (localName.equals("item")){
                 sinlgeItem = new ArrayList<String>();
                 ITEM=true;
             }
         }
     }
}

这是xml Feed的链接 https://itunes.apple.com/WebObjects/MZStore.woa/wpa/MRSS/newreleases/sf=143441/limit=25/rss.xml

我正在尝试使用item标记解析title元素,并在item标记中解析元素itms:artist。 我不知道如何处理带有名称空间的标签

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

我认为您不应该使用SAXParser来解析RSS。使用XML Pull Parser会更容易。我会选择SAXParser来解析像XMPP协议这样的连续客户端 - 服务器通信,因为SAX最适合解析不完整和连续的XML。