Last active
July 17, 2019 02:44
-
-
Save alialrahahleh/7356474d131c1f63bf8b46c87b144eeb to your computer and use it in GitHub Desktop.
XML to CSV sample code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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