newLISP Fan Club

Forum => newLISP in the real world => Topic started by: joejoe on September 15, 2009, 04:41:01 PM

Title: parsing an xml to separate local files
Post by: joejoe on September 15, 2009, 04:41:01 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.php?id=introduction:introduction_part_2#working_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.


#!/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)
Title:
Post by: cormullion on September 16, 2009, 08:49:58 AM
(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:


(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 "nn")))
 
Title:
Post by: joejoe on September 16, 2009, 11:48:49 AM
cormullion! thanks for this help!!


Quote from: "cormullion"(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.


 
Quote from: "cormullion"


...



This is one way to do it:


(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 "nn")))
 


Thank you again, cormullion. It makes sense what you are doing in this code.
Title:
Post by: cormullion on September 16, 2009, 11:50:22 AM
Cool. It could do with some error handling - but that always clutters up the code so much... :)