gpt4 book ai didi

xml - 将 XML 集合(Pivotal Tracker 故事)转换为 Ruby 哈希/对象

转载 作者:数据小太阳 更新时间:2023-10-29 02:49:31 24 4
gpt4 key购买 nike

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

是否Nokogiri支持这个,还是有更好的工具/库可以使用?

XML 文档具有以下结构,通过 Pivotal Tracker's 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>

最佳答案

您可以利用 ActiveSupport 中的哈希扩展。然后你只需要在 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'] }

这将生成一个哈希数组(每个故事节点一个),并保留基于属性的类型,如下所示:

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

关于xml - 将 XML 集合(Pivotal Tracker 故事)转换为 Ruby 哈希/对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1780879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com