xmlから取得したデータを用いて、HiccupでHTML出力メモ
Hiccup を利用してxmlから取得したデータを用いたHTML生成のメモ。
(ns xmlhello.core (:require [clojure.xml :as xml] [hiccup.core :as hic])) (defn load-xml-seq [filepath] (xml-seq (xml/parse (java.io.File. filepath)))) ; Hiccup を用いて、xmlから取得したデータをHTMLのリスト形式で出力するサンプル (defn html-sample [] (let [xmlseq (load-xml-seq "data.xml")] (hic/html [:html [:head [:meta {:http-equiv "content-type" :content "text/html;charset=utf-8"}] [:title "Hello"]] [:body [:h1 "Hello World"] [:ul (for [x xmlseq :when (= :record (:tag x))] [:li (flatten (:content x))])]]])))
実行
(use :reload 'xmlhello.core) (html-sample) ; -> "<html><head><meta content=\"text/html;charset=utf-8\" http-equiv=\"content-type\" /><title>Hello</title></head><body><h1>Hello World</h1><ul><li>record1</li><li>record2</li></ul></body></html>"
補足
なお、Hiccup-bridgeを用いると、HTML表現からHiccup表現への変換ができるよう。