2012-12-27 144 views
1

所以,我寫了一些小的東西來解析Youtube用戶的feed。Android解析XML以獲取節點值

問題是,當我打電話給getNodeValue()時返回null。爲什麼是這樣?任何想法或替代方式接近表示讚賞,謝謝。

這裏是如何我目前正在做的事情的一個片段:

try { 

    Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("https://gdata.youtube.com/feeds/api/users/estudiosabiertostv/uploads"); 

    NodeList entries = d.getElementsByTagName("entry"); 

    for(int i = 0; i < entries.getLength(); i++) { 

     NodeList children = entries.item(i).getChildNodes(); 
     YoutubeVideo video = new YoutubeVideo(); 

     for(int j = 0; j < children.getLength(); j++) { 

      Node child = children.item(j); 

      if(child.getNodeName().equals("title")) { 

       video.title = child.getNodeValue(); 
       break; 
      } 
     } 
     videos.add(video); 
    } 
} 
catch (Exception e) { 

    e.printStackTrace(); 
} 

這裏就是我從獲得XML的網址:

https://gdata.youtube.com/feeds/api/users/estudiosabiertostv/uploads 

下面是一個條目的片段,我試圖獲得標題

<entry> 
    <id>http://gdata.youtube.com/feeds/api/videos/oDbOmTY3gDw</id> 
    <published>2012-12-17T08:14:12.000Z</published> 
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video"/> 
    <title type="text">SEAS - Grupo San Valero - Crea tu propia Navidad (2012)</title> 

    <content type="text">Felicitación de Navidad de SEAS, Estudios Abiertos del Grupo San Valero</content> 
    <link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=oDbOmTY3gDw&feature=youtube_gdata"/> 
    <author> 
     <name>estudiosabiertostv</name> 
     <uri>http://gdata.youtube.com/feeds/api/users/estudiosabiertostv</uri> 
    </author> 
</entry> 
+3

關閉我的頭頂,標題節點應該反過來有一個孩子 - 一個文本節點,而一個應該包含實際的文本。 –

+0

@ full.stack.ex這是你的好東西。創建一個答案,我會接受。 – conor

回答