Last active
August 29, 2015 14:12
-
-
Save mnzk/194e226c262513d9ca1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(System.Reflection.Assembly/LoadWithPartialName "System.Xml.Linq") | |
(ns linq.xml | |
(:import [System.Xml.Linq XDocument XElement XAttribute XNode]) | |
(:import [System.IO MemoryStream StreamReader]) | |
(:import [System.Text Encoding]) | |
(:gen-class)) | |
(defmacro ^:private local-name [node] | |
`(.. ~node Name LocalName)) | |
(defprotocol XmlParsable | |
(parse* [this])) | |
(extend-protocol XmlParsable | |
XDocument | |
(parse* [this] (parse* (.Root this))) | |
XAttribute | |
(parse* [this] {(-> this local-name keyword) | |
(.Value this) }) | |
XElement | |
(parse* [this] | |
{:tag (-> this local-name keyword) | |
:attrs (->> (.Attributes this) (map parse*) (into {}) not-empty) | |
:content | |
(let [es (->> (.Elements this) (mapv parse*) not-empty) | |
v (.Value this)] | |
(cond | |
(not (nil? es)) es | |
(empty? v) nil | |
:else [v]))}) | |
XNode | |
(parse* [this] (.Value this))) | |
;; compatible with clojure.xml/parse | |
(defn parse [path] | |
(parse* (XDocument/Load path))) | |
;; "parse from xml string" | |
(defn parse-str [s] | |
(->> s (.GetBytes Encoding/UTF8) (MemoryStream.) (StreamReader.) | |
XDocument/Load | |
parse*)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment