XMLMapper構想
CSVMapperみたいな感じでXML(or HTML)をRubyのクラスにしたい。と思ってたらhappy mapperってのがすでにあって
http://happymapper.rubyforge.org/
class User include HappyMapper element :id, Integer element :name, String element :screen_name, String element :location, String element :description, String element :profile_image_url, String element :url, String element :protected, Boolean element :followers_count, Integer end class Status include HappyMapper element :id, Integer element :text, String element :created_at, Time element :source, String element :truncated, Boolean element :in_reply_to_status_id, Integer element :in_reply_to_user_id, Integer element :favorited, Boolean has_one :user, User end statuses = Status.parse(file_contents) statuses.each do |status| puts status.user.name, status.user.screen_name, status.text, status.source, '' end
これだと使いやすいなって感じがしなくて、俺がやりたいのは普通にxpath(or CSS3 Selector)でカラムと対応させたい。
class MyXML < XMLMapper column :name, String, '/path/to/element' column :location, String, 'body > h1' end xml = MyXML.load_file("http://example.com/test.xml") p xml.name p xml.location
みたいな感じで。