Guest User

Untitled

a guest
Jul 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. (get-value "<a><b>SOMETHING</b></a>)" "b")
  2.  
  3. "SOMETHING"
  4.  
  5. user=> (use 'clojure.xml)
  6.  
  7. user=> (for [x (xml-seq
  8. (parse (java.io.File. file)))
  9. :when (= :b (:tag x))]
  10. (first (:content x)))
  11.  
  12. (require '[net.cgrand.enlive-html :as html])
  13.  
  14. (map html/text
  15. (html/select (html/html-snippet "<a><b>SOMETHING</b></a>") [:a :b]))
  16.  
  17. (import javax.xml.parsers.DocumentBuilderFactory)
  18. (import javax.xml.xpath.XPathFactory)
  19.  
  20. (defn document [filename]
  21. (-> (DocumentBuilderFactory/newInstance)
  22. .newDocumentBuilder
  23. (.parse filename)))
  24.  
  25. (defn get-value [document xpath]
  26. (-> (XPathFactory/newInstance)
  27. .newXPath
  28. (.compile xpath)
  29. (.evaluate document)))
  30.  
  31. user=> (get-value (document "something.xml") "//a/b/text()")
  32. "SOMETHING"
  33.  
  34. user=> (require '[clojure zip xml] '[clojure.contrib.zip-filter [xml :as x]])
  35.  
  36. user=> (def z (-> (.getBytes "<a><b>SOMETHING</b></a>")
  37. java.io.ByteArrayInputStream.
  38. clojure.xml/parse clojure.zip/xml-zip))
  39.  
  40. user=> (x/xml1-> z :b x/text)
  41.  
  42. "SOMETHING"
  43.  
  44. (use 'com.github.kyleburton.clj-xpath :only [$x:text])
  45. ($x:text "/a/b" "<a><b>SOMETHING</b></a>)")
Add Comment
Please, Sign In to add comment