Skip to content

Instantly share code, notes, and snippets.

@mrchrisadams
Last active April 3, 2020 08:36
Show Gist options
  • Save mrchrisadams/cb9b3af65757b9fb9253b0c3cfa1d6cd to your computer and use it in GitHub Desktop.
Save mrchrisadams/cb9b3af65757b9fb9253b0c3cfa1d6cd to your computer and use it in GitHub Desktop.
I'm trying to learn to use clojure to parse a wordpress XML and generate markdown files for each one.
; add a basic namespace, I guess
(ns thingo.core)
(require '[clojure.java.io :as io])
(require '[clojure.xml :as xml])
(require '[clojure.zip :as zip])
(require '[clojure.data.zip.xml :as zip-xml])
(require '[clojure.pprint :refer pprint :as pp])
(def blog-file "./path-to-wordpress-export-file.xml")
(def root (-> blog-file
io/resource
io/file
xml/parse
zip/xml-zip))
; I want a vector of maps, where each map has all the attributes of the items.
(def posts (zip-xml/xml-> root :channel
:item [(keyword "wp:post_type") "post"]))
(defn post->map
"Return a map for the corresponding post we pass in."
[post]
{:title (zip-xml/xml1-> post :title zip-xml/text)
:content (zip-xml/xml1-> post (keyword "content:encoded") zip-xml/text)})
; inspect a single post
(let [[post & rest] posts]
(post->map post))
; iterate through the posts
(pp (for [post posts]
(post->map post)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment