xml stuff

Started by tom, January 31, 2005, 06:08:31 PM

Previous topic - Next topic

tom

hey guys,



can someone give me a brief tutorial on how to output xml from newlisp?  I'm pretty sure I can get sxml out of xml, but how do I go the other direction?  I want to write xml, only I want to write it in newlisp.



Thanks!

Lutz

#1
This is a 'typical' LISP problem:

;; translate s-expr to XML
;;
(define (expr2xml expr level)
  (cond
    ((empty? expr) "")
    ((list? (first expr))
        (expr2xml (first expr) (+ level 1))
        (dolist (s (rest expr)) (expr2xml (list s) (+ level 1))))
    ((symbol? (first expr))
        (print (dup " " level))
        (println "<" (name (first expr)) ">")
        (dolist (s (rest expr)) (expr2xml (list s) (+ level 1)))
        (print (dup " " level))
        (println "</" (name (first expr)) ">"))
    ( true
        (print (dup " " level))
        (println (first expr)) ; unquoted
        ;; (println {"} (first expr) {"}) ; quoted
        (dolist (s (rest expr)) (expr2xml (list s) (+ level 1))))
     ))

;; a lisp expression for a person

(set 'expr
    '(person
         (name "John Doe")
         (address
             (street "Main Street")
             (city "Anytown"))))

;; translate to XML

(expr2xml expr 0)   =>

<person>
    <name>
      John Doe
    </name>
    <address>
        <street>
          Main Street
        </street>
        <city>
          Anytown
        </city>
    </address>
</person>



Lutz



ps: see and improved version at http://www.newlisp.org/index.cgi?page=Tips_and_Tricks">http://www.newlisp.org/index.cgi?page=Tips_and_Tricks