简体   繁体   English

如何在 ruby​​ 中将 XML 转换为 Hash?

[英]How to convert XML to Hash in ruby?

I have a XML code which I want to convert into Hash我有一个 XML 代码,我想将其转换为 Hash

   <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>

I want to convert this xml into Hash .我想将此 xml 转换为 Hash 。 I try to find functions which convert this xml to h=Hash.new How I do this?我试图找到将这个 xml 转换为h=Hash.new函数我该怎么做?

There is ActiveSupport's Hash#from_xml method that you can use:您可以使用 ActiveSupport 的Hash#from_xml方法:

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

If you are using Rails you can use the answer provided above, otherwise you can require the ActiveSuppport gem:如果您使用的是 Rails,您可以使用上面提供的答案,否则您可能需要ActiveSuppport gem:

require 'active_support/core_ext/hash'

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

Note this will only work with valid xml.请注意,这仅适用于有效的 xml。 See comments on op.请参阅对 op 的评论。 Also note that using element attributes like id="1" won't convert back the same way for example:另请注意,使用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>

Use nokogiri to parse XML response to ruby hash.使用 nokogiri 将 XML 响应解析为 ruby​​ 哈希。 It's pretty fast.这是相当快的。

require 'active_support/core_ext/hash'  #from_xml 
require 'nokogiri'

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM