Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 573
user6874328 Avatar asked Oct 15 '16 12:10

user6874328


Video Answer


2 Answers

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)
like image 90
chumakoff Avatar answered Oct 20 '22 05:10

chumakoff


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>
like image 10
lacostenycoder Avatar answered Oct 20 '22 04:10

lacostenycoder