Skip to content

Instantly share code, notes, and snippets.

@carlohamalainen
Created October 27, 2011 04:42
Show Gist options
  • Save carlohamalainen/1318790 to your computer and use it in GitHub Desktop.
Save carlohamalainen/1318790 to your computer and use it in GitHub Desktop.
(ns blerp
(:require [clojure.xml :as xml])
(import (java.io ByteArrayInputStream)))
(def xml-data "<body> <myheader> <author> bob </author> </myheader> <data> d1 </data> <data>d2</data> <data>d3</data> </body>")
(def xml-parsed (let [input-stream (ByteArrayInputStream. (.getBytes xml-data))]
(xml-seq (xml/parse input-stream))))
(let [name-blob (for [x xml-parsed
:when (= (:tag x) :myheader)]
(for [data (:content x)
:when (= (:tag data) :author)]
(:content data)))
data-blob (for [x xml-parsed
:when (= (:tag x) :data)]
(.trim (first (:content x))))
result {:author (.trim (first (first (first name-blob))))
:data data-blob}]
(println result))
;; Output:
;;
;; {:author bob, :data (d1 d2 d3)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment