Nokogiri parsing

前端 未结 1 1788
感情败类
感情败类 2021-01-26 02:55

I have some XML:

xml = <<-EOT

    
        Sport
        

        
相关标签:
1条回答
  • 2021-01-26 03:31
    require 'nokogiri'
    
    doc = Nokogiri::XML.parse <<-EOT
    <xml>
        <advcampaign_categories>
            <category id="85">Sport</category>
            <category id="79">Mobile</category>
            <category id="62">Flowers</category>
        </advcampaign_categories>
    </xml>
    EOT
    
    # if you are >= 2.1
    doc.css('category').map { |node| [node.text, node['id'].to_i] }.to_h
    # => {"Sport"=>85, "Mobile"=>79, "Flowers"=>62}
    # if you are below version < 2.1
    Hash[doc.css('category').map { |node| [node.text, node['id'].to_i] }]
    # => {"Sport"=>85, "Mobile"=>79, "Flowers"=>62}
    
    0 讨论(0)
提交回复
热议问题