ジッパーを使用して石鹸データを解析するコードがあります。フォーマットすると、期待どおりに機能します
(defn parse-data
[raw-data]
(let [soap-data (:body raw-data)
soap-envelope (zip/xml-zip
(xml/parse
(java.io.ByteArrayInputStream. (.getBytes (str soap-data) "UTF-8"))))]
(pprint (xml-z/xml-> soap-envelope :soap:Envelope :soap:Body :Tag1 :Tag2))))
期待される結果を出力します。しかし、XML 解析を let ステートメントに移動すると、コンパイルに失敗し、次のエラーが発生します (ブラケットが一致することを 3 回確認しました)。
RuntimeException EOF while reading, starting at line 3 clojure.lang.Util.runtimeException (Util.java:221)
jcode.oc-drift.aquisition=> (pprint result-data)))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: result-data in this context, compiling:(/tmp/form-init6714472131112461091.clj:1:1)
RuntimeException Unmatched delimiter: ) clojure.lang.Util.runtimeException (Util.java:221)
RuntimeException Unmatched delimiter: ) clojure.lang.Util.runtimeException (Util.java:221)
jcode.oc-drift.aquisition=>
次のように、let ステートメントに xml-z/xml-> 呼び出しを入れるようにコードが変更されました。
(defn parse-data
[raw-data]
(let [soap-data (:body raw-data)
soap-envelope (zip/xml-zip
(xml/parse
(java.io.ByteArrayInputStream. (.getBytes (str soap-data) "UTF-8"))))
result-data (xml-z/xml-> soap-envelope :soap:Envelope :soap:Body :GetNextTripsForStopResponse :GetNextTripsForStopResult)]
(pprint result-data)))
これは let フォームのバグですか、それとも xml-> 機能の動作に関する何かが欠けていますか?
機能しない関数呼び出しを含む完全なコード ファイル:
ソース/こんにちは/core.clj:
(ns hello.core
(:require [clj-http.client :as http-client]
[clojure.zip :as zip]
[clojure.xml :as xml]
[clojure.data.xml :as xml-data]
[clojure.data.zip.xml :as xml-z]))
(use 'clojure.pprint)
(def app-id "redacted")
(def api-key "redacted")
(def post-data {:apiKey api-key :appID app-id})
(defn get-data
[post-data function]
"function is a string with the api function to be called"
(let [url (str "redacted" function)]
(http-client/post url {:form-params post-data})))
(defn parse-data
[raw-data]
(let [soap-data (:body raw-data)
soap-envelope (zip/xml-zip
(xml/parse
(java.io.ByteArrayInputStream. (.getBytes (str soap-data) "UTF-8"))))
result-data (xml-z/xml-> soap-envelope :soap:Envelope :soap:Body :GetNextTripsForStopResponse :GetNextTripsForStopResult)]
(pprint result-data)))
project.clj:
(defproject hello "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:main hello.core
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/data.xml "0.0.8"]
[org.clojure/data.zip "0.1.2"]
[clj-http "2.2.0"]])