Reputation: 58786
What is the easiest way to get the value of an element from an XML string in Clojure? I am looking for something like:
(get-value "<a><b>SOMETHING</b></a>)" "b")
to return
"SOMETHING"
Upvotes: 7
Views: 2970
Reputation: 564
Zippers can be handy for xml, they give you xpath like syntax that you can mix with native clojure functions.
user=> (require '[clojure zip xml] '[clojure.contrib.zip-filter [xml :as x]])
user=> (def z (-> (.getBytes "<a><b>SOMETHING</b></a>")
java.io.ByteArrayInputStream.
clojure.xml/parse clojure.zip/xml-zip))
user=> (x/xml1-> z :b x/text)
returns
"SOMETHING"
Upvotes: 10
Reputation: 1201
Using clj-xpath, ( https://github.com/brehaut/necessary-evil ) :
(use 'com.github.kyleburton.clj-xpath :only [$x:text])
($x:text "/a/b" "<a><b>SOMETHING</b></a>)")
Upvotes: 2
Reputation: 19717
Using Christophe Grand's great Enlive library:
(require '[net.cgrand.enlive-html :as html])
(map html/text
(html/select (html/html-snippet "<a><b>SOMETHING</b></a>") [:a :b]))
Upvotes: 6
Reputation: 19632
I do not know how idiomatic Clojure it is but if you happen to know and like XPath it can be used quite easily in Clojure due to its excellent interoperability with Java:
(import javax.xml.parsers.DocumentBuilderFactory)
(import javax.xml.xpath.XPathFactory)
(defn document [filename]
(-> (DocumentBuilderFactory/newInstance)
.newDocumentBuilder
(.parse filename)))
(defn get-value [document xpath]
(-> (XPathFactory/newInstance)
.newXPath
(.compile xpath)
(.evaluate document)))
user=> (get-value (document "something.xml") "//a/b/text()")
"SOMETHING"
Upvotes: 7
Reputation: 3885
Try this:
user=> (use 'clojure.xml)
user=> (for [x (xml-seq
(parse (java.io.File. file)))
:when (= :b (:tag x))]
(first (:content x)))
Check this link for more info.
Upvotes: 5
Reputation: 4773
Is this: Clojure XML Parsing not what you want? An alternative (external) source is here: http://blog.rguha.net/?p=510 .
Upvotes: 1