2011-01-12 4 views
0

Dans mon application, j'ai une classe qui étend la classe org.xml.sax.helpers.DefaultHandler. Son but est de lire les flux rss. Mon problème est que lorsque j'atteins la balise "enclosure", la méthode des caractères n'est pas appelée, donc je ne peux pas extraire l'URL de l'image. Est-ce que quelqu'un sait pourquoi cela se passe-t-il?méthode de caractères non appelée pour la balise de boîtier

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 = "<"; 
private final String GREATER_THAN_SIGN = ">"; 
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("====>> 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; 
    } 
} 

Merci beaucoup,

Gratzi

+0

Vous devez publier le code que vous utilisez pour analyser; il n'y a pas de raison spécifique à l'étiquette d'enceinte qui l'empêcherait d'être appelée. – kcoppock

+0

J'ai ajouté le code – Gratzi

Répondre

0

utilisant l'approche this m'a aidé à résoudre le problème.

Questions connexes