| 1 | # data.xml |
| 2 | |
| 3 | [data.xml](https://github.com/clojure/data.xml) is a Clojure library for reading and writing XML data. This |
| 4 | library is the successor to |
| 5 | [lazy-xml](http://clojure.github.com/clojure-contrib/lazy-xml-api.html). |
| 6 | data.xml has the following features: |
| 7 | |
| 8 | * Parses XML documents into Clojure data structures |
| 9 | * Emits XML from Clojure data structures |
| 10 | * No additional dependencies if using JDK >= 1.6 |
| 11 | * Uses StAX internally |
| 12 | * lazy - should allow parsing and emitting of large XML documents |
| 13 | |
| 14 | ## API Reference |
| 15 | |
| 16 | Generated API docs for data.xml are available [here](http://clojure.github.com/data.xml). |
| 17 | |
| 18 | ## Bugs |
| 19 | |
| 20 | Please report bugs using JIRA [here](http://dev.clojure.org/jira/browse/DXML). |
| 21 | |
| 22 | ## Installation |
| 23 | |
| 24 | Latest stable release: `0.0.8` |
| 25 | |
| 26 | Latest preview release: `0.2.0-alpha5` |
| 27 | |
| 28 | (The main features of the `0.2.0` series are XML Namespace support and Clojurescript support) |
| 29 | |
| 30 | * [All Released Versions](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22data.xml%22) |
| 31 | |
| 32 | * [Development Snapshot Versions](https://oss.sonatype.org/index.html#nexus-search;gav~org.clojure~data.xml~~~) |
| 33 | |
| 34 | ### Maven |
| 35 | For Maven projects, add the following XML in your `pom.xml`'s `<dependencies>` section: |
| 36 | |
| 37 | For stable: |
| 38 | |
| 39 | <dependency> |
| 40 | <groupId>org.clojure</groupId> |
| 41 | <artifactId>data.xml</artifactId> |
| 42 | <version>0.0.8</version> |
| 43 | </dependency> |
| 44 | |
| 45 | --- |
| 46 | |
| 47 | For preview: |
| 48 | |
| 49 | <dependency> |
| 50 | <groupId>org.clojure</groupId> |
| 51 | <artifactId>data.xml</artifactId> |
| 52 | <version>0.2.0-alpha5</version> |
| 53 | </dependency> |
| 54 | |
| 55 | ### Leiningen |
| 56 | Add the following to the `project.clj` dependencies: |
| 57 | |
| 58 | For stable: |
| 59 | |
| 60 | [org.clojure/data.xml "0.0.8"] |
| 61 | |
| 62 | --- |
| 63 | |
| 64 | For preview: |
| 65 | |
| 66 | [org.clojure/data.xml "0.2.0-alpha5"] |
| 67 | |
| 68 | ## Examples |
| 69 | |
| 70 | The examples below assume you have added a `:refer :all` for data.xml: |
| 71 | |
| 72 | (require '[clojure.data.xml :refer :all]) |
| 73 | |
| 74 | data.xml supports parsing and emitting XML. The parsing functions will |
| 75 | read XML from a |
| 76 | [Reader](http://docs.oracle.com/javase/6/docs/api/java/io/Reader.html) |
| 77 | or |
| 78 | [InputStream](http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html). |
| 79 | |
| 80 | (let [input-xml (java.io.StringReader. "<?xml version=\"1.0\" encoding=\"UTF-8\"?> |
| 81 | <foo><bar><baz>The baz value</baz></bar></foo>")] |
| 82 | (parse input-xml)) |
| 83 | |
| 84 | #clojure.data.xml.Element{:tag :foo, |
| 85 | :attrs {}, |
| 86 | :content (#clojure.data.xml.Element{:tag :bar, |
| 87 | :attrs {}, |
| 88 | :content (#clojure.data.xml.Element{:tag :baz, |
| 89 | :attrs {}, |
| 90 | :content ("The baz value")})})} |
| 91 | |
| 92 | The data is returned as defrecords and can be manipulated using the |
| 93 | normal clojure data structure functions. Additional parsing options |
| 94 | can be passed via key pairs: |
| 95 | |
| 96 | (parse-str "<a><![CDATA[\nfoo bar\n]]><![CDATA[\nbaz\n]]></a>" :coalescing false) |
| 97 | #clojure.data.xml.Element{:tag :a, :attrs {}, :content ("\nfoo bar\n" "\nbaz\n")} |
| 98 | |
| 99 | XML elements can be created using the typical defrecord constructor |
| 100 | functions or the element function used below or just a plain map with :tag :attrs :content keys, and written using a |
| 101 | [java.io.Writer](http://docs.oracle.com/javase/6/docs/api/java/io/Writer.html).: |
| 102 | |
| 103 | (let [tags (element :foo {:foo-attr "foo value"} |
| 104 | (element :bar {:bar-attr "bar value"} |
| 105 | (element :baz {} "The baz value")))] |
| 106 | (with-open [out-file (java.io.FileWriter. "/tmp/foo.xml")] |
| 107 | (emit tags out-file))) |
| 108 | |
| 109 | ;;-> Writes XML to /tmp/foo.xml |
| 110 | |
| 111 | The same can also be expressed using a more Hiccup-like style of defining the elements using sexp-as-element: |
| 112 | |
| 113 | (= (element :foo {:foo-attr "foo value"} |
| 114 | (element :bar {:bar-attr "bar value"} |
| 115 | (element :baz {} "The baz value"))) |
| 116 | (sexp-as-element |
| 117 | [:foo {:foo-attr "foo value"} |
| 118 | [:bar {:bar-attr "bar value"} |
| 119 | [:baz {} "The baz value"]]])) |
| 120 | ;;-> true |
| 121 | |
| 122 | Comments and CDATA can also be emitted as an S-expression with the special tag names :-cdata and :-comment: |
| 123 | |
| 124 | (= (element :tag {:attr "value"} |
| 125 | (element :body {} (cdata "not parsed <stuff"))) |
| 126 | (sexp-as-element [:tag {:attr "value"} [:body {} [:-cdata "not parsed <stuff"]]] |
| 127 | ;;-> true |
| 128 | |
| 129 | XML can be "round tripped" through the library: |
| 130 | |
| 131 | (let [tags (element :foo {:foo-attr "foo value"} |
| 132 | (element :bar {:bar-attr "bar value"} |
| 133 | (element :baz {} "The baz value")))] |
| 134 | (with-open [out-file (java.io.FileWriter. "/tmp/foo.xml")] |
| 135 | (emit tags out-file)) |
| 136 | (with-open [input (java.io.FileInputStream. "/tmp/foo.xml")] |
| 137 | (parse input))) |
| 138 | |
| 139 | #clojure.data.xml.Element{:tag :foo, :attrs {:foo-attr "foo value"}...} |
| 140 | |
| 141 | There are also some string based functions that are useful for |
| 142 | debugging. |
| 143 | |
| 144 | (let [tags (element :foo {:foo-attr "foo value"} |
| 145 | (element :bar {:bar-attr "bar value"} |
| 146 | (element :baz {} "The baz value")))] |
| 147 | (= tags (parse-str (emit-str tags)))) |
| 148 | |
| 149 | true |
| 150 | |
| 151 | Indentation is supported, but should be treated as a debugging feature |
| 152 | as it's likely to be pretty slow: |
| 153 | |
| 154 | (print (indent-str (element :foo {:foo-attr "foo value"} |
| 155 | (element :bar {:bar-attr "bar value"} |
| 156 | (element :baz {} "The baz value1") |
| 157 | (element :baz {} "The baz value2") |
| 158 | (element :baz {} "The baz value3"))))) |
| 159 | |
| 160 | <?xml version="1.0" encoding="UTF-8"?> |
| 161 | <foo foo-attr="foo value"> |
| 162 | <bar bar-attr="bar value"> |
| 163 | <baz>The baz value1</baz> |
| 164 | <baz>The baz value2</baz> |
| 165 | <baz>The baz value3</baz> |
| 166 | </bar> |
| 167 | </foo> |
| 168 | |
| 169 | CDATA can be emitted: |
| 170 | |
| 171 | (emit-str (element :foo {} |
| 172 | (cdata "<non><escaped><info><here>"))) |
| 173 | |
| 174 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo><![CDATA[<non><escaped><info><here>]]></foo>" |
| 175 | |
| 176 | But will be read as regular character data: |
| 177 | |
| 178 | (parse-str (emit-str (element :foo {} |
| 179 | (cdata "<non><escaped><info><here>")))) |
| 180 | |
| 181 | #clojure.data.xml.Element{:tag :foo, :attrs {}, :content ("<non><escaped><info><here>")} |
| 182 | |
| 183 | Comments can also be emitted: |
| 184 | |
| 185 | (emit-str (element :foo {} |
| 186 | (xml-comment "Just a <comment> goes here") |
| 187 | (element :bar {} "and another element"))) |
| 188 | |
| 189 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo><!--Just a <comment> goes here--><bar>and another element</bar></foo>" |
| 190 | |
| 191 | But are ignored when read: |
| 192 | |
| 193 | (emit-str |
| 194 | (parse-str |
| 195 | (emit-str (element :foo {} |
| 196 | (xml-comment "Just a <comment> goes here") |
| 197 | (element :bar {} "and another element"))))) |
| 198 | |
| 199 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo><bar>and another element</bar></foo>" |
| 200 | |
| 201 | ## Namespace Support |
| 202 | |
| 203 | XML Namespaced names (QNames) are encoded into clojure keywords, by percent-encoding the (XML) namespace: `{http://www.w3.org/1999/xhtml}head` is encoded in data.xml as `:http%3A%2F%2Fwww.w3.org%2F1999%2Fxhtml/head`. |
| 204 | |
| 205 | Below is an example of parsing an XHTML document: |
| 206 | |
| 207 | (parse-str "<?xml version=\"1.0\" encoding=\"UTF-8\"?> |
| 208 | <foo:html xmlns:foo=\"http://www.w3.org/1999/xhtml\"/>") |
| 209 | |
| 210 | #...Element{:tag :xmlns.http%3A%2F%2Fwww.w3.org%2F1999%2Fxhtml/html, |
| 211 | :attrs {}, |
| 212 | :content ()} |
| 213 | |
| 214 | Emitting namespaced XML is usually done by using `alias-uri` in combination with clojure's built-in `::kw-ns/shorthands`: |
| 215 | |
| 216 | (alias-uri 'xh "http://www.w3.org/1999/xhtml") |
| 217 | |
| 218 | (emit-str {:tag ::xh/html |
| 219 | :content [{:tag ::xh/head} {:tag ::xh/body :content ["DOCUMENT"]}]}) |
| 220 | |
| 221 | <?xml version="1.0" encoding="UTF-8"?> |
| 222 | <a:html xmlns:a="http://www.w3.org/1999/xhtml"> |
| 223 | <a:head/> |
| 224 | <a:body>DOCUMENT</a:body> |
| 225 | </a:html> |
| 226 | |
| 227 | It is also allowable to use `javax.xml.namespace.QName` instances, as well as strings with the informal `{ns}n` encoding. |
| 228 | |
| 229 | (emit-str {:tag (qname "http://www.w3.org/1999/xhtml" "html")}) |
| 230 | (emit-str {:tag "{http://www.w3.org/1999/xhtml}html"}) |
| 231 | |
| 232 | <?xml version=\"1.0\" encoding=\"UTF-8\"?><a:html xmlns:a=\"http://www.w3.org/1999/xhtml\"></a:html> |
| 233 | |
| 234 | ### Namespace Prefixes |
| 235 | |
| 236 | Prefixes are mostly an artifact of xml serialisation. They can be |
| 237 | customized by explicitly declaring them as attributes in the `xmlns` |
| 238 | kw-namespace: |
| 239 | |
| 240 | (emit-str (element (qname "http://www.w3.org/1999/xhtml" "title") |
| 241 | {:xmlns/foo "http://www.w3.org/1999/xhtml"} |
| 242 | "Example title")) |
| 243 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo:title xmlns:foo=\"http://www.w3.org/1999/xhtml\">Example title</foo:title>" |
| 244 | |
| 245 | Not specifying a namespace prefix will results in a prefix being generated: |
| 246 | |
| 247 | (emit-str (element ::xh/title |
| 248 | {} |
| 249 | "Example title")) |
| 250 | |
| 251 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:title xmlns:a=\"http://www.w3.org/1999/xhtml\">Example title</a:title>" |
| 252 | |
| 253 | The above example auto assigns prefixes for the namespaces used. In |
| 254 | this case it was named `a` by the emitter. Emitting several nested |
| 255 | tags with the same namespace will use one prefix: |
| 256 | |
| 257 | (emit-str (element ::xh/html |
| 258 | {} |
| 259 | (element ::xh/head |
| 260 | {} |
| 261 | (element ::xh/title |
| 262 | {} |
| 263 | "Example title")))) |
| 264 | |
| 265 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:html xmlns:a=\"http://www.w3.org/1999/xhtml\"><a:head><a:title>Example title</a:title></a:head></a:html>" |
| 266 | |
| 267 | Note that the jdk QName ignores namespace prefixes for equality, but allows to preserve them for emitting. |
| 268 | |
| 269 | (= (parse-str "<foo:title xmlns:foo=\"http://www.w3.org/1999/xhtml2\">Example title</foo:title>") |
| 270 | (parse-str "<bar:title xmlns:bar=\"http://www.w3.org/1999/xhtml2\">Example title</bar:title>")) |
| 271 | |
| 272 | 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. |
| 273 | |
| 274 | (emit-str (parse-str "<foo:element xmlns:foo=\"FOO:\" />")) |
| 275 | |
| 276 | ## Location information as meta |
| 277 | |
| 278 | By default the parser attaches location information as element meta, |
| 279 | `:character-offset`, `:column-number` and `:line-number` are available under |
| 280 | the `:clojure.data.xml/location-info` key: |
| 281 | |
| 282 | (deftest test-location-meta |
| 283 | (let [input "<a><b/>\n<b/></a>" |
| 284 | location-meta (comp :clojure.data.xml/location-info meta)] |
| 285 | (is (= 1 (-> input parse-str location-meta :line-number))) |
| 286 | |
| 287 | To elide location information, pass `:location-info false` to the parser: |
| 288 | |
| 289 | (parse-str your-input :location-info false) |
| 290 | |
| 291 | ## Clojurescript support |
| 292 | |
| 293 | The Clojurescript implementation uses the same namespace as the Clojure one `clojure.data.xml`. |
| 294 | |
| 295 | ### Native DOM support |
| 296 | |
| 297 | data.xml can directly work with native dom nodes. |
| 298 | |
| 299 | - To parse into DOM objects, call parse with `:raw true` |
| 300 | - To use DOM objects like regular persistent maps, call `(extend-dom-as-data!)`. |
| 301 | This extends the native dom node prototypes to Clojurescript collection protocols, such that you can treat them as data.xml parse trees. |
| 302 | - To coerce to native dom use `element-node` |
| 303 | - To coerce to records use `element-data` |
| 304 | |
| 305 | ### Missing Features, Patches Welcome |
| 306 | |
| 307 | #### Streaming |
| 308 | |
| 309 | 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. |
| 310 | |
| 311 | 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`. |
| 312 | |
| 313 | 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. |
| 314 | |
| 315 | #### Utilities |
| 316 | |
| 317 | Some utilities, like `process/*-xmlns`, `prxml/sexp-as-*`, `indent` aren't yet implemented. |
| 318 | |
| 319 | #### Immutable updates for dom types |
| 320 | |
| 321 | Make `extend-dom-as-data!` also support assoc, ... on dom nodes. |
| 322 | |
| 323 | #### Feel free to pick a [ticket](http://dev.clojure.org/jira/secure/IssueNavigator.jspa?reset=true&jqlQuery=project+%3D+DXML+AND+status+in+%28Open%2C+%22In+Progress%22%2C+Reopened%29) to work on |
| 324 | |
| 325 | ## License |
| 326 | |
| 327 | Licensed under the [Eclipse Public License](http://www.opensource.org/licenses/eclipse-1.0.php). |
| 328 | |
| 329 | ## Developer Information |
| 330 | |
| 331 | * [GitHub project](https://github.com/clojure/data.xml) |
| 332 | |
| 333 | * [Bug Tracker](http://dev.clojure.org/jira/browse/DXML) |
| 334 | |
| 335 | * [Continuous Integration](http://build.clojure.org/job/data.xml/) |
| 336 | |
| 337 | * [Compatibility Test Matrix](http://build.clojure.org/job/data.xml-test-matrix/) |
| 338 | |
| 339 | ## Contributing |
| 340 | |
| 341 | All contributions need to be made via patches attached to tickets in |
| 342 | [JIRA](http://dev.clojure.org/jira/browse/DXML). Check the |
| 343 | [Contributing to Clojure](http://clojure.org/contributing) page for |
| 344 | more information. |