Clojure core environment and runtime library providing a dynamic, general-purpose programming language on the JVM.
—
Utilities for processing structured data formats including EDN (Extensible Data Notation), UUID handling, and XML parsing through specialized namespaces: clojure.edn for EDN format reading, clojure.uuid for UUID data readers, and clojure.xml for XML processing.
Reading and parsing EDN (Extensible Data Notation) format data - a subset of Clojure data structures.
(clojure.edn/read)
(clojure.edn/read stream)
(clojure.edn/read opts stream)
;; Reads next object from stream (PushbackReader), defaults to *in*
;; opts: map with optional keys:
;; :eof - value to return on end-of-file (default: throws exception)
;; :readers - map of tag symbols to data-reader functions
;; :default - function of two args called when no reader found for tag
;; Returns: parsed EDN data structure
(clojure.edn/read-string s)
(clojure.edn/read-string opts s)
;; Reads one object from string s, returns nil when s is nil/empty
;; s: EDN string to parse
;; opts: map as per clojure.edn/read
;; Returns: parsed EDN data structure or nilUUID literal syntax support and print methods for java.util.UUID objects.
;; UUID literal syntax
#uuid "550e8400-e29b-41d4-a716-446655440000"
;; Creates java.util.UUID instance from string representation
;; Returns: java.util.UUID
;; Print methods (automatically available)
;; print-method for java.util.UUID outputs #uuid "uuid-string"
;; print-dup for java.util.UUID outputs #uuid "uuid-string"XML parsing and emission functionality for working with XML documents.
XML documents are represented as nested maps with standard keys:
;; XML Element Structure
{:tag :element-name
:attrs {:attr-name "attr-value" ...} ; nil if no attributes
:content [child-elements-and-text...]} ; nil if no content
;; Accessor functions
(clojure.xml/tag element)
;; Returns the tag (keyword) of XML element
;; Returns: keyword
(clojure.xml/attrs element)
;; Returns the attributes map of XML element
;; Returns: map of keyword->string or nil
(clojure.xml/content element)
;; Returns the content vector of XML element
;; Returns: vector of child elements and text or nil(clojure.xml/parse source)
;; Parses XML from various source types
;; source: File, InputStream, Reader, URI, URL, or String filename
;; Returns: parsed XML as nested element maps
(clojure.xml/sax-parser)
;; Creates new SAX parser instance
;; Returns: javax.xml.parsers.SAXParser
(clojure.xml/startparse-sax source content-handler)
;; Low-level SAX parsing with custom content handler
;; source: XML source
;; content-handler: SAX ContentHandler implementation
;; Returns: nil (handler processes events)
(clojure.xml/startparse-sax-safe source content-handler)
;; Safe SAX parsing with external entity processing disabled
;; source: XML source
;; content-handler: SAX ContentHandler implementation
;; Returns: nil(clojure.xml/disable-external-entities parser)
;; Configures SAX parser to disable external entity processing for security
;; parser: SAXParser instance
;; Returns: configured parser(clojure.xml/emit element)
;; Prints XML element to *out* in XML format
;; element: XML element map
;; Returns: nil (prints to *out*)
(clojure.xml/emit-element element)
;; Internal function for emitting XML element structure
;; element: XML element map
;; Returns: nil (prints to *out*)Usage Examples:
;; EDN reading examples
(require '[clojure.edn :as edn])
;; Basic EDN parsing
(edn/read-string "{:name \"John\" :age 30}")
;; => {:name "John", :age 30}
(edn/read-string "[1 2 3 #{:a :b}]")
;; => [1 2 3 #{:a :b}]
;; EDN with custom readers
(edn/read-string
{:readers {'date #(java.util.Date. %)}}
"#date 1672531200000")
;; => #inst "2023-01-01T00:00:00.000-00:00"
;; Reading from stream
(with-open [reader (java.io.PushbackReader.
(java.io.StringReader. "{:a 1 :b 2}"))]
(edn/read reader))
;; => {:a 1, :b 2}
;; Handle EOF gracefully
(edn/read-string {:eof :done} "")
;; => :done
;; UUID examples
(require '[clojure.uuid])
;; Create UUID from literal
#uuid "550e8400-e29b-41d4-a716-446655440000"
;; => #uuid "550e8400-e29b-41d4-a716-446655440000"
;; UUID in data structures
(def user-data {:id #uuid "550e8400-e29b-41d4-a716-446655440000"
:name "Alice"})
;; UUID printing
(str #uuid "550e8400-e29b-41d4-a716-446655440000")
;; => "550e8400-e29b-41d4-a716-446655440000"
(pr-str #uuid "550e8400-e29b-41d4-a716-446655440000")
;; => "#uuid \"550e8400-e29b-41d4-a716-446655440000\""
;; XML processing examples
(require '[clojure.xml :as xml])
;; Parse XML string
(def xml-str "<book id='123'>
<title>Clojure Programming</title>
<author>Rich Hickey</author>
<pages>400</pages>
</book>")
;; Note: parse expects file/stream, for strings use:
(def book-xml
(xml/parse (java.io.ByteArrayInputStream. (.getBytes xml-str))))
;; Access parsed structure
(xml/tag book-xml) ; => :book
(xml/attrs book-xml) ; => {:id "123"}
(xml/content book-xml) ; => [{:tag :title, :content ["Clojure Programming"]} ...]
;; Extract specific data
(defn get-text [element]
(when element
(first (xml/content element))))
(defn find-element [tag elements]
(first (filter #(= (xml/tag %) tag) elements)))
(let [children (xml/content book-xml)]
{:title (get-text (find-element :title children))
:author (get-text (find-element :author children))
:pages (get-text (find-element :pages children))})
;; => {:title "Clojure Programming", :author "Rich Hickey", :pages "400"}
;; Emit XML (prints to *out*)
(xml/emit {:tag :person
:attrs {:id "1"}
:content [{:tag :name :content ["John Doe"]}
{:tag :age :content ["30"]}]})
;; Prints: <person id='1'><name>John Doe</name><age>30</age></person>
;; Safe XML parsing (recommended for untrusted input)
(defn safe-parse-xml [source]
(let [parser (xml/sax-parser)]
(xml/disable-external-entities parser)
(xml/startparse-sax-safe source xml/content-handler)))
;; Working with XML files
(xml/parse "data.xml") ; parse from file
(xml/parse (clojure.java.io/input-stream "http://example.com/data.xml")) ; from URL
;; Complex XML processing with zipper
(require '[clojure.xml :as xml]
'[clojure.zip :as zip])
(defn xml-zip [root]
(zip/zipper
#(and (map? %) (xml/content %))
xml/content
(fn [node children]
(assoc node :content (vec children)))
root))
;; Navigate XML with zipper
(let [xml-data (xml/parse "books.xml")
z (xml-zip xml-data)]
; Navigate and transform XML using zipper functions
)Install with Tessl CLI
npx tessl i tessl/maven-org-clojure--clojure