2013-10-06 60 views
0

如何從以下XML-Tag提取url = ""零件。我在Android中使用Jsoup如何在jsoup中提取屬性

<enclosure type="image/jpg" url="EXTRACT THIS" length="123456" /> 

見我的代碼:

Document doc = db.parse(new InputSource(url.openStream())); 
      doc.getDocumentElement().normalize(); 

      // Get all <item> tags. 
      NodeList nl = doc.getElementsByTagName("item"); 
      int length = nl.getLength(); 


      for (int i = 0; i < length; i++) { 
       Node currentNode = nl.item(i); 
       RSSItem _item = new RSSItem(); 

       NodeList nchild = currentNode.getChildNodes(); 
       int clength = nchild.getLength(); 

       // Get the required elements from each Item 
       for (int j = 0; j < clength; j = j + 1) { 

        Node thisNode = nchild.item(j); 
        String theString = null; 
        String nodeName = thisNode.getNodeName(); 

        //NodeList test = nchild.item(j).getChildNodes(); 

        if(j<=3){ 
         theString = nchild.item(j).getFirstChild().getNodeValue(); 
        } 
        if("enclosure".equals(nodeName)){ 
         //HAVE TO GET URL HERE from ATTRIBUTE: 
        } 

這是XML:

view-source:http://www.skysports.com/rss/0,20514,11661,00.xml 
+0

我已經看了看這個鏈接。它沒有幫助。我是Android新手 –

回答

1
Document doc = Jsoup.connect(link).get(); 
     Elements elements= doc 
       .getElementsByTag("enclosure"); 
    for(int i=0;i<elements.getsize();i++){ 
     String url=elements.get(i).attr("href");} 

您可以通過標籤名稱搜索,它會得到有標籤您輸入元素.. 我使用get(0)來表示它可能是您的鏈接的第一個元素..或..可能是它唯一的元素..使用索引如您所見它以鏈接的順序.. ATTR:要獲得的屬性在元 它`返回字符串..

祝你好運:)

+0

請參閱我的編輯。 –

+0

我看到你的鏈接..你可以做的循環.get(index) –

+0

看到我的編輯.... –