Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

List Edinburgh businesses grouped by category (See related posts)

Following on from fetching the details for every business in Edinburgh [dzone.com] this code groups those businesses by category.

require 'rexml/document'
include REXML

doc = Document.new(File.open('edinburgh_business.xml','r'))

raw_grps = doc.root.elements.to_a('records/listing/records/business').group_by do |b| 
  REXML::Text::unnormalize(b.elements['summary/category/text()'].to_s)
end

gbusiness = raw_grps.map do |category, businesses|
  summary = "<name>%s</name><business_no>%s</business_no>" % [category, businesses.length]  
  a = [summary, businesses.join("\n")]
  "<category><summary>%s</summary><records>%s</records></category>" % a
end

buffer = "<categories><summary/><records>%s</records></categories>" % gbusiness.join("\n")
File.open('business_categories_group_edinburgh.xml','w'){|f| Document.new(buffer).write f}


Here's a screenshot as it appears in the web browser [twitxr.com]

You need to create an account or log in to post comments to this site.


Click here to browse all 7207 code snippets

Related Posts