2016-10-15 1 views
0

私はハッシュにこのXMLを変換したいハッシュにXMLをRubyの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> 

を変換したいXMLコードを持っています。 このxmlをh=Hash.newに変換する関数を見つけようとしています どうすればいいですか?

答えて

-1

鋸山はあなたGemfilegem 'nokogiri'を追加し、ハッシュに書き込むことができ、あなたのXMLデータを解析するために鋸山使用することをバンドルしてXML/HTMLファイル

を解析するために使用できる強力な逸品です。

require 'nokogiri' 

# parse xml FILE: store your xml content on a file and read 
data = File.open("data.xml") { |f| Nokogiri::XML(f) } 

# parse STRING xml content 
data = Nokogiri::XML("<meta><language id="1">EN</language></meta>") 

あなたが使用することができますactivesupportののHash#from_xml方法があり、詳細な説明 parsing xml with nokogiri

1

のリンクをたどる:

xml = File.open("data.xml").read # if your xml is in the 'data.xml' file 
Hash.from_xml(xml) 
関連する問題