Page 1 of 1

(xml-parse)

Posted: Thu Jun 26, 2008 1:54 am
by tyler

Code: Select all

<users>
 <user>
  <id>20</id>
  <name>Evan Williams</name>
  <screen_name>ev</screen_name>
  <location>San Francisco, CA, US</location>
  <description>Co-founder and CPO of Twitter</description>
  <profile_image_url>
   http://s3.amazonaws.com/twitter_production/profile_images/14019652/ev-sky_normal.jpg
  </profile_image_url>
  <url>http://evhead.com</url>
  <protected>false</protected>
  <followers_count>14430</followers_count>
  <status>
   <created_at>Wed Jun 25 20:49:44 +0000 2008</created_at>
   <id>843603299</id>
   <text>Drinking an Odwalla "Mental Energy Juice Drink." So, um, watch out.</text>
   <source><a>txt</a></source>
   <truncated>false</truncated>
   <in_reply_to_status_id>
   <in_reply_to_user_id>
   <favorited>false</favorited>
  </status>
 </user>
</users>
Using (xml-parse) on the above XML, what would be the best way to extract a single TEXT from the ELEMENT "screen_name"? Suppose that a list of users was used in place of the above single user; how would you guys go about populating another list with only the screen names?

Thanks for the help.

Posted: Thu Jun 26, 2008 11:24 am
by Jeff

Posted: Thu Jun 26, 2008 5:06 pm
by cormullion
Hi Tyler - Jeff's article is cool! If you want a quick solution now:

Code: Select all

(xml-type-tags nil nil nil nil)
(set 'xml (xml-parse (read-file {/Users/me/Desktop/test.xml}) 15))
(map (fn (screen-name-ref) (last (xml  (chop screen-name-ref)))) (ref-all (xml 'screen_name)))
There's a much more basic introduction to XML than Jeff's article in my Introduction to newLISP... (Which needs updating, unfortunately :)

By the way, the XML that you posted doesn't work with this code. I wonder whether the 'in-reply-to' elements are correct...?

Posted: Thu Jun 26, 2008 5:17 pm
by Jeff
They are not. They need to be <foo> for empty tags.

Posted: Thu Jun 26, 2008 5:30 pm
by tyler
It's the XML returned by Twitter when making requests through their API. I've changed nothing; that is a cut 'n paste.

Posted: Thu Jun 26, 2008 6:00 pm
by cormullion
The forum has messed up the XML formatting! A quick google confirms that these are closed by matching tags, even when empty.