简体   繁体   中英

How to convert XML to Hash in ruby?

I have a XML code which I want to convert into 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 . I try to find functions which convert this xml to h=Hash.new How I do this?

There is ActiveSupport's Hash#from_xml method that you can use:

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:

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. See comments on op. Also note that using element attributes like id="1" won't convert back the same way for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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