Android: читать RSS со сложным xml

Сегмент RSS xmlа, данные из которого мне надо получить, выглядит следующим образом:



Благословение на Эрец-Исроэль, предисловие к благословению «Йерушалаим». Комментарии к молитве 73

Tue, 06 Mar 2012 05:00:00 GMT

http://toldot.ru/tv/video/video_17869.html?template=168


Цикл видеоуроков рава Элиезера Ксидо, посвященный смыслу еврейских молитв


http://toldot.ru/tv/video/video_17869.html?template=168

Рав Элиезер Ксидо




Мне надо было получить не тольно значения из полей типа «description» и «author», но также для «media:content» и «media:thumbnail» с атрибутами.

В основу я взял проект, который нашел тут RSSReader
Но сделал некоторые изменения в коде, чтобы получить необходимую информацию.
Первым делом я изменил файл RSSItem.java

public class RSSItem {
        public String title;
        public String date;
        public String link;
        public String description;
        public String author;
        public String pubDate;
        public String thumbnail;
        public String videoURL;
        public String audioURL;
        public Double videoSize;
        public Double audioSize;
        public Double videoTime;
        public Double audioTime;
}

Это значит, что опредленный конечный масси будет иметь такие параметры.

Далее, я изменил коллекцию тэгов в файле RSSParser.java, которые должны парситься:

private final static String[] xmltags = { "title", "link", "pubDate", "description", "author", "pubDate", "thumbnail", "content"};

Дополнительно в этом же файле я добавил код для получения данных из тэгов «media:content» м «media:thumbnail»

@Override
        public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
                super.startElement(uri, localName, qName, attributes);
                
                if(qName.equalsIgnoreCase(TAG_ITEM))
                {
                        currentitem = new RSSItem();
                        currentindex = -1;
                        isParsing = true;
                        globalAttrs=null;
                        itemarray.add(currentitem);
                }
                else
                {
                        currentindex = itemIndexFromString(localName);
                        
                        builder = null;
                        
                        if(-1 != currentindex)
                                builder = new StringBuilder();
                        
                }
                if (localName.trim().equals("thumbnail")) {          
                    thumbnail = attributes.getValue("url");            
                }
                if (localName.trim().equals("content")) {
                	String media = attributes.getValue("medium");
                	if ( media.contains("video"))
                	{
                		video = attributes.getValue("url");
                		videoSize = Double.parseDouble(attributes.getValue("fileSize"));
                		videoTime = Double.parseDouble(attributes.getValue("duration"));
                	}
                	if ( media.contains("audio"))
                	{
                		audio = attributes.getValue("url"); 
                		audioSize = Double.parseDouble(attributes.getValue("fileSize"));
                		audioTime = Double.parseDouble(attributes.getValue("duration"));
                	}     
                }
                
        }
        
                
        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
                super.endElement(uri, localName, qName);
                
                if(qName.equalsIgnoreCase(TAG_ITEM))
                {
                        isParsing = false;
                }
                else if(currentindex != -1)
                {
                	
                        if(isParsing)
                        {
                                switch(currentindex)
                                {
                                        case 0: currentitem.title = builder.toString();                 break; 
                                        case 1: currentitem.link = builder.toString();                  break;
                                        case 2: currentitem.date = builder.toString();                  break;
                                        case 3: currentitem.description= builder.toString();    break;
                                        case 4: currentitem.author = builder.toString();    break;
                                        case 5: currentitem.pubDate = builder.toString();    break;
                                        case 6: currentitem.thumbnail =  thumbnail; break;
                                        case 7: 
                                        	currentitem.videoURL =  video; 
                                        	currentitem.audioURL =  audio;
                                        	if (audioSize != null)
                                        	{
                                        		currentitem.audioSize = audioSize;
                                        		currentitem.audioTime = audioTime;
                                        	}else{
                                        		currentitem.audioSize = (double) 0;
                                        		currentitem.audioTime = (double) 0;
                                        	}
                                        	if (videoSize != null)
                                        	{
                                        		currentitem.videoSize = videoSize;
                                        		currentitem.videoTime = videoTime;
                                        	}else{
                                        		currentitem.videoSize = (double) 0;
                                        		currentitem.videoTime = (double) 0;
                                        	}
                                        	break;
                                }
                        }
                }
        }

Вот и все. Теперь вы можете добавить полученные данные в ваш ListView.

TextView fileSizeTV = (TextView)view.findViewById(R.id.fileSize);
		fileSizeTV.setText(data.videoSize);

Leave a Reply

%d bloggers like this: