我有一些代码,可以使用拉链解析出soap数据。当我格式化它时,它就会像我所期望的那样工作
(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语句时,它无法编译,出现以下错误(我已经三次检查了我的括号是否匹配)
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=> 将代码更改为将xml-z/xml->调用放入let语句中,如下所示
(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表单的一个bug,还是我遗漏了一些关于xml->功能的行为?
包含非工作函数调用的完整代码文件:
src/hello/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"]])发布于 2016-08-17 05:27:45
您粘贴的代码是正确的,并且在堆栈跟踪中没有粘贴问题。
具体地说,该错误显示Unable to resolve symbol: result-data,从上面的代码行可以看出,它似乎在使用以下命令行之前关闭了表达式
(pprint result-data)))这听起来像是缓冲区中存在更高的语法错误的情况。例如,如果在程序中处于较高级别,则存在不平衡的[或(。
因为额外的开头paren会与这个匹配,并提早结束表达式。之后的两个错误增加了证据,使我认为它将`(pprint -data))数据解释为一个新表达式的开始,末尾有两个额外的括号。
尝试:
,,然后键入clear),然后重试。,,然后键入restart),然后重试。无论是在repl中运行还是在文件中求值,运行你的代码都是正确的(它们是完全相同的操作)
hello.core> (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))))
#'hello.core/parse-data
hello.core> (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)))
#'hello.core/parse-data所以这看起来真的像是一个环境问题。以下是更多需要检查的内容:
process
lean repl并粘贴行并加载文件时没有任何问题(反射警告除外)发布于 2016-08-18 00:22:19
你粘贴的代码正在为我工作。我认为问题一定与您使用的repl、编辑器/repl组合或其他什么东西有关。创建一个新项目并粘贴以下内容:
> lein new app hello然后编辑这两个文件:
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"]] )src/hello/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)))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, World!"))然后从命令行运行
> lein run
Hello, World!没有编译器错误(当然,没有测试数据,我们实际上也没有调用您的任何函数)。
看起来问题出在您的环境中的某个地方。如果你清理了所有的东西,重新开始一个新的项目,你应该会看到同样的结果。
https://stackoverflow.com/questions/38984519
复制相似问题