将Clojure映射转换为XML

时间:2019-05-06 14:25:03

标签: xml clojure

我正在尝试找到一种简单的解决方案,将Clojure数据结构转换为XML。

Clojure hash-map to xml不起作用。

我用过

data.xml

它可以为我创建解决方案吗?

2 个答案:

答案 0 :(得分:0)

以下是使用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)) 

结果:

<?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,使用clojure.data.xmltupelo.parse.xmltupelo.parse.tagsoup将XML解析为Clojure数据结构(实时格式)。

答案 1 :(得分:0)

这正是您所需要的:

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