Code Monkey home page Code Monkey logo

xml-writer's Introduction

xml-writer

xml-writer generates XML using StAX in a manner compatible with clojure.data.xml but with lower overhead.

  • Emits XML from Clojure data structures
  • No additional dependencies if using 1.6
  • Uses StAX internally

Usage

Leiningen

Add the following to the project.clj dependencies:

[xml-writer "0.1.1"]

Benchmarks

$ lein with-profile dev perforate
Performing task 'perforate' with profile(s): 'dev'
Benchmarking profiles:  [:dev :bench]
======================
Reflection warning, clojure/data/xml.clj:294:17 - call to createXMLStreamReader can't be resolved.
Reflection warning, clojure/data/xml.clj:362:5 - call to transform can't be resolved.
Goal:  Emit XML
-----
Case:  :clojure-data-xml-nested
Evaluation count : 180 in 60 samples of 3 calls.
Execution time mean : 353.511661 ms
Execution time std-deviation : 27.027851 ms
Execution time lower quantile : 341.283025 ms ( 2.5%)
Execution time upper quantile : 393.613717 ms (97.5%)

Found 2 outliers in 60 samples (3.3333 %)
low-severe       2 (3.3333 %)
Variance from outliers : 56.8174 % Variance is severely inflated by outliers

Case:  :xml-writer-10k
Evaluation count : 5160 in 60 samples of 86 calls.
Execution time mean : 11.571566 ms
Execution time std-deviation : 81.898332 µs
Execution time lower quantile : 11.427674 ms ( 2.5%)
Execution time upper quantile : 11.743721 ms (97.5%)

Case:  :xml-writer-nested
Evaluation count : 480 in 60 samples of 8 calls.
Execution time mean : 132.103054 ms
Execution time std-deviation : 555.582421 µs
Execution time lower quantile : 131.241375 ms ( 2.5%)
Execution time upper quantile : 133.431791 ms (97.5%)

Found 2 outliers in 60 samples (3.3333 %)
low-severe       2 (3.3333 %)
Variance from outliers : 1.6389 % Variance is slightly inflated by outliers

Case:  :clojure-data-xml-10k
Evaluation count : 1320 in 60 samples of 22 calls.
Execution time mean : 46.277786 ms
Execution time std-deviation : 459.673914 µs
Execution time lower quantile : 45.708318 ms ( 2.5%)
Execution time upper quantile : 47.322723 ms (97.5%)

Found 2 outliers in 60 samples (3.3333 %)
low-severe       2 (3.3333 %)
Variance from outliers : 1.6389 % Variance is slightly inflated by outliers

Examples

A single root element:

user> (require '[xml-writer.core :as xml])
user> (xml/emit-sexp-str [:root])
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><root></root>"

An element with optional attributes:

user> (xml/emit-sexp-str [:root [:child "value"]])
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child>value</child></root>"

With attributes:

user> (xml/emit-sexp-str [:root [:child {:attribute "value"} "value"]])
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child attribute=\"value\">value</child></root>"

With prefixed elements and attributes:

user> (xml/emit-sexp-str [:root {:xmlns:xsi "http://www.w3.org/2001/XMLSchema-instance"} [:child {:xsi:nil true} nil]])
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><child xsi:nil=\"true\"></child></root>"

With :invalid-char-behavior option. Supports :fail, :replace-all, and replace-whitespace. If no option is supplied, defaults to Woodstox failing handler.

user> (xml/emit-sexp-str [:root "value1\u001Cvalue2"] :invalid-char-behavior :replace-whitespace)
"<?xml version='1.0' encoding='UTF-8'?><root>value1 value2</root>"

TODO

  • use empty elements when values are nil [:elem {} nil] ->
  • indenting transformer

License

Copyright © 2013 Ben Poweski

Distributed under the Eclipse Public License, the same as Clojure.

xml-writer's People

Contributors

bpoweski avatar rcullito avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

Forkers

rcullito

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.