开发者

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:

  1. Load
  2. 开发者_JAVA百科Parse
  3. 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}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜