2011-10-31 76 views
2

我創建了一個包含詳細視圖的XML解析器。我正在閱讀包含機箱標籤的RSS源。我試圖從機箱標籤中獲取視頻文件的網址。Android SAX XML解析器訪問外殼標記URL屬性

這是機箱標籤的樣子:

<enclosure url="http://video.calvaryccm.com/main/podcastmp4/MB66-03.mp4" type="video/mp4"/>

每次我試圖找回解析器返回null。我該怎麼辦?

這是一個顯示來自這似乎是導致問題的解析結果的一部分:

// TESTING HERE! 
String enclosure = b.getString("enclosure"); 
// What is getting displayed 
theTitle = b.getString("title").trim(); 
theDate = newDateStr; 
theStory = b.getString("description") + "\n\nView in full website:\n" + b.getString("link")+ "\n\nDownload teaching:\n" + enclosure; 

這是RSSHandler類:

public class RSSHandler extends DefaultHandler 
{ 

RSSFeed _feed; 
RSSItem _item; 
String _lastElementName = ""; 
boolean bFoundChannel = false; 
final int RSS_TITLE = 1; 
final int RSS_LINK = 2; 
final int RSS_DESCRIPTION = 3; 
final int RSS_ENCLOSURE = 4; 
final int RSS_PUBDATE = 5; 

int depth = 0; 
int currentstate = 0; 
/* 
* Constructor 
*/ 
RSSHandler() 
{ 
} 

/* 
* getFeed - this returns the feed when all of the parsing is complete 
*/ 
RSSFeed getFeed() 
{ 
    return _feed; 
} 


public void startDocument() throws SAXException 
{ 
    // Initialize RSSFeed object - this will hold 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 
{ 
    depth++; 
    if (localName.equals("channel")) 
    { 
     currentstate = 0; 
     return; 
    } 
    if (localName.equals("image")) 
    { 
     // Record feed data - Temporarily stored it in the item 
     _feed.setTitle(_item.getTitle()); 
     _feed.setPubDate(_item.getPubDate()); 
    } 
    if (localName.equals("item")) 
    { 
     // create a new item 
     _item = new RSSItem(); 
     return; 
    } 
    if (localName.equals("title")) 
    { 
     currentstate = RSS_TITLE; 
     return; 
    } 
    if (localName.equals("description")) 
    { 
     currentstate = RSS_DESCRIPTION; 
     return; 
    } 
    if (localName.equals("link")) 
    { 
     currentstate = RSS_LINK; 
     return; 
    } 
    if (localName.equals("enclosure")) 
    { 
     currentstate = RSS_ENCLOSURE; 
     return; 
    } 
    if (localName.equals("pubDate")) 
    { 
     currentstate = RSS_PUBDATE; 
     return; 
    } 
    // If I don't explicitly handle the element and to make sure I don't wind up erroneously 
    // storing a newline or other fake 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")) 
    { 
     // Add the 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 + "]"); 

    switch (currentstate) 
    { 
     case RSS_TITLE: 
      _item.setTitle(theString); 
      currentstate = 0; 
      break; 
     case RSS_LINK: 
      _item.setLink(theString); 
      currentstate = 0; 
      break; 
     case RSS_DESCRIPTION: 
      _item.setDescription(theString); 
      currentstate = 0; 
      break; 
     case RSS_ENCLOSURE: 
      _item.setEnclosure(theString); 
      currentstate = 0; 
      break; 
     case RSS_PUBDATE: 
      _item.setPubDate(theString); 
      currentstate = 0; 
      break; 
     default: 
      return; 
    } 

} 
} 

這是RSSFeed類:

public class RSSFeed 
{ 
private String _title = null; 
private String _pubdate = null; 
private String _enclosure = null; 
private int _itemcount = 0; 
private List<RSSItem> _itemlist; 


RSSFeed() 
{ 
    _itemlist = new Vector<RSSItem>(0); 
} 
int addItem(RSSItem item) 
{ 
    _itemlist.add(item); 
    _itemcount++; 
    return _itemcount; 
} 
RSSItem getItem(int location) 
{ 
    return _itemlist.get(location); 
} 
List<RSSItem> getAllItems() 
{ 
    return _itemlist; 
} 
int getItemCount() 
{ 
    return _itemcount; 
} 
void setTitle(String title) 
{ 
    _title = title; 
} 
void setPubDate(String pubdate) 
{ 
    _pubdate = pubdate; 
} 
void setEnclosure(String enclosure) 
{ 
    _enclosure = enclosure; 
} 
String getTitle() 
{ 
    return _title; 
} 
String getPubDate() 
{ 
    return _pubdate; 
} 
String getEnclosure() 
{ 
    return _enclosure; 
} 
} 

這是RSSItem類:

public class RSSItem 
{ 
    private String _title = null; 
    private String _description = null; 
    private String _link = null; 
    private String _enclosure = null; 
    private String _pubdate = null; 


RSSItem() 
{ 
} 
void setTitle(String title) 
{ 
    _title = title; 
} 
void setDescription(String description) 
{ 
    _description = description; 
} 
void setLink(String link) 
{ 
    _link = link; 
} 
void setEnclosure(String enclosure) 
{ 
    _enclosure = enclosure; 
} 
void setPubDate(String pubdate) 
{ 
    _pubdate = pubdate; 
} 
String getTitle() 
{ 
    return _title; 
} 
String getDescription() 
{ 
    return _description; 
} 
String getLink() 
{ 
    return _link; 
} 
String getEnclosure() 
{ 
    return _enclosure; 
} 
String getPubDate() 
{ 
    return _pubdate; 
} 
public String toString() 
{ 
    // limit how much text we display 
    if (_title.length() > 42) 
    { 
     return _title.substring(0, 42) + "..."; 
    } 
    return _title; 
} 
} 
+0

你的「b」是什麼類型的對象?你的對象都沒有定義'getString(String)'方法。 – laz

+0

b是我用來將數據從一個類傳輸到另一個類的包的名稱像這樣:Bundle b = startingIntent.getBundleExtra(「android.intent.extra.INTENT」); –

回答

2

url標記是一個屬性,所以要訪問它,您可以在localName爲「enclosure」時在startElement中調用attributes.getValue("url");