I am trying to find an easy solution to convert Clojure data structures to XML.
this Clojure hash-map to xml does not work.
I used
data.xml
can it create a solution for me?
I am trying to find an easy solution to convert Clojure data structures to XML.
this Clojure hash-map to xml does not work.
I used
data.xml
can it create a solution for me?
Here is an example of using clojure.data.xml
:
(ns tst.tupelo.parse.xml
(:use tupelo.core tupelo.test)
(:require
[clojure.data.xml :as clj-xml]))
(def enlive-tree-normalized-nonblank
{:tag :foo,
:attrs {},
:content [{:tag :name, :attrs {}, :content ["John"]}
{:tag :address, :attrs {}, :content ["1 hacker way"]}
{:tag :phone, :attrs {}, :content []}
{:tag :school,
:attrs {},
:content [{:tag :name, :attrs {}, :content ["Joe"]}
{:tag :state, :attrs {}, :content ["CA"]}
{:tag :type, :attrs {}, :content ["FOOBAR"]}]}
{:tag :college,
:attrs {},
:content [{:tag :name, :attrs {}, :content ["mit"]}
{:tag :address, :attrs {}, :content []}
{:tag :state, :attrs {}, :content ["Denial"]}]}]})
(println (clj-xml/indent-str enlive-tree-normalized-nonblank))
with result:
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<name>John</name>
<address>1 hacker way</address>
<phone/>
<school>
<name>Joe</name>
<state>CA</state>
<type>FOOBAR</type>
</school>
<college>
<name>mit</name>
<address/>
<state>Denial</state>
</college>
</foo>
Here are three examples of parsing XML into Clojure data structures (Enlive format) using clojure.data.xml
, tupelo.parse.xml
, and tupelo.parse.tagsoup
.
this is just what you need:
(:require [clojure.data.xml :as xml])
(xml/emit-str
(xml/element
:response {}
(map (fn make-node [[f s]]
(if (map? s)
(xml/element f {} (map make-node (seq s)))
(xml/element f {} s)))
(seq --your-map--))))