开发者

How do I Transform an .xml file to an instance of a ruby array?

开发者 https://www.devze.com 2023-03-30 08:57 出处:网络
I have the following xml file: /my_file.xml <?xml version=\"1.0\" encoding=\"utf-8\" ?> <words>

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

精彩评论

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

关注公众号