• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parse the xml

 
Gayathri kopparapu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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">
<Response>
<MetaInfo>
<Timestamp>2013-12-11T14:09:57.901Z</Timestamp>
</MetaInfo>
<View xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:SearchResultsViewType">
<ViewId>0</ViewId>
<Result>
<Relevance>1.0</Relevance>
<Distance>10366.8</Distance>
<MatchLevel>city</MatchLevel>
<MatchQuality>
<Country>1.0</Country>
<State>1.0</State>
<County>1.0</County>
<City>1.0</City>
<PostalCode>1.0</PostalCode>
</MatchQuality>
<Location>
<LocationId>AREA_20448374</LocationId>
<LocationType>point</LocationType>
<DisplayPosition>
<Latitude>60.20678</Latitude>
<Longitude>24.65578</Longitude>
</DisplayPosition>
<MapView>
<TopLeft>
<Latitude>60.3625</Latitude>
<Longitude>24.49933</Longitude>
</TopLeft>
<BottomRight>
<Latitude>60.05958</Latitude>
<Longitude>24.87</Longitude>
</BottomRight>
</MapView>
<Address>
<Label>Espoo, Etelä-Suomi, Suomi</Label>
<Country>FIN</Country>
<State>Etelä-Suomi</State>
<County>Uusimaa</County>
<City>Espoo</City>
<PostalCode>02770</PostalCode>
<AdditionalData key="CountryName">Suomi</AdditionalData>
<AdditionalData key="StateName">Etelä-Suomi</AdditionalData>
</Address>
<MapReference>
<MapVersion>Q2/2013</MapVersion>
<CountryId>20241487</CountryId>
<StateId>20422913</StateId>
<CountyId>20422914</CountyId>
<CityId>20448374</CityId>
</MapReference>
</Location>
</Result>
</View>
</Response>
</ns2:Search>
 
Sean Corfield
Ranch Hand
Posts: 305
11
Clojure Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

https://github.com/kyleburton/clj-xpath

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
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
11
Clojure Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The result of (slurp "stuff.xml") is a string so just put your variable there instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic