I can’t figure out what an InputStream is in the context of the clojure-xml documentation. The clojure-xml documentation lists the inputs to clojure-xml/parse as a “File, InputStream, or String naming a URL”.
I tried:
(defn open-file
"Attempts to open a file and complains if the file is not present."
[file-name]
(let [file-data (try
(slurp file-name)
(catch Exception e))]
file-data))
(clojure-xml/parse (utl/open-file "test.xml"))
and receive this error:
FileNotFoundException /home/cnorton/projects/clojure/xml-lib/<
(No such file or directory) java.io.FileInputStream.open
(FileInputStream.java:-2)
but this works:
(clojure-xml/parse "test.xml")
Why wouldn’t an InputStream be considered the result of opening a file? Therefore, what is an InputStream in this context?
InputStream refers to a java.io.InputStream. The easiest way to get one in Clojure is by using clojure.java.io/input-stream.
clojure.xml/parse should accept anything that clojure.java.io/input-stream would. Examples:
The difference between “/Users/bsmith/.m2/settings.xml” and a “File name”:
“/Users/bsmith/.m2/settings.xml” is a String which (happens) to specify an (absolute) path to a file on my file system. In the Java world, however, the class java.io.File is the idiomatic way to represent a file path. clojure.java.io does’t care, it will accept a String naming a file or a java.io.File naming a file.
The error message you got in your initial post came because you first loaded the XML into a String using slurp. (Not a good idea since XML carries its own encoding with it, which slurp is not able to interpret.) In any event, you then passed the String on and clojure.java.io ultimately tried to interpret the actual content of the XML as a file path, which clearly can’t work.