Skip to content

Instantly share code, notes, and snippets.

@mrchrisadams

mrchrisadams/core.clj

Last active Apr 3, 2020
Embed
What would you like to do?
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