# data.xml **Repository Path**: mirrors_exoscale/data.xml ## Basic Information - **Project Name**: data.xml - **Description**: Functions to parse XML into lazy sequences and lazy trees and emit these as text - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-24 - **Last Updated**: 2026-05-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # data.xml [data.xml](https://github.com/clojure/data.xml) is a Clojure library for reading and writing XML data. This library is the successor to [lazy-xml](http://clojure.github.com/clojure-contrib/lazy-xml-api.html). data.xml has the following features: [![Build Status](https://secure.travis-ci.org/exoscale/data.xml.png)](http://travis-ci.org/exoscale/data.xml) * Parses XML documents into Clojure data structures * Emits XML from Clojure data structures * No additional dependencies if using 1.6 * Uses StAX internally * lazy - should allow parsing and emitting of large XML documents ## JDK 1.5 This library uses the pull parser that ships with JDK 1.6. If you running on JDK 1.6+, you do not need any additional dependencies. If you are using JDK 1.5, you will need to include a dependency on StAX. More information on this is available [here](https://github.com/exoscale/data.xml/blob/jdk16-pull-parser/jdk_15_readme.txt) ## Bugs Please report bugs using JIRA [here](http://dev.clojure.org/jira/browse/DXML). ## Installation Latest stable release: 0.0.8.1 * [All Released Versions](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22data.xml%22) * [Development Snapshot Versions](https://oss.sonatype.org/index.html#nexus-search;gav~org.clojure~data.xml~~~) ### Maven For Maven projects, add the following XML in your `pom.xml`'s `` section: org.clojure data.xml 0.0.8.1 ### Leiningen Add the following to the `project.clj` dependencies: [org.clojure/data.xml "0.0.8.1"] ## Examples The examples below assume you have added a `use` for data.xml: (require '[clojure.data.xml :refer :all]) data.xml supports parsing and emitting XML. The parsing functions will read XML from a [Reader](http://docs.oracle.com/javase/6/docs/api/java/io/Reader.html) or [InputStream](http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html). (let [input-xml (java.io.StringReader. " The baz value")] (parse input-xml)) #clojure.data.xml.Element{:tag :foo, :attrs {}, :content (#clojure.data.xml.Element{:tag :bar, :attrs {}, :content (#clojure.data.xml.Element{: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 "" :coalescing false) #clojure.data.xml.Element{: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, and written using a [java.io.Writer](http://docs.oracle.com/javase/6/docs/api/java/io/Writer.html).: (let [tags (element :foo {:foo-attr "foo value"} (element :bar {:bar-attr "bar value"} (element :baz {} "The baz value")))] (with-open [out-file (java.io.FileWriter. "/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"))) (sexp-as-element [: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 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 (java.io.FileWriter. "/tmp/foo.xml")] (emit tags out-file)) (with-open [input (java.io.FileInputStream. "/tmp/foo.xml")] (parse input))) #clojure.data.xml.Element{: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)))) true 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"))))) The baz value1 The baz value2 The baz value3 CDATA can be emitted: (emit-str (element :foo {} (cdata ""))) "]]>" But will be read as regular character data: (parse-str (emit-str (element :foo {} (cdata "")))) #clojure.data.xml.Element{:tag :foo, :attrs {}, :content ("")} Comments can also be emitted: (emit-str (element :foo {} (xml-comment "Just a goes here") (element :bar {} "and another element"))) "and another element" But are ignored when read: (emit-str (parse-str (emit-str (element :foo {} (xml-comment "Just a goes here") (element :bar {} "and another element"))))) "and another element" Generated API docs for data.xml are available [here](http://clojure.github.com/data.xml). ## License Licensed under the [Eclipse Public License](http://www.opensource.org/licenses/eclipse-1.0.php). ## Developer Information * [GitHub project](https://github.com/clojure/data.xml) * [Bug Tracker](http://dev.clojure.org/jira/browse/DXML) * [Continuous Integration](http://build.clojure.org/job/data.xml/) * [Compatibility Test Matrix](http://build.clojure.org/job/data.xml-test-matrix/) ## Contributing All contributions need to be made via patches attached to tickets in [JIRA](http://dev.clojure.org/jira/browse/DXML). Check the [Contributing to Clojure](http://clojure.org/contributing) page for more information.