Question

I have a leiningen project with [org.clojure/data.xml "0.0.7"] as a dependency and an xml file in data/data-sample.xml

That works:

(require '[clojure.xml :as xml])
(xml/parse "data/small-sample.xml")

It returns:

{:tag :data, :attrs nil, :content [{:tag :person, :attrs nil, :content [{:tag :given-name, :attrs nil, :content ["Gomez"]} {:tag :surname, :attrs nil, :content ["Addams"]} {:tag :relation, :attrs nil, :content ["father"]}]} {:tag :person, :attrs nil, :content [{:tag :given-name, :attrs nil, :content ["Morticia"]}...

That doesn't work:

(require '[clojure.data.xml :as data.xml])
(data.xml/parse "data/small-sample.xml")

It returns:

IllegalArgumentException No matching method found: createXMLStreamReader for class com.sun.xml.internal.stream.XMLInputFactoryImpl  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:80)

What am I doing wrong? Thanks.

Was it helpful?

Solution

As explained in its docstring, clojure.data.xml/parse accepts an InputStream or Reader, so that's what you need to provide:

(require '[clojure.data.xml :as xml]
         '[clojure.java.io :as io])

(xml/parse (io/reader "data/small-sample.xml"))

Note that io/reader attempts to treat strings as URIs as a first guess, then as local file names. You can use io/file if you want to be explicit about dealing with a file ((io/reader (io/file ...))). There's also io/resource for looking things up on the classpath.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top