clojure.contrib.lazy-xml の parse-seq で要素の内容を取り出す

clojure.contrib.lazy-xmlparse-seq を使って、XML のテキストのみを取り出してみる。

lazy-xml API reference (clojure-contrib)

『プログラミング Clojure』の code/examples/sequences/compositions.xml を parse-seq でパースしてみる。

(use '[clojure.contrib.lazy-xml :only (parse-seq)])
(def uri "./code/examples/sequences/compositions.xml")

実行結果

user=> (parse-seq uri)
({:type :start-element, :name :compositions, :attrs {}, :str nil} {:type :start-element, :name :composition, :attrs {:composer "J. S. Bach"}, :str nil} {:type :start-element, :name :name, :attrs {}, :str nil} {:type :characters, :name nil, :attrs nil, :str "The Art of the Fugue"} {:type :end-element, :name :name, :attrs nil, :str nil} {:type :end-element, :name :composition, :attrs nil, :str nil} {:type :start-element, :name :composition, :attrs {:composer "J. S. Bach"}, :str nil} {:type :start-element, :name :name, :attrs {}, :str nil} {:type :characters, :name nil, :attrs nil, :str "Musical Offering"} {:type :end-element, :name :name, :attrs nil, :str nil} {:type :end-element, :name :composition, :attrs nil, :str nil} {:type :start-element, :name :composition, :attrs {:composer "W. A. Mozart"}, :str nil} {:type :start-element, :name :name, :attrs {}, :str nil} {:type :characters, :name nil, :attrs nil, :str "Requiem"} {:type :end-element, :name :name, :attrs nil, :str nil} {:type :end-element, :name :composition, :attrs nil, :str nil} {:type :end-element, :name :compositions, :attrs nil, :str nil})

このシーケンスから、:type:characters である :str を抽出する。

(for [src (parse-seq uri)
      :when (= :characters (:type src))]
  (:str src))

実行結果

user=> (for [src (parse-seq uri)
             :when (= :characters (:type src))]
         (:str src))
("The Art of the Fugue" "Musical Offering" "Requiem")
プログラミングClojure
プログラミングClojure

posted with amazlet at 10.05.19
Stuart Halloway
オーム社
売り上げランキング: 8244
«
»