Note that there are some explanatory texts on larger screens.

plurals
  1. POcharacters method not called for the enclosure tag
    text
    copied!<p>In my application I have a class that extends the org.xml.sax.helpers.DefaultHandler class. It's purpose is to read rss feeds. My problem is that when reaching the "enclosure tag", the characters method doesn't get called, so I can't extract the image's url. Does anybody know why is this happening?</p> <pre><code>public class RSSHandler extends DefaultHandler { private RSSFeed feed; private RSSItem item; boolean bFoundChannel = false; final int RSS_TITLE = 1; final int RSS_LINK = 2; final int RSS_DESCRIPTION = 3; final int RSS_CATEGORY = 4; final int RSS_PUBDATE = 5; final int RSS_ENCLOSURE = 6; int depth = 0; int currentstate = 0; private final String CHANNEL_ELEMENT = "channel"; private final String IMAGE_ELEMENT = "image"; private final String ITEM_ELEMENT = "item"; private final String TITLE_ELEMENT = "title"; private final String DESCRIPTION_ELEMENT = "description"; private final String LINK_ELEMENT = "link"; private final String CATEGORY_ELEMENT = "category"; private final String PUBDATE_ELEMENT = "pubDate"; private final String ENCLOSURE_ELEMENT = "enclosure"; private final String LESS_THAN_SIGN = "&lt;"; private final String GREATER_THAN_SIGN = "&gt;"; private final String IMAGE_SOURCE_PATTERN = "src='(.*?)'"; /* * getFeed - this returns our feed when all of the parsing is complete */ public RSSFeed getFeed() { return feed; } public void startDocument() throws SAXException { // initialize our RSSFeed object - this will hold our parsed contents feed = new RSSFeed(); // initialize the RSSItem object - we will use this as a crutch to grab the info from the channel // because the channel and items have very similar entries.. item = new RSSItem(); } public void endDocument() throws SAXException { } public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException { System.out.println("###START_ELEMENT - localName: " + localName + "; qName: " + qName); depth++; if (localName.equals(CHANNEL_ELEMENT)) { currentstate = 0; return; } else if (localName.equals(IMAGE_ELEMENT)) { // record our feed data - we temporarily stored it in the item :) feed.setTitle(item.getTitle()); feed.setPubDate(item.getPubDate()); } else if (localName.equals(ITEM_ELEMENT)) { // create a new item item = new RSSItem(); return; } else if (localName.equals(TITLE_ELEMENT)) { currentstate = RSS_TITLE; return; } else if (localName.equals(DESCRIPTION_ELEMENT)) { currentstate = RSS_DESCRIPTION; return; } else if (localName.equals(LINK_ELEMENT)) { currentstate = RSS_LINK; return; } else if (localName.equals(CATEGORY_ELEMENT)) { currentstate = RSS_CATEGORY; return; } else if (localName.equals(PUBDATE_ELEMENT)) { currentstate = RSS_PUBDATE; return; } else if (localName.equals(ENCLOSURE_ELEMENT)) { System.out.println("====&gt;&gt; ENCLOSURE_ELEMENT"); currentstate = RSS_ENCLOSURE; return; } // if we don't explicitly handle the element, make sure we don't wind up erroneously // storing a newline or other bogus data into one of our existing elements currentstate = 0; } public void endElement(String namespaceURI, String localName, String qName) throws SAXException { depth--; if (localName.equals(ITEM_ELEMENT)) { // add our item to the list! feed.addItem(item); return; } } public void characters(char ch[], int start, int length) { String theString = new String(ch,start,length); Log.i("RSSReader","_______characters[" + theString + "] - currentstate: " + currentstate); if (theString.equals(LESS_THAN_SIGN) || theString.equals(GREATER_THAN_SIGN) || (theString.trim().length() == 0)) { return; } switch (currentstate) { case RSS_TITLE: item.setTitle(theString); break; case RSS_LINK: item.setLink(theString); break; case RSS_DESCRIPTION: item.setDescription(theString); break; case RSS_CATEGORY: item.setCategory(theString); break; case RSS_PUBDATE: item.setPubDate(theString); break; case RSS_ENCLOSURE: Pattern p = Pattern.compile(IMAGE_SOURCE_PATTERN); Matcher m = p.matcher(theString); if (m.find()) { try { Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(m.group(1)).getContent()); item.setImage(bitmap); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } break; default: return; } } </code></pre> <p>Thanks a lot,</p> <p>Gratzi</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload