How do I Transform an .xml file to an instance of a ruby array?
I have the following xml file:
/my_file.xml
<?xml version="1.0" encoding="utf-8" ?>
<words>
<w>my_word</w>
<w>second_word</w>
</words>
How can I do the following using Ruby:
- Load
- 开发者_JAVA百科Parse
- Transform an xml file to an instance of a ruby array:
words = ["my_word","second_word"]
With the Nokogiri gem...
require 'rubygems'
require 'nokogiri'
xml = '<?xml version="1.0" encoding="utf-8" ?>
<words>
<w>my_word</w>
<w>second_word</w>
</words>'
doc = Nokogiri::XML(xml)
words = doc.xpath("//w").map {|x| x.text}
精彩评论