Over a million developers have joined DZone.

Ruby XML I18n File Parser

·
// 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()
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}