twitter APIのXMLからデータを抜き出す時に陥った問題と解決。
twitter APIのXMLが以下みたいな感じになってるんだけど、
<entry> <id>tag:search.twitter.com,2005:2455477581</id> <published>2009-07-03T15:47:12Z</published> <link type="text/html" rel="alternate" href="http://twitter.com/se1ken/statuses/2455477581"/> <title>検索ちゃんってもう4年も続いてるのか</title> <content type="html">検索ちゃんってもう4年も続いてるのか</content> <updated>2009-07-03T15:47:12Z</updated> <link type="image/png" rel="image" href="http://s3.amazonaws.com/twitter_production/profile_images/290667767/tanabata_normal.gif"/> <twitter:source><a href="http://twitter.com/">web</a></twitter:source> <twitter:lang>ja</twitter:lang> <author> <name>se1ken (せいけん)</name> <uri>http://twitter.com/se1ken</uri> </author> </entry>
投稿者の名前とかは、
entry->author->name
とかで簡単に抜き出せたんだけど、同じタグが二つあった場合の抜き出し方でつまづいた。
たとえば、上の場合だったら、
<link type="text/html" rel="alternate" href="http://twitter.com/se1ken/statuses/2455477581"/> <link type="image/png" rel="image" href="http://s3.amazonaws.com/twitter_production/profile_images/290667767/tanabata_normal.gif"/>
このようにlinkタグが二つある。で、そのlinkタグのアイコンのURLが埋まってる部分を取り出したかったんだけど、普通に、
entry->link->attributes()*1
ってやると、上の方linkタグの属性値が抜き出されちゃう。
で、調べた結果、タグが複数ある場合は配列みたいになっているらしい。なので、
entry->link[1]->attributes()
このようにやると、二つ目のlinkの属性値が抜き出せるみたい。
*1:attributes関数は属性値が抜き出せるらしい。