Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. def self.read_from_xml file_name
  2. file_path = File.dirname __FILE__ + "/" + file_name
  3.  
  4. unless File.exist? file_path
  5. abort puts "Файл #{file_name} - не найден."
  6. end
  7.  
  8. file = File.new file_path, "r:UTF-8"
  9. doc = REXML::Document.new file
  10. file.close
  11.  
  12. res = []
  13. item = nil
  14.  
  15. doc.elements.each("products/product")do |product_node|
  16. it_price = product_node.attributes["price"].to_i
  17. it_store = product_node.attributes["store"].to_i
  18.  
  19. product_node.elements.each("book")do |book_node|
  20. item = Book.new it_price, it_store
  21. item.update(book_node.attributes["title"],
  22. book_node.attributes["author"])
  23. res << item
  24. end
  25.  
  26. product_node.elements.each("disk")do |disk_node|
  27. item = Disk.new it_price, it_store
  28.  
  29. item.update(disk_node.attributes["title"],
  30. disk_node.attributes["author"],
  31. disk_node.attributes["jendre"])
  32. res << item
  33. end
  34.  
  35. product_node.elements.each("movie")do |movie_node|
  36. item = Movie.new it_price, it_store
  37.  
  38. item.update(
  39. movie_node.attributes["title"],
  40. movie_node.attributes["author"],
  41. movie_node.attributes["release"]
  42. )
  43. res << item
  44. end
  45. end
  46. return res
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement