将xml转换为本机Ruby数据结构

我正在从像这样返回xml的api中获取数据:

<?xml version="1.0" encoding="utf-8" ?> <seriess realtime_start="2013-01-28" realtime_end="2013-01-28"> <series id="GDPC1" realtime_start="2013-01-28" realtime_end="2013-01-28" title="Real Gross Domestic Product,1 Decimal" observation_start="1947-01-01" observation_end="2012-07-01" frequency="Quarterly" frequency_short="Q" units="Billions of Chained 2005 Dollars" units_short="Bil. of Chn. 2005 $" seasonal_adjustment="Seasonally Adjusted Annual Rate" seasonal_adjustment_short="SAAR" last_updated="2012-12-20 08:16:28-06" popularity="93" notes="Real gross domestic product is the inflation adjusted value of the goods and services produced by labor and property located in the United States. For more information see the Guide to the National Income and Product Accounts of the United States (NIPA) - (http://www.bea.gov/national/pdf/nipaguid.pdf)"/> </seriess>

我是反序列化的新手,但我认为合适的是将这个xml解析成一个ruby对象,然后我可以引用像objectFoo.seriess.series.frequency那样返回’Quarterly’.

从我在这里和谷歌的搜索中,似乎没有一个明显的解决方案,这在Ruby(NOT rails),这让我觉得我错过了一些相当明显的东西.有任何想法吗?

编辑
我根据Winfield的建议设置了一个测试用例.

class Exopenstruct

  require 'ostruct'

  def initialize()  

  hash = {"seriess"=>{"realtime_start"=>"2013-02-01","realtime_end"=>"2013-02-01","series"=>{"id"=>"GDPC1","realtime_start"=>"2013-02-01","title"=>"Real Gross Domestic Product,1 Decimal","observation_start"=>"1947-01-01","observation_end"=>"2012-10-01","frequency"=>"Quarterly","frequency_short"=>"Q","units"=>"Billions of Chained 2005 Dollars","units_short"=>"Bil. of Chn. 2005 $","seasonal_adjustment"=>"Seasonally Adjusted Annual Rate","seasonal_adjustment_short"=>"SAAR","last_updated"=>"2013-01-30 07:46:54-06","popularity"=>"93","notes"=>"Real gross domestic product is the inflation adjusted value of the goods and services produced by labor and property located in the United States.\n\nFor more information see the Guide to the National Income and Product Accounts of the United States (NIPA) - (http://www.bea.gov/national/pdf/nipaguid.pdf)"}}}

  object_instance = OpenStruct.new( hash )

  end
end

在irb中我加载了rb文件并实例化了该类.但是,当我尝试访问一个属性(例如instance.seriess)时,我收到了:NoMethodError:undefined method`seriess’

如果我遗漏了一些明显的东西,再次道歉.

解决方法

您可能最好使用标准XML进行散列解析,例如Rails中包含的:

object_hash = Hash.from_xml(xml_string)
puts object_hash['seriess']

如果您不使用Rails堆栈,则可以使用像Nokogiri这样的库来实现相同的行为.

编辑:如果您正在寻找对象行为,使用OpenStruct是一个很好的方式来包装哈希:

object_instance = OpenStruct.new( Hash.from_xml(xml_string) )
puts object_instance.seriess

注意:对于深度嵌套的数据,您可能还需要以递归方式将嵌入的哈希值转换为OpenStruct实例. IE:如果上面的属性是值的哈希值,则它将是哈希值而不是OpenStruct.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇