Converting an SXML file to a list

Started by cormullion, July 11, 2006, 01:57:54 AM

Previous topic - Next topic

cormullion

Say I have an SXML file:


((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... :-)

m i c h a e l

#1
Quote from: "cormullion"How can I make this into a list?



(list (rss
       (@
         (version "0.92"))
       (channel
         (docs "http://backend.userland.com/rss092")    
         ...


Sorry, bad joke ;-)



m i c h a e l

cormullion

#2
Well, funnily enough I did try that (using read-file)...:-)  I got a list containing one very large string...

Lutz

#3
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:

(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

cormullion

#4
Hi Lutz! The thing I can't do is this:


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

Lutz

#5
lets assume this in your file:


;; 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:



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

cormullion

#6
Quote from: "Lutz"
> (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!