Skip to content

Instantly share code, notes, and snippets.

@alialrahahleh
Last active July 17, 2019 02:44
Show Gist options
  • Select an option

  • Save alialrahahleh/7356474d131c1f63bf8b46c87b144eeb to your computer and use it in GitHub Desktop.

Select an option

Save alialrahahleh/7356474d131c1f63bf8b46c87b144eeb to your computer and use it in GitHub Desktop.
XML to CSV sample code
ns st.core
(:gen-class))
(def init-state {:matching-enabled false
:p-headers true
:headers []
:values []})
(defn print-line [obj]
(when (obj :p-headers) (println (clojure.string/join "," (obj :headers))))
(println (clojure.string/join "," (obj :values))))
(defn process [obj, item]
(cond
(re-find #"<ITEM>" item) (assoc obj :matching-enabled true)
(re-find #"</ITEM>" item) (do
(print-line obj)
(assoc init-state :p-headers false))
(:matching-enabled obj) (do
(assoc obj :headers (conj (:headers obj)
(second
(re-find #"<([^>]+)>" item)))
:values (conj (:values obj)
(second
(re-find #">([^<]+)<" item)))))))
(defn read-file [name]
(with-open [rdr (clojure.java.io/reader name)]
(into [] (reduce process init-state (line-seq rdr)))))
(defn -main []
(read-file "file.xml"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment