Use for understanding , it gives lazy sequences.
Here is the code for you. I took the time to run it on the command line; you only need to replace the name of the parsed XML file.
Caution 1: Avoid defining variables. Use local variables instead.
Caveat 2: This is the Java API for XML, so objects change; since you have a lazy sequence, if any changes happen to the mutable DOM tree during the replay, you may have unpleasant race changes.
Caveat 3: although this is a lazy structure, the whole DOM tree is already in memory anyway (I'm not quite sure about this last comment). I think the API is trying to defer reading the tree in memory until it is needed, but no guarantees). Therefore, if you run into problems with large XML documents, try to avoid the DOM approach.
(require ['clojure.java.io :as 'io]) (import [javax.xml.parsers DocumentBuilderFactory]) (import [org.xml.sax InputSource]) (def dbf (DocumentBuilderFactory/newInstance)) (doto dbf (.setValidating false) (.setNamespaceAware true) (.setIgnoringElementContentWhitespace true)) (def builder (.newDocumentBuilder dbf)) (def doc (.parse builder (InputSource. (io/reader "C:/workspace/myproject/pom.xml")))) (defn lazy-child-list [element] (let [nodelist (.getChildNodes element) len (.getLength nodelist)] (for [i (range len)] (.item nodelist i)))) ;; To print the children of an element (-> doc (.getDocumentElement) (lazy-child-list) (println)) ;; Prints clojure.lang.LazySeq (-> doc (.getDocumentElement) (lazy-child-list) (class) (println))
source share