如何在Ruby中将XML转换为哈希表?

8

我有一个XML代码,想要将其转换为哈希表

   <meta_description><language id="1"></language><language id="2"></language></meta_description>
    <meta_keywords><language id="1"></language><language id="2"></language></meta_keywords>
    <meta_title><language id="1"></language><language id="2" ></language></meta_title>
    <link_rewrite><language id="1" >konsult-500-krtim</language><language id="2" >konsult-500-krtim</language></link_rewrite>
    <name><language id="1" >Konsult 500 kr/tim</language><language id="2" >Konsult 500 kr/tim</language></name>
    <description><language id="1" ></language><language id="2" ></language></description>
    <description_short><language id="1" ></language><language id="2" ></language></description_short>
    <available_now><language id="1" ></language><language id="2" ></language></available_now>
    <available_later><language id="1" ></language><language id="2" ></language></available_later>
    <associations>
    <categories nodeType="category" api="categories">
    <category>
    <id>2</id>
    </category>
    </categories>
    <images nodeType="image" api="images"/>
    <combinations nodeType="combination" api="combinations"/>
    <product_option_values nodeType="product_option_value" api="product_option_values"/>
    <product_features nodeType="product_feature" api="product_features"/>
    <tags nodeType="tag" api="tags"/>
    <stock_availables nodeType="stock_available" api="stock_availables">
    <stock_available>
    <id>111</id>
    <id_product_attribute>0</id_product_attribute>
    </stock_available>
    </stock_availables>
    <accessories nodeType="product" api="products"/>
    <product_bundle nodeType="product" api="products"/>
    </associations>

我想将这个XML转换为哈希表。 我尝试寻找将此XML转换为h=Hash.new的函数。 我该如何做到这一点?


首先,任何下面的答案都需要有效的HTML才能工作。这个XML有错误,您可以使用这样的工具进行验证。 - lacostenycoder
这是您示例的一个变体,移除了错误xml - lacostenycoder
3个回答

18

你可以使用ActiveSupport的Hash#from_xml方法:

xml = File.open("data.xml").read # if your xml is in the 'data.xml' file
Hash.from_xml(xml)

7
如果你在使用Rails框架,这段代码可以正常工作,因为它对Hash类进行了猴子补丁。但是在纯粹的Ruby环境中,它不能直接运行。 - lacostenycoder

10

如果您使用的是Rails,可以使用上面提供的答案,否则您可以要求ActiveSupport gem:

require 'active_support/core_ext/hash'

xml = '<foo>bar</foo>'
hash = Hash.from_xml(xml)
=>{"foo"=>"bar"}

请注意,这仅适用于有效的xml。请查看op上的评论。 还要注意,使用元素属性,如id="1",将无法以相同的方式转换回去,例如:
xml = %q(
  <root>
    <foo id="1"></foo>
    <bar id="2"></bar>
  </root>).strip

hash = Hash.from(xml)
=>{"root"=>{"foo"=>{"id"=>"1"}, "bar"=>{"id"=>"2"}}}

puts hash.to_xml
# will output

<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <root>
    <foo>
      <id>1</id>
    </foo>
    <bar>
      <id>2</id>
    </bar>
  </root>
</hash>

-3
使用Nokogiri将XML响应解析为Ruby哈希表。它非常快速。
require 'active_support/core_ext/hash'  #from_xml 
require 'nokogiri'

doc = Nokogiri::XML(response_body)
Hash.from_xml(doc.to_s)

如果 respons_body 是 XML 类型,则为什么需要 Nokogiri?例如 Hash.from_xml(HTTParty.get('https://www.w3schools.com/xml/note.xml').body) 可以正常工作。是的,我使用了 HTTParty 作为示例,但它应该说明问题。 - lacostenycoder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接