parse

added
1.0

ns
clojure.xml

type
function

(parse s) (parse s startparse)

Parses and loads the source s, which can be a File, InputStream or
String naming a URI. Returns a tree of the xml/element struct-map,
which has the keys :tag, :attrs, and :content. and accessor fns tag,
attrs, and content. Other parsers can be supplied by passing
startparse, a fn taking a source and a ContentHandler and returning
a parser

                (require '[clojure.xml :as xml]
         '[clojure.zip :as zip])

;;convenience function, first seen at nakkaya.com later in clj.zip src
(defn zip-str [s]
  (zip/xml-zip 
      (xml/parse (java.io.ByteArrayInputStream. (.getBytes s)))))

;;parse from xml-strings to internal xml representation
user=> (zip-str "<a href='nakkaya.com'/>")
[{:tag :a, :attrs {:href "nakkaya.com"}, :content nil} nil]

;;root can be rendered with xml/emit-element
user=> (xml/emit-element (zip/root [{:tag :a, :attrs {:href "nakkaya.com"}, :content nil} nil]))
<a href='nakkaya.com'/>

;;printed (to assure it's not lazy and for performance), can be caught to string variable with with-out-str