I am getting an exception parsing an XML file with clojure.data.xml
, because the stream is closing before the parsing is complete.
What I do not understand is why doall
is not forcing the evaluation of the XML data before with-open
closes it (as suggested by 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))))
Which throws the exception:
(file->xml "example.xml")
;-> XMLStreamException ParseError at [row,col]:[80,1926]
Message: Stream closed com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next
If I remove the with-open
wrapper, it returns the XML data as expected (so the file is legit though the reader is not guaranteed closed).
I see that (source xml/parse)
yields lazy results:
(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)))
so perhaps that is related, but the function I have is very similar to the "round-trip" example on the clojure.data.xml README.
What am I missing here?