【发布时间】:2017-04-03 20:59:16
【问题描述】:
我在解析带有clojure.data.xml 的 XML 文件时遇到异常,因为在解析完成之前流正在关闭。
我不明白为什么doall 不强制在with-open 关闭XML 数据之前对其进行评估(如this related answer 所建议):
(:require [clojure.java.io :as io]
[clojure.data.xml :as xml])
(defn file->xml [path]
(with-open [rdr (-> path io/resource io/reader)]
(doall (xml/parse rdr))))
抛出异常:
(file->xml "example.xml")
;-> XMLStreamException ParseError at [row,col]:[80,1926]
Message: Stream closed com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next
如果我删除 with-open 包装器,它会按预期返回 XML 数据(因此文件是合法的,尽管不能保证阅读器已关闭)。
我看到 (source xml/parse) 产生惰性结果:
(defn parse
"Parses the source, which can be an
InputStream or Reader, and returns a lazy tree of Element records.
Accepts key pairs with XMLInputFactory options, see http://docs.oracle.com/javase/6/docs/api/javax/xml/stream/XMLInputFactory.html
and xml-input-factory-props for more information.
Defaults coalescing true."
[source & opts]
(event-tree (event-seq source opts)))
所以也许这是相关的,但我拥有的功能与clojure.data.xml README 上的“往返”示例非常相似。
我在这里错过了什么?
【问题讨论】:
标签: xml clojure xml-parsing