(xml-parse)

Started by tyler, June 25, 2008, 06:54:19 PM

Previous topic - Next topic

tyler

<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.

Jeff

#1
http://www.artfulcode.net/articles/working-xml-newlisp/">http://www.artfulcode.net/articles/working-xml-newlisp/
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

cormullion

#2
Hi Tyler - Jeff's article is cool! If you want a quick solution now:


(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...?

Jeff

#3
They are not.  They need to be <foo> for empty tags.
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

tyler

#4
It's the XML returned by Twitter when making requests through their API. I've changed nothing; that is a cut 'n paste.

cormullion

#5
The forum has messed up the XML formatting! A quick google confirms that these are closed by matching tags, even when empty.