Skip to content

Instantly share code, notes, and snippets.

Created December 11, 2011 21:58
Show Gist options
  • Save anonymous/1463002 to your computer and use it in GitHub Desktop.
Save anonymous/1463002 to your computer and use it in GitHub Desktop.
require "rexml/document"
require "rubygems"
require "builder"
customers = []
items = []
File.open("orders.xml", "r") do |file|
doc = REXML::Document.new(file)
doc.root.elements.each do |order|
order.elements.each do |element|
if element.name == "address"
customers << element.elements["name"].text
customers << element.elements["street"].text
customers << element.elements["city"].text
customers << element.elements["state"].text
customers << element.elements["zip"].text
customers << element.elements["country"].text
elsif element.name == "items"
element.elements["item"].each do |item|
items << item
end
end
end
end
end
p customers
p items
Výpis:
["Ellen Adams", "123 Maple street", "Mill Valley", "CA", "10999", "USA", "Tai Yee", "8 Oak Avenue", "Old Town", "PA", "95819", "USA", "Cristian Osorio", "456 Main street", "Buffalo", "NY", "98112", "USA", "Cristian Osorio", "456 Main street", "Buffalo", "NY", "98112", "USA", "Jessica Arnold", "4055 Madison Ave", "Seattle", "WA", "98112", "USA", "Jessica Arnold", "4055 Madison Ave", "Buffalo", "NY", "98112", "USA"]
["\n ", <name> ... </>, "\n ", <quantity> ... </>, "\n ", <price> ... </>, "\n ", "\n ", <name> ... </>, "\n ", <quantity> ... </>, "\n ", <price> ... </>, "\n ", "\n ", <name> ... </>, "\n ", <quantity> ... </>, "\n ", <price> ... </>, "\n "]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment