简体   繁体   中英

Parse xml file with rails REXML?

How can i parse this xml file ?

<resultset>
−<row>
<field name="Table Name">table_1</field>
<field name="Quant of Rows">2</field>
<field name="Total Size Mb">0.02</field>
</row>
−<row>
<field name="Table Name">table_2</field>
<field name="Quant of Rows">1</field>
<field name="Total Size Mb">0.02</field>
</row>
</resultset>

I am using REXML.

    table_data = []
    xml = File.read('file1.xml')
    doc, posts = REXML::Document.new(xml), []
    doc.elements.each('resultset/row') do |table_row|
      table_data << {:table_name => table_row.elements[1].text,
        :record_count => table_row.elements[2].text,
        :size_in_mb => table_row.elements[3].text
      }
    end

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