Clojure类库
我们已经看到了我们早期的示例中使用的许多库用于web测试,web开发,开发基于swing的应用程序,用于连接到MySQL数据库的jdbc库。以下只是几个更多图书馆的例子。
data.xml中
这个库允许使用Clojure使用XML数据。要使用的库版本是org.clojure / data.xml“ 0.0.8”。data.xml支持解析和发出XML。解析函数读取器或InputStream读取XML。
例
下面是data.xml的一个例子。
(ns clojure.examples.example
   (use 'clojure.data.xml)
   (:gen-class))
(defn Example []
   (let [input-xml (java.io.StringReader. "<?xml version = "1.0"
      encoding = "UTF-8"?><example><clo><Tutorial>The Tutorial
      value</Tutorial></clo></example>")]
      (parse input-xml)))
#clojure.data.xml.Element{
   :tag :example, :attrs {}, :content (#clojure.data.xml.Element {
      :tag :clo, :attrs {}, :content (#clojure.data.xml.Element {
         :tag :Tutorial, :attrs {},:content ("The Tutorial value")})})}
(Example)
data.json
这个库允许Clojure使用JSON数据。要使用的库版本是org.clojure / data.json“ 0.2.6”。
例
下面是data.json的一个例子。
(ns clojure.examples.example
   (:require [clojure.data.json :as json])
   (:gen-class))
(defn Example []
   (println (json/write-str {:a 1 :b 2})))
(Example)
上面的示例输出以下结果:
{"a":1,"b":2}
data.csv
这个库允许Clojure使用'csv'数据。要使用的库版本是org.clojure / data.csv“ 0.1.3”。
例
下面是data.csv的一个例子。
(ns clojure.examples.example
   (require '[clojure.data.csv :as csv]
      '[clojure.java.io :as io])
   (:gen-class))
(defn Example []
   (with-open [in-file (io/reader "in-file.csv")]
      (doall
      (csv/read-csv in-file)))
   (with-open [out-file (io/writer "out-file.csv")]
   (csv/write-csv out-file
      [[":A" "a"]
      [":B" "b"]])))
(Example)
在上面的代码中,'csv'函数将首先重新导入一个文件中。csv的文件,将所有数据放入变量中。接着,我们使用write-csv函数将所有数据写入一个叫做out-file.csv的文件。