clojure.xml provides XML parsing and emission for Clojure programs. Its main workflow turns an XML input source into an in-memory nested data structure for further processing, then converts that structure back to XML. section 22.0 section 22.1
The central operations have these contracts:
- xml/parse accepts a java.io.File, a URL string, or a java.io.InputStream and returns nested structs and vectors. section 22.1
- Each XML node contains :tag, :attrs, and :content; child elements and text appear in the :content vector. section 22.1
- xml/emit accepts parsed XML data and writes its XML representation to standard output, returning nil. section 22.1
- xml/parse accepts an optional startparse function for parser configuration, including disabling external schema loading or validation. section 22.1
Parse and emit a document
(require '[clojure.xml :as xml]) (def document (xml/parse "http://feeds.bbci.co.uk/news/rss.xml")) (keys document) ;; (:tag :attrs :content) (xml/emit {:tag :html :attrs {:xmlns "http://www.w3.org/1999/xhtml"} :content [{:tag :article :attrs nil :content ["Hello"]}]}) ;; <?xml version='1.0' encoding='UTF-8'?> ;; <html xmlns='http://www.w3.org/1999/xhtml'> ;; <article> ;; Hello ;; </article> ;; </html> ;; nil section 22.1
{:tag ... ; #1
:attrs ...
:content
[{:tag ... :attrs ... :content [...]}
...
{:tag ...
:attrs ...
:content
[{:tag ... :attrs ... :content [...]}
...
{:tag ... :attrs ... :content [...]}]}]}
Inspect the recursive XML node shape before traversing or transforming parsed content. section 22.1
The namespace remains functional but has been proposed for deprecation and was not substantially redesigned; use Clojure Data XML whenever possible. section 22.1
