(xml-parse)

Q&A's, tips, howto's
Locked
tyler
Posts: 4
Joined: Mon Apr 23, 2007 3:55 pm

(xml-parse)

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

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

Jeff
=====
Old programmers don't die. They just parse on...

Artful code

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

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

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

They are not. They need to be <foo> for empty tags.
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

tyler
Posts: 4
Joined: Mon Apr 23, 2007 3:55 pm

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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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

Locked