2014-12-30 66 views
1

我試圖讓這個XML「鏈接」標籤元素的文本:http://www.istana.gov.sg/latestupdate/rss.xmlJsoup。選擇返回空值,但元素也包含文本

我已經編碼得到的第一篇文章。

 URL = getResources().getString(R.string.istana_home_page_rss_xml); 
     // URL = "http://www.istana.gov.sg/latestupdate/rss.xml"; 

     try { 
      doc = Jsoup.connect(URL).ignoreContentType(true).get(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // retrieve the link of the article 
     links = doc.select("link"); 

     // retrieve the publish date of the article 
     dates = doc.select("pubDate"); 

     //retrieve the title of the article 
     titles = doc.select("title"); 

     String[] article1 = new String[3]; 
     article1[0] = links.get(1).text(); 
     article1[1] = titles.get(1).text(); 
     article1[2] = dates.get(0).text(); 

該文章很好地發表,但鏈接返回「」值(整個鏈接元素返回「」值)。標題和日期沒有任何問題。鏈接標籤包含一個URL文本。任何人都知道爲什麼,它返回「」值?

回答

3

它看起來像默認的HTML分析器無法識別<link>作爲有效標籤,並自動關閉它<link />這意味着這個標籤的內容是空的。

爲了解決這個問題,而不是HTML解析器,你可以使用不很在乎標籤名稱的XML解析器。

doc = Jsoup.connect(URL) 
     .ignoreContentType(true) 
     .parser(Parser.xmlParser()) // <-- add this 
     .get(); 
+0

工作就像一個魅力。謝謝你這麼多Pshemo! – burper333