Q&A's, tips, howto's
joejoe
Posts: 173 Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA
Post
by joejoe » Tue Sep 15, 2009 11:41 pm
Hi - I am trying to put together a few bits of code from cormullion's superb introduction here:
http://www.newlisp.org/dokuwiki/doku.ph ... g_with_xml
but am ending up with:
ERR: value expected : (0 2 9 0)
The two bits Im trying to merge are commented in the code below, along with my attempt to take the titles from the xml file and make them into local file names. From there I want to put the links and description into each respective file.
Would anyone be able to say which part of the manual I need to go back and study? I cant seem to get inside of the dolist to insert a save command for each title as it is recursed through. Thanks for any sort of tip.
Code: Select all
#!/usr/bin/newlisp
(set 'xml (get-url "http://shop.ebay.com/i.html?_nkw=lisp&_rss=1"))
(xml-type-tags nil nil nil nil)
(set 'sxml (xml-parse xml 15))
;(println (lookup 'title (sxml (chop (ref 'item sxml)))))
;(save (lookup 'title (sxml (chop (ref 'item sxml)))))
;(dolist (el (ref-all 'title sxml))
; (println (rest (sxml (chop el)))))
(dolist (el (println (lookup 'title (sxml (chop (ref-all 'item sxml))))))
(save (lookup 'title (sxml (chop (ref-all 'item sxml))))))
(exit)
cormullion
Posts: 2038 Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:
Post
by cormullion » Wed Sep 16, 2009 3:49 pm
Code: Select all
(dolist (el (println (lookup 'title (sxml
You've got a dolist but aren't you're supplying the result of a println function rather than a list?
I'll have a closer look later on today when I get time...
...
This is one way to do it:
Code: Select all
(xml-type-tags nil nil nil nil)
(set 'sxml (xml-parse xml 15))
; get references to items
(set 'item-refs (ref-all 'item sxml))
; for each item
(dolist (ir item-refs)
(set 'title (lookup 'title (sxml (chop ir))))
(set 'description (lookup 'description (sxml (chop ir))))
(set 'file-name (lower-case (replace "[^A-Za-z]" title "" 0)))
(change-dir "/Users/me")
(write-file file-name (string title "\n" description "\n\n")))
joejoe
Posts: 173 Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA
Post
by joejoe » Wed Sep 16, 2009 6:48 pm
cormullion! thanks for this help!!
cormullion wrote: Code: Select all
(dolist (el (println (lookup 'title (sxml
You've got a dolist but aren't you're supplying the result of a println function rather than a list?
Yes - I thought that was how I was going to get the filename to the save, but what you've suggested is beautiful. I am studying and playing with it now.
cormullion wrote:
...
This is one way to do it:
Code: Select all
(xml-type-tags nil nil nil nil)
(set 'sxml (xml-parse xml 15))
; get references to items
(set 'item-refs (ref-all 'item sxml))
; for each item
(dolist (ir item-refs)
(set 'title (lookup 'title (sxml (chop ir))))
(set 'description (lookup 'description (sxml (chop ir))))
(set 'file-name (lower-case (replace "[^A-Za-z]" title "" 0)))
(change-dir "/Users/me")
(write-file file-name (string title "\n" description "\n\n")))
Thank you again, cormullion. It makes sense what you are doing in this code.
cormullion
Posts: 2038 Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:
Post
by cormullion » Wed Sep 16, 2009 6:50 pm
Cool. It could do with some error handling - but that always clutters up the code so much... :)