Page 1 of 1

Converting an SXML file to a list

Posted: Tue Jul 11, 2006 8:57 am
by cormullion
Say I have an SXML file:

Code: Select all

((rss 
		 (@ 
			(version "0.92")) 
		 (channel 
			(docs "http://backend.userland.com/rss092")    
			(title "newLISP Fan Club")    
			(link "http://www.alh.net/newlisp/phpbb/")    
			(description "Friends and Fans of newLISP ")    
			(managingEditor "dooright101@yahoo.com")    
			(webMaster "dooright101@yahoo.com")    
			(lastBuildDate "Mon, 10 Jul 2006 22:40:58 GMT")    
			(item 
			...
How can I make this into a list?


(I think this might be an easy question but when you don't know the answer, every question seems hard... :-)

Posted: Tue Jul 11, 2006 9:09 am
by m i c h a e l
cormullion wrote:How can I make this into a list?

Code: Select all

(list (rss 
       (@ 
         (version "0.92")) 
       (channel 
         (docs "http://backend.userland.com/rss092")    
         ...
Sorry, bad joke ;-)

m i c h a e l

Posted: Tue Jul 11, 2006 9:11 am
by cormullion
Well, funnily enough I did try that (using read-file)...:-) I got a list containing one very large string...

Posted: Tue Jul 11, 2006 1:04 pm
by Lutz
It is already a list. I assume you want to find certain things in it?

I have assigned the list to a variable called RSS to handle it better:

Code: Select all

(set 'RSS '((rss
       (@
         (version "0.92"))
       (channel
         (docs "http://backend.userland.com/rss092")
         (title "newLISP Fan Club")
         (link "http://www.alh.net/newlisp/phpbb/")
         (description "Friends and Fans of newLISP ")
         (managingEditor "dooright101@yahoo.com")
         (webMaster "dooright101@yahoo.com")
         (lastBuildDate "Mon, 10 Jul 2006 22:40:58 GMT")   ))))

;use ref to find the indices of something:

> (ref 'channel RSS)
(0 2 0)
> 

; try it

> (RSS 0 2 0)
channel
> 

; strip of the last index number to extract the channel members

> (RSS 0 2)
(channel (docs "http://backend.userland.com/rss092") (title "newLISP Fan Club") 
 (link "http://www.alh.net/newlisp/phpbb/") 
 (description "Friends and Fans of newLISP ") 
 (managingEditor "dooright101@yahoo.com") 
 (webMaster "dooright101@yahoo.com") 
 (lastBuildDate "Mon, 10 Jul 2006 22:40:58 GMT"))

; taking the rest of it gives you just the channel members

> (set 'CHN (1 (RSS 0 2)))
((docs "http://backend.userland.com/rss092") (title "newLISP Fan Club") 
 (link "http://www.alh.net/newlisp/phpbb/") 
 (description "Friends and Fans of newLISP ") 
 (managingEditor "dooright101@yahoo.com") 
 (webMaster "dooright101@yahoo.com") 
 (lastBuildDate "Mon, 10 Jul 2006 22:40:58 GMT"))
> 
; on this you could use assoc to find something:

> (assoc 'link CHN)
(link "http://www.alh.net/newlisp/phpbb/")

> (assoc 'webMaster CHN)
(webMaster "dooright101@yahoo.com")
> 
Lutz

Posted: Tue Jul 11, 2006 1:08 pm
by cormullion
Hi Lutz! The thing I can't do is this:

Code: Select all

(set 'RSS '((rss 
since the ((rss ... stuff is in a file. For now I'm doing what you're doing, and copying the SXML into a (set ... expression. But I couldn't work out how to get a file containing this into a symbol...

Posted: Tue Jul 11, 2006 1:14 pm
by Lutz
lets assume this in your file:

Code: Select all

;; file rss-info
((rss (@ (version "0.92")) (channel (docs "http://backend.userland.com/rss092") 
   (title "newLISP Fan Club") 
   (link "http://www.alh.net/newlisp/phpbb/") 
   (description "Friends and Fans of newLISP ") 
   (managingEditor "dooright101@yahoo.com") 
   (webMaster "dooright101@yahoo.com") 
   (lastBuildDate "Mon, 10 Jul 2006 22:40:58 GMT"))))
;; eof
Do the following to get it all in to the variable RSS:

Code: Select all

> (set 'RSS (eval-string (append "'" (read-file "rss-info"))))
((rss (@ (version "0.92")) (channel (docs "http://backend.userland.com/rss092") 
   (title "newLISP Fan Club") 
   (link "http://www.alh.net/newlisp/phpbb/") 
   (description "Friends and Fans of newLISP ") 
   (managingEditor "dooright101@yahoo.com") 
   (webMaster "dooright101@yahoo.com") 
   (lastBuildDate "Mon, 10 Jul 2006 22:40:58 GMT"))))
Lutz

Posted: Tue Jul 11, 2006 1:37 pm
by cormullion
Lutz wrote:

Code: Select all

> (set 'RSS (eval-string (append "'" (read-file "rss-info"))))
That's the one! I feel better now, because I never tried that ... :-)

This works for the short example file that you gave. I am trying to make it work with a longer SXML file now - assuming there's no size limit for eval-string, it might just be some formatting that's gone adrift...

Anyway, thanks, Lutz!