Clojure XML Parsing

  • 3 Answer(s)

    XML to parse in the file:

      <low-node>my text</low-node>

         load clojure.xml:

    user=> (use 'clojure.xml)

         while it is parsed, then the xml will have the structure like,

    {:tag :high-node, :attrs nil, :content [{:tag :low-node, :attrs nil, :content ["my text"]}]}

         then we can seq over the content of the file to get the content of the low-node:

    user=> (for [x (xml-seq
                  (parse ( file)))
                       :when (= :low-node (:tag x))]
              (first (:content x)))
    ("my text")

         Normally, if we need to have access to the whole list of information on low-node, we can change the :when predicate to (= (:high-node (:tag x))):

    user=> (for [x (xml-seq
                  (parse ( file)))
                        :when (= :high-node (:tag x))]
              (first (:content x)))
    ({:tag :low-node, :attrs nil, :content ["my text"]})
    Answered on January 12, 2019.
    Add Comment

         Use which is used to be prior to Clojure 1.3.

    file: myfile.xml

      <track id="t1"><name>Track one</name></track>
      <track id="t2"><name>Track two</name></track>


    ; Clojure 1.3
    (ns example
      (:use [ :only (attr text xml->)]) ; dep: see below
      (:require [clojure.xml :as xml]
              [ :as zip]))
    (def xml (xml/parse "myfile.xml"))
    (def zipped (zip/xml-zip xml))
    (xml-> zipped :track :name text)     ; ("Track one" "Track two")
    (xml-> zipped :track (attr :id))     ; ("t1" "t2")

         The user want to pull in a dependency on to get this nice read/filter functionality by unfortunate.

    [org.clojure/ "0.1.1"]

         Then for docs for The relative small source file here to see what is possible.

    Answered on January 12, 2019.
    Add Comment
    (require '[clojure.xml :as xml]
             '[ :as zip])
    ;;convenience function, first seen at later in src
    (defn zip-str [s]
          (xml/parse ( (.getBytes s)))))
    ;;parse from xml-strings to internal xml representation
    user=> (zip-str "<a href=''/>")
    [{:tag :a, :attrs {:href ""}, :content nil} nil]
    ;;root can be rendered with xml/emit-element
    user=> (xml/emit-element (zip/root [{:tag :a, :attrs {:href ""}, :content nil} nil]))
    <a href=''/>
    ;;printed (to assure it's not lazy and for performance), can be caught to string variable with with-out-str


    Answered 7 days ago.
    Add Comment

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.