-2

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?

Kevin
  • 2,258
  • 1
  • 32
  • 40
  • It’s unclear what you have tried based on your question, so very hard to give constructive help. clojure.data.xml works, in my experience. – pete23 May 06 '19 at 14:59
  • Please provide an example of your input data and the desired output, as well as what you tried. – Alan Thompson May 06 '19 at 16:34

2 Answers2

2

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.

Alan Thompson
  • 29,276
  • 6
  • 41
  • 48
  • I have been experimenting with the clojure.data.xml library in a Clojurescript project. It seems that some of the functions work, some do not in cljs. For example emit-str seems to work but indent-str does not - I. get the following error: _Use of undeclared Var clojure.data.xml/indent-str_ – chladni Jan 17 '21 at 20:10
1

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--))))