2011-12-06 75 views
0

我試圖解析這個XML提要Android的XML解析空指針異常

http://www.billboard.com/rss/charts/hot-100

但每當我試着去閱讀說明標籤,它拋出NullPointerException。 Theres標籤在文檔的開始是這個標籤導致問題?

我能夠成功拉出所有其他字段。

這裏是我的代碼:

Element element = result.getDocumentElement(); 
NodeList list = element.getElementsByTagName("item"); 
try{   
    if(list != null && list.getLength()>0){ 
    for(int i=0;i<list.getLength();i++){ 
     Element entry = (Element)list.item(i); 
     Element title = (Element)entry.getElementsByTagName("title").item(0);  
     String strTitle = title.getFirstChild().getNodeValue(); 
     Log.i("Zealdeveloper", "Title"); 
     Element link = (Element)entry.getElementsByTagName("link").item(0); 
     String strLink = link.getFirstChild().getNodeValue(); 
     Log.i("Zealdeveloper", "Link"); 
     Element pubdate = (Element)entry.getElementsByTagName("pubDate").item(0);    
     String strPubdate = pubdate.getFirstChild().getNodeValue(); 
     Log.i("Zealdeveloper", "pubdate"); 
     Element description = (Element)entry.getElementsByTagName("description").item(0);    
     String strDescription = description.getFirstChild().getNodeValue(); 
     Log.i("Zealdeveloper", "Description"); } } 

例外,我越來越:

12-06 13:44:09.202: I/Zealdeveloper(824): Title 
12-06 13:44:09.202: I/Zealdeveloper(824): Link 
12-06 13:44:09.202: I/Zealdeveloper(824): pubdate 
12-06 13:44:09.202: W/System.err(824): java.lang.NullPointerException 
12-06 13:44:09.202: W/System.err(824): at com.zealdeveloper.musicfreak.MusicFreakActivity$ParseXmlFeeds.onPostExecute(MusicFreakActivity.java:152) 
12-06 13:44:09.212: W/System.err(824): at com.zealdeveloper.musicfreak.MusicFreakActivity$ParseXmlFeeds.onPostExecute(MusicFreakActivity.java:1) 
12-06 13:44:09.212: W/System.err(824): at android.os.AsyncTask.finish(AsyncTask.java:417) 
12-06 13:44:09.212: W/System.err(824): at android.os.AsyncTask.access$300(AsyncTask.java:127) 
12-06 13:44:09.212: W/System.err(824): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 
12-06 13:44:09.212: W/System.err(824): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-06 13:44:09.212: W/System.err(824): at android.os.Looper.loop(Looper.java:123) 
12-06 13:44:09.212: W/System.err(824): at android.app.ActivityThread.main(ActivityThread.java:4627) 
12-06 13:44:09.212: W/System.err(824): at java.lang.reflect.Method.invokeNative(Native Method) 
12-06 13:44:09.212: W/System.err(824): at java.lang.reflect.Method.invoke(Method.java:521) 
12-06 13:44:09.212: W/System.err(824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
12-06 13:44:09.222: W/System.err(824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
12-06 13:44:09.222: W/System.err(824): at dalvik.system.NativeStart.main(Native Method) 

謝謝!

+1

我沒有回答這個問題。但根據我的經驗,我經常遇到DOM解析器中的空指針異常。然後我轉移到SAX Parser,對我來說工作非常順利。 –

+0

thnx爲提示夥伴,如果你有任何好的來源或教程添加到列表視圖使用saxparser那麼它將是非常有益的.. – ZealDeveloper

+1

你可以試試這個http://iamvijayakumar.blogspot.com/2011/03/android -webservice-with.html –

回答

1

您可以使用RSS FEED Library下載RSS FEED JAR文件的位置---->http://code.google.com/p/superpodder/source/browse/trunk/lib/rsslib4j-0.2.jar?r=3

這樣你就可以分析數據

TextView textView = (TextView) findViewById(R.id.textview); 

      String feedUrl = "http://www.billboard.com/rss/charts/hot-100"; // RSS means Real Simply Syndication 
      URL url = new URL(feedUrl); 

      RSSHandler rsshandler = new RSSHandler(); 

      RSSParser.parseXmlFile(url, rsshandler, false); 

      RSSChannel rssChannel = rsshandler.getRSSChannel(); 

      String data = rssChannel.getAboutAttribute(); 
      textView.append("\n\n"); 
      textView.append("Attributes: "+data+"\n"); 

      data = rssChannel.getCopyright(); 
      textView.append("Copyright: "+data+"\n"); 

      data = rssChannel.getDescription(); 
      textView.append("Description: "+data+"\n"); 

      data = rssChannel.getDocs(); 
      textView.append("Docs: "+data+"\n"); 

      data = rssChannel.getGenerator(); 
      textView.append("Generator: "+data+"\n"); 

      data = rssChannel.getLanguage(); 
      textView.append("Language: "+data+"\n"); 

      data = rssChannel.getLastBuildDate(); 
      textView.append("LastBuildDate: "+data+"\n"); 

      data = rssChannel.getLink(); 
      textView.append("Link: "+data+"\n"); 

      data = rssChannel.getPubDate(); 
      textView.append("PubDate: "+data+"\n"); 

      data = rssChannel.getTitle(); 
      textView.append("Title: "+data+"\n"); 

      data = rssChannel.getTTL(); 
      textView.append("TTL: "+data+"\n"); 

      data = rssChannel.getWebMaster(); 
      textView.append("WebMaster: "+data+"\n"); 


      RSSImage rssImage = rssChannel.getRSSImage(); 
      data = rssImage.getTitle(); 
      textView.append("ImageTitle: "+data+"\n"); 

      data = rssImage.getWidth(); 
      textView.append("ImageWidth: "+data+"\n"); 

      data = rssImage.getHeight(); 
      textView.append("ImageHeight: "+data+"\n"); 

      data = rssImage.getLink(); 
      textView.append("ImageLink: "+data+"\n"); 

      data = rssImage.getUrl(); 
      textView.append("ImageUrl: "+data+"\n"); 

      textView.append("\n"); 
      LinkedList linkedList = rssChannel.getItems(); 
      int size = linkedList.size(); 

      textView.append("Total Items: "+size+"\n"); 

注:這僅是一個RSSFeed的url請勿使用此庫解析任何其他url

+0

嗨thanx的回覆, 我試着做新的項目和使用這個jar文件..但似乎沒有發生?我已經複製了你的spinet並刪除了不需要的標籤。文本視圖似乎是完好無損的變化我也給了互聯網許可。我錯過了什麼? – ZealDeveloper

+0

URL url = new URL(「http://www.billboard.com/rss/charts/hot-100」); RSSHandler rsshandler = new RSSHandler(); RSSParser.parseXmlFile(url,rsshandler,true); RSSChannel rssChannel = rsshandler.getRSSChannel(); String data = rssChannel.getAboutAttribute(); data = rssChannel.getDescription(); – ZealDeveloper

+0

對不起,我在粘貼之前將其更改爲true,它是錯誤的,沒有以前沒有工作過......我直接在邊URL中使用字符串值,並且URL也正常工作。 M沒有得到任何輸出,我已經把它印在日誌上了.. – ZealDeveloper

0

在您的XML文件中是否有像「&」或「 - 」的任何字符? 我覺得解析器無法解析這些! 只是試圖改變它,如果你有一個