git clone ''



data.xml is a Clojure library for reading and writing XML data. This library is the successor to lazy-xml. data.xml has the following features:

API Reference

Generated API docs for data.xml are available here.


Please report bugs using JIRA here.


Latest stable release: 0.0.8

Latest preview release: 0.2.0-alpha6

(The main features of the 0.2.0 series are XML Namespace support and Clojurescript support)


For Maven projects, add the following XML in your pom.xml's <dependencies> section:

For stable:


For preview:



Add the following to the project.clj dependencies:

For stable:

[org.clojure/data.xml "0.0.8"]

For preview:

[org.clojure/data.xml "0.2.0-alpha6"]


The examples below assume you have added a :refer :all for data.xml:

(require '[ :refer :all])

data.xml supports parsing and emitting XML. The parsing functions will read XML from a Reader or InputStream.

(let [input-xml ( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
                                        <foo><bar><baz>The baz value</baz></bar></foo>")]
  (parse input-xml)){:tag :foo,
                          :attrs {},
                          :content ({:tag :bar,
                                                              :attrs {},
                                                              :content ({:tag :baz,
                                                                                                  :attrs {},
                                                                                                  :content ("The baz value")})})}

The data is returned as defrecords and can be manipulated using the normal clojure data structure functions. Additional parsing options can be passed via key pairs:

(parse-str "<a><![CDATA[\nfoo bar\n]]><![CDATA[\nbaz\n]]></a>" :coalescing false){:tag :a, :attrs {}, :content ("\nfoo bar\n" "\nbaz\n")}

XML elements can be created using the typical defrecord constructor functions or the element function used below or just a plain map with :tag :attrs :content keys, and written using a

(let [tags (element :foo {:foo-attr "foo value"}
             (element :bar {:bar-attr "bar value"}
               (element :baz {} "The baz value")))]
  (with-open [out-file ( "/tmp/foo.xml")]
    (emit tags out-file)))

;;-> Writes XML to /tmp/foo.xml

The same can also be expressed using a more Hiccup-like style of defining the elements using sexp-as-element:

(= (element :foo {:foo-attr "foo value"}
     (element :bar {:bar-attr "bar value"}
       (element :baz {} "The baz value")))
      [:foo {:foo-attr "foo value"}
       [:bar {:bar-attr "bar value"}
        [:baz {} "The baz value"]]]))
;;-> true

Comments and CDATA can also be emitted as an S-expression with the special tag names :-cdata and :-comment:

(= (element :tag {:attr "value"}
     (element :body {} (cdata "not parsed <stuff")))
   (sexp-as-element [:tag {:attr "value"} [:body {} [:-cdata "not parsed <stuff"]]]
;;-> true

XML can be “round tripped” through the library:

(let [tags (element :foo {:foo-attr "foo value"}
             (element :bar {:bar-attr "bar value"}
               (element :baz {} "The baz value")))]
  (with-open [out-file ( "/tmp/foo.xml")]
    (emit tags out-file))
  (with-open [input ( "/tmp/foo.xml")]
    (parse input))){:tag :foo, :attrs {:foo-attr "foo value"}...}

There are also some string based functions that are useful for debugging.

(let [tags (element :foo {:foo-attr "foo value"}
             (element :bar {:bar-attr "bar value"}
               (element :baz {} "The baz value")))]
  (= tags (parse-str (emit-str tags))))


Indentation is supported, but should be treated as a debugging feature as it's likely to be pretty slow:

(print (indent-str (element :foo {:foo-attr "foo value"}
                     (element :bar {:bar-attr "bar value"}
                       (element :baz {} "The baz value1")
                       (element :baz {} "The baz value2")
                       (element :baz {} "The baz value3")))))

<?xml version="1.0" encoding="UTF-8"?>
<foo foo-attr="foo value">
  <bar bar-attr="bar value">
    <baz>The baz value1</baz>
    <baz>The baz value2</baz>
    <baz>The baz value3</baz>

CDATA can be emitted:

(emit-str (element :foo {}
            (cdata "<non><escaped><info><here>")))

"<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo><![CDATA[<non><escaped><info><here>]]></foo>"

But will be read as regular character data:

(parse-str (emit-str (element :foo {}
             (cdata "<non><escaped><info><here>")))){:tag :foo, :attrs {}, :content ("<non><escaped><info><here>")}

Comments can also be emitted:

(emit-str (element :foo {}
            (xml-comment "Just a <comment> goes here")
            (element :bar {} "and another element")))

"<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo><!--Just a <comment> goes here--><bar>and another element</bar></foo>"

But are ignored when read:

    (emit-str (element :foo {}
                (xml-comment "Just a <comment> goes here")
                (element :bar {} "and another element")))))

"<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo><bar>and another element</bar></foo>"

Namespace Support

XML Namespaced names (QNames) are encoded into clojure keywords, by percent-encoding the (XML) namespace: {}head is encoded in data.xml as

Below is an example of parsing an XHTML document:

(parse-str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
            <foo:html xmlns:foo=\"\"/>")

            :attrs {},
            :content ()}

Emitting namespaced XML is usually done by using alias-uri in combination with clojure's built-in ::kw-ns/shorthands:

(alias-uri 'xh "")

(emit-str {:tag ::xh/html
           :content [{:tag ::xh/head} {:tag ::xh/body :content ["DOCUMENT"]}]})

<?xml version="1.0" encoding="UTF-8"?>
<a:html xmlns:a="">

It is also allowable to use javax.xml.namespace.QName instances, as well as strings with the informal {ns}n encoding.

(emit-str {:tag (qname "" "html")})
(emit-str {:tag "{}html"})

<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:html xmlns:a=\"\"></a:html>

Namespace Prefixes

Prefixes are mostly an artifact of xml serialisation. They can be customized by explicitly declaring them as attributes in the xmlns kw-namespace:

(emit-str (element (qname "" "title")
                   {:xmlns/foo ""}
                   "Example title"))
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo:title xmlns:foo=\"\">Example title</foo:title>"

Not specifying a namespace prefix will results in a prefix being generated:

(emit-str (element ::xh/title
                   "Example title"))

"<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:title xmlns:a=\"\">Example title</a:title>"

The above example auto assigns prefixes for the namespaces used. In this case it was named a by the emitter. Emitting several nested tags with the same namespace will use one prefix:

(emit-str (element ::xh/html
                   (element ::xh/head
                            (element ::xh/title
                                     "Example title"))))

"<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:html xmlns:a=\"\"><a:head><a:title>Example title</a:title></a:head></a:html>"

Note that the jdk QName ignores namespace prefixes for equality, but allows to preserve them for emitting.

(= (parse-str "<foo:title xmlns:foo=\"\">Example title</foo:title>")
   (parse-str "<bar:title xmlns:bar=\"\">Example title</bar:title>"))

In data.xml prefix mappings are (by default) retained in metadata on a tag record. If there is no metadata, new prefixes will be generated when emitting.

(emit-str (parse-str "<foo:element xmlns:foo=\"FOO:\" />"))

Location information as meta

By default the parser attaches location information as element meta, :character-offset, :column-number and :line-number are available under the key:

(deftest test-location-meta
  (let [input "<a><b/>\n<b/></a>"
        location-meta (comp meta)]
    (is (= 1 (-> input parse-str location-meta :line-number)))

To elide location information, pass :location-info false to the parser:

(parse-str your-input :location-info false)

Clojurescript support

The Clojurescript implementation uses the same namespace as the Clojure one

Native DOM support

data.xml can directly work with native dom nodes.

Missing Features, Patches Welcome


data.xml on Clojurescript doesn't currently support streaming, hence only the *-str variants of parse/emit are implemented. Those are just wrappers for browser's native xml parsing/printing.

Pull parsing doesn't seem the right solution for Clojurescript, because when code cannot block, the parser has no way of waiting on its input. For this reason, parsing in Clojurescript cannot be based around event-seq.

Push parsing, on the other hand should not pose a problem, because when data arrives in a callback, it can be pushed on into the parser. Fortunately, clojure already has a nice push-based pendant for lazy sequences: transducers.


Some utilities, like process/*-xmlns, prxml/sexp-as-*, indent aren't yet implemented.

Immutable updates for dom types

Make extend-dom-as-data! also support assoc, … on dom nodes.

Feel free to pick a ticket to work on


Licensed under the Eclipse Public License.

Developer Information


All contributions need to be made via patches attached to tickets in JIRA. Check the Contributing to Clojure page for more information.