将XML集合(Pivotal Tracker故事)转换为Ruby散列/对象

mla*_*bie 2 ruby xml hash nokogiri pivotaltracker

我有一个XML格式的故事集.我想解析文件并将每个故事作为散列或Ruby对象返回,以便我可以进一步操作Ruby脚本中的数据.

Nokogiri是否支持此功能,还是有更好的工具/库可供使用?

XML文档具有以下结构,通过Pivotal Tracker的Web API返回:

<?xml version="1.0" encoding="UTF-8"?>
<stories type="array" count="145" total="145">
  <story>
    <id type="integer">16376</id>
    <story_type>feature</story_type>
    <url>http://www.pivotaltracker.com/story/show/16376</url>
    <estimate type="integer">2</estimate>
    <current_state>accepted</current_state>
    <description>A description</description>
    <name>Receivable index listing will allow selection viewing</name>
    <requested_by>Tony Superman</requested_by>
    <owned_by>Tony Superman</owned_by>
    <created_at type="datetime">2009/11/04 15:49:43 WST</created_at>
    <accepted_at type="datetime">2009/11/10 11:06:16 WST</accepted_at>
    <labels>index ui,receivables</labels>
  </story>
  <story>
    <id type="integer">17427</id>
    <story_type>feature</story_type>
    <url>http://www.pivotaltracker.com/story/show/17427</url>
    <estimate type="integer">3</estimate>
    <current_state>unscheduled</current_state>
    <description></description>
    <name>Validations in wizards based on direction</name>
    <requested_by>Matthew McBoggle</requested_by>
    <created_at type="datetime">2009/11/17 15:52:06 WST</created_at>
  </story>
  <story>
    <id type="integer">17426</id>
    <story_type>feature</story_type>
    <url>http://www.pivotaltracker.com/story/show/17426</url>
    <estimate type="integer">2</estimate>
    <current_state>unscheduled</current_state>
    <description>Manual payment needs a description field.</description>
    <name>Add description to manual payment</name>
    <requested_by>Tony Superman</requested_by>
    <created_at type="datetime">2009/11/17 15:10:41 WST</created_at>
    <labels>payment process</labels>
  </story>
  <story>
    <id type="integer">17636</id>
    <story_type>feature</story_type>
    <url>http://www.pivotaltracker.com/story/show/17636</url>
    <estimate type="integer">3</estimate>
    <current_state>unscheduled</current_state>
    <description>The SMS and email templates needs to be editable by merchants.</description>
    <name>Notifications are editable by the merchant</name>
    <requested_by>Matthew McBoggle</requested_by>
    <created_at type="datetime">2009/11/19 16:44:08 WST</created_at>
  </story>
</stories>
Run Code Online (Sandbox Code Playgroud)

Nic*_*s C 5

您可以利用ActiveSupport中的Hash扩展.然后,您只需要在Nokogiri中解析文档,然后将节点集结果转换​​为哈希值.此方法将保留属性类型(例如整数,日期,数组).(当然,如果你使用Rails,你不必在你的环境中拥有/包括主动支持或nokogiri.我在这里假设一个纯Ruby实现.)

require 'rubygems'
require 'nokogiri'
require 'activesupport'

include ActiveSupport::CoreExtensions::Hash

doc = Nokogiri::XML.parse(File.read('yourdoc.xml'))
my_hash = doc.search('//story').map{ |e| Hash.from_xml(e.to_xml)['story'] }
Run Code Online (Sandbox Code Playgroud)

这将生成一个哈希数组(每个故事节点一个),并根据属性保留键入,如下所示:

my_hash.first['name']
=> "Receivable index listing will allow selection viewing"

my_hash.first['id']
=> 16376

my_hash.first['id'].class
=> Fixnum

my_hash.first['created_at'].class
=> Time
Run Code Online (Sandbox Code Playgroud)