Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def self.read_from_xml file_name
- file_path = File.dirname __FILE__ + "/" + file_name
- unless File.exist? file_path
- abort puts "Файл #{file_name} - не найден."
- end
- file = File.new file_path, "r:UTF-8"
- doc = REXML::Document.new file
- file.close
- res = []
- item = nil
- doc.elements.each("products/product")do |product_node|
- it_price = product_node.attributes["price"].to_i
- it_store = product_node.attributes["store"].to_i
- product_node.elements.each("book")do |book_node|
- item = Book.new it_price, it_store
- item.update(book_node.attributes["title"],
- book_node.attributes["author"])
- res << item
- end
- product_node.elements.each("disk")do |disk_node|
- item = Disk.new it_price, it_store
- item.update(disk_node.attributes["title"],
- disk_node.attributes["author"],
- disk_node.attributes["jendre"])
- res << item
- end
- product_node.elements.each("movie")do |movie_node|
- item = Movie.new it_price, it_store
- item.update(
- movie_node.attributes["title"],
- movie_node.attributes["author"],
- movie_node.attributes["release"]
- )
- res << item
- end
- end
- return res
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement