wood burning stoves 2.0*
The moose likes Clojure and the fly likes Parse the xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Languages » Clojure
Bookmark "Parse the xml" Watch "Parse the xml" New topic

Parse the xml

Gayathri kopparapu

Joined: Nov 07, 2013
Posts: 11
Hi ,

can anyone tell how to parse the below xml and get the city details(Espoo)................

<ns2:Search xmlns:ns2="http://www.navteq.com/lbsp/Search-Search/4">
<View xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:SearchResultsViewType">
<Label>Espoo, Etelä-Suomi, Suomi</Label>
<AdditionalData key="CountryName">Suomi</AdditionalData>
<AdditionalData key="StateName">Etelä-Suomi</AdditionalData>
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 259

There are a number of Clojure libraries that can parse XML into a data structure but your choice will depend on what you really need to do with the XML.

If you really want the entire structure of the XML as a Clojure data structure, you might look at data.xml (contrib library) or perhaps Enlive (templating & parsing engine) but I suspect you'll find those fairly complex to work with.

If you just want to extract specific parts of the XML, you probably just want something like XPath. You can use XPath directly from Clojure but, like most Java libraries, it's a bit of a pain to use. There is a Clojure wrapper for XPath that is a lot more pleasant to use:


I put your XML in a file slurp.xml and did the following:

That takes advantage of the excellent lein-try plugin so that you can quickly fire up a REPL with various libraries loaded, without needing to create a project.clj file etc.

Without lein-try you would need to create a project (lein new xpathtest; cd xpathtest) and edit project.clj to include the dependency:

and then start a REPL with lein repl.
Gayathri kopparapu

Joined: Nov 07, 2013
Posts: 11
Hi Corfield ,

Is there any other way to parse the above response as a string.I dont want to use the response as "Stuff.xml" instead i need to parse that as a string.

Your inputs are really needed.
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 259

The result of (slurp "stuff.xml") is a string so just put your variable there instead.
I agree. Here's the link: http://aspose.com/file-tools
subject: Parse the xml