解析 XML 以使用 Nori 和 Nokogiri 进行哈希处理,结果不理想 [英] Parsing XML to hash with Nori and Nokogiri with undesired result

查看:76
本文介绍了解析 XML 以使用 Nori 和 Nokogiri 进行哈希处理,结果不理想的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Nori 将 XML 文档转换为 Ruby 哈希.但不是接收根元素的集合,而是返回一个包含该集合的新节点.这就是我正在做的:

I am attempting to convert an XML document to a Ruby hash using Nori. But instead of receiving a collection of the root element, a new node containing the collection is returned. This is what I am doing:

@xml  = content_for(:layout)
@hash = Nori.new(:parser => :nokogiri, :advanced_typecasting => false).parse(@xml)

@hash = Hash.from_xml(@xml)

@xml 的内容在哪里:

<bundles>
  <bundle>
    <id>6073</id>
    <name>Bundle-1</name>
    <status>1</status>
    <bundle_type>
      <id>6713</id>
      <name>BundleType-1</name>
    </bundle_type>
    <begin_at nil=\"true\"></begin_at>
    <end_at nil=\"true\"></end_at>
    <updated_at>2013-03-21T23:02:32Z</updated_at>
    <created_at>2013-03-21T23:02:32Z</created_at>
  </bundle>
  <bundle>
    <id>6074</id>
    <name>Bundle-2</name>
    <status>1</status>
    <bundle_type>
      <id>6714</id>
      <name>BundleType-2</name>
    </bundle_type>
    <begin_at nil=\"true\"></begin_at>
    <end_at nil=\"true\"></end_at>
    <updated_at>2013-03-21T23:02:32Z</updated_at>
    <created_at>2013-03-21T23:02:32Z</created_at>
  </bundle>
</bundles>

解析器返回格式的@hash:

{"bundles"=>{"bundle"=>[{"id"=>"6073", "name"=>"Bundle-1", "status"=>"1", "bundle_type"=>{"id"=>"6713", "name"=>"BundleType-1"}, "begin_at"=>nil, "end_at"=>nil, "updated_at"=>"2013-03-21T23:02:32Z", "created_at"=>"2013-03-21T23:02:32Z"}, {"id"=>"6074", "name"=>"Bundle-2", "status"=>"1", "bundle_type"=>{"id"=>"6714", "name"=>"BundleType-2"}, "begin_at"=>nil, "end_at"=>nil, "updated_at"=>"2013-03-21T23:02:32Z", "created_at"=>"2013-03-21T23:02:32Z"}]}} 

相反,我想得到:

{"bundles"=>[{"id"=>"6073", "name"=>"Bundle-1", "status"=>"1", "bundle_type"=>{"id"=>"6713", "name"=>"BundleType-1"}, "begin_at"=>nil, "end_at"=>nil, "updated_at"=>"2013-03-21T23:02:32Z", "created_at"=>"2013-03-21T23:02:32Z"}, {"id"=>"6074", "name"=>"Bundle-2", "status"=>"1", "bundle_type"=>{"id"=>"6714", "name"=>"BundleType-2"}, "begin_at"=>nil, "end_at"=>nil, "updated_at"=>"2013-03-21T23:02:32Z", "created_at"=>"2013-03-21T23:02:32Z"}]}

关键是我控制了 XML,它的形成方式与上述方式类似.

The point is that I control the XML, where it if formed similar to the way described above.

我的问题也与RABL的JSON有关吗输出不符合标准?可以吗?

推荐答案

想象一个 XML 只包含相同标签的列表,例如

Imagine an XML that consists only of a list of the same tags, e.g.

<shoppinglist>
    <item>apple</item>
    <item>banana</item>
    <item>cherry</item>
    <item>pear</item>
<shoppinglist>

当您将其转换为散列时,使用例如访问项目是非常简单的hash['shoppinglist']['item'][0].但是在这种情况下你会期待什么?只是一个数组?根据您的逻辑,现在应该可以使用 hash['shoppinglist'][0] 访问这些项目,但是如果容器内有不同的元素,例如

When you convert this into a hash, it is quite straightforward to access the items with e.g. hash['shoppinglist']['item'][0]. But what would you expect in this case? just an array? According to your logic, the items should now be accessible with hash['shoppinglist'][0] but what if you have different elements inside the container e.g.

<shoppinglist>
    <date>2013-01-01</date>
    <item>apple</item>
    <item>banana</item>
    <item>cherry</item>
    <item>pear</item>
<shoppinglist>

您现在将如何访问这些项目?以及如何约会?问题是在一般情况下必须转换为散列.

How would you now access the items? And how the date? The problem is that the conversion to a hash has to work in the general case.

虽然我不认识 Nori,但我很确定你从它那里问的东西并没有被烤熟,只是因为当你考虑一般情况时它没有意义.作为替代方案,您仍然可以自己将 bundle 数组提升一层:

Although i do not know Nori, i am pretty sure what you ask from it is not baked in, just because it makes no sense when you consider the general case. As an alternative, you can still get the bundle array up one level by yourself:

@hash['bundles'] = @hash['bundles']['bundle']

这篇关于解析 XML 以使用 Nori 和 Nokogiri 进行哈希处理,结果不理想的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆