I am having trouble figuring out what the elements are in parsed xml input, after I extract values using the :content key. Given the following parsed xml input, how can I extract the values for all the :content keys?
#clojure.data.xml.Element{ :tag :Header, :attrs {}, :content ( #clojure.data.xml.Element{ :tag :ExportType, :attrs {}, :content ("Tamper Export")} #clojure.data.xml.Element{ :tag :CurrentDateTime, :attrs {}, :content ("2012-06-26T15:40:22.063")} #clojure.data.xml.Element{ :tag :ScheduledDateTime, :attrs {}, :content ("2012-06-25T00:00:00")} #clojure.data.xml.Element{ :tag :ExportGuid, :attrs {}, :content ("{06643D9B-DCD3-459B-86A6-D21B20A03576}")} #clojure.data.xml.Element{ :tag :FractionalReadIndicator, :attrs {}, :content ("1")})}
This output was created by parsing an xml file using data.xml, and then extracted using
(first (:content parsed-xml-input))
Thank you.
The following is giving me only device type and device id, which is only part of :content in the xml file. <DeviceId>80580608</DeviceId><DeviceType>43</DeviceType>
(defn extract-inner-map-val
"Returns a map of embedded :content tag and value."
[item]
(let [key-elem (-> item :content first)
val-elem (-> item :content second)]
[(-> key-elem :content first)
(-> val-elem :content first)]))
(defn extract-content-from-map
"Accepts a sequence of values associated with a map key,
and transforms them into a map of key/value pairs."
[parsed-map]
(into {} (map extract-inner-map-val (:content parsed-map))))
this basically comes down to walking the tree and selecting all the :content. You can do this and a lot more with clojure.zip (Functional Zippers), though in this case that is a bit heavy handed when all you really need is a tree walk.
You may want it willout the flatten, that just makes it print more nicely.