1.3.0 permalink Arrow_down_16x16

parse

clojure.xml

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

1 Example top

  • (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
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/xml.clj:78 top

(defn parse
  "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"
  {:added "1.0"}
  ([s] (parse s startparse-sax))
  ([s startparse]
    (binding [*stack* nil
              *current* (struct element)
              *state* :between
              *sb* nil]
      (startparse s content-handler)
      ((:content *current*) 0))))
Vars in clojure.xml/parse:
Used in 0 other vars

Comments top

No comments for parse. Log in to add a comment.