2014-12-30 73 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