Created
March 12, 2012 12:02
-
-
Save godfat/2021408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
require 'time' | |
module Plist | |
module_function | |
def parse xml | |
fold_dict(Nokogiri::XML.parse(xml).root.xpath('./dict')) | |
end | |
def fold_dict dict | |
dict.xpath('./key').inject({}){ |r, key| | |
r[key.inner_text] = fold(key.next_element) | |
r | |
} | |
end | |
def fold_array array | |
array.children.map{ |child| fold(child) }.compact | |
end | |
def fold element | |
case element.name | |
when 'dict' ; fold_dict( element) | |
when 'array' ; fold_array(element) | |
when 'integer'; element.inner_text.to_i | |
when 'real' ; element.inner_text.to_f | |
when 'date' ; Time.parse(element.inner_text) | |
when 'false' ; false | |
when 'true' ; true | |
when 'string' ; element.inner_text | |
when 'data' ; element.inner_text | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment