DZone 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

Rafael has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

Ruby XML I18n File Parser

05.22.2009
| 3979 views |
  • submit to reddit
        // Ruby script to help parse a XML file

require 'rexml/document'
include REXML
englishFile = File.new('englishFileName', 'w+')
spanishFile = File.new('spanishFileName', 'w+')
portugueseFile = File.new('portugueseFileName', 'w+')
errorMessagesFile = File.new("errorMessages.xml")
document = Document.new(file)
root = document.root
root.each_element("Property") do |propertyTag|
  id = propertyTag.attributes['id']   
  propertyTag.each_element("element") do |elementTag|
	  elementAttr = elementTag.attributes['otherTag']
	  error = elementTag.text == nil ? "" : "#{id} = #{elementTag.text}\n"
	  if elementAttr = "pt"
		  portugueseFile << error
	  elsif elementAttr == "es"
		  spanishFile << error
	  else 
		  portugueseFile << error
	  end
  end
end
errorMessagesFile.close()
englishFile.close()
spanishFile.close()
portugueseFile.close()