Search code examples
rubyxmlrexml

Ruby REXML confusion


Hi I am trying to parse data out of an XML file I exported from mysql command line tool. I am following a tutorial located here: http://www.germane-software.com/software/rexml/docs/tutorial.html to extract the data I want which is what is in the tags

XML FILE:

<resultset statement='select count(id) as &apos;Builds/Month&apos; , 
 CONCAT(MONT HNAME(submittime), &apos;-&apos;,  
 YEAR(submittime)) as &apos;Month-Year&apos;fr om builds group by 
 YEAR(submittime), MONTH(submittime)'>   
 <row>
    <field name='Builds/Month'>11</field>
    <field name='Month-Year'>May-2010</field>   
 </row>
 <row>
    <field name='Builds/Month'>38</field>
    <field name='Month-Year'>June-2010</field>   </row>

  <row>
    <field name='Builds/Month'>35</field>
    <field name='Month-Year'>July-2010</field>   
  </row>
  <row>
    <field name='Builds/Month'>51</field>
    <field name='Month-Year'>August-2010</field>  
  </row>
  <row>
    <field name='Builds/Month'>10</field>
    <field name='Month-Year'>September-2010</field> 
  </row>
    ....
  </resultset>

And here is what I am doing:

doc = Document.new(File.new("month.xml"))
doc.elements.each("//row") {|e| puts e.attributes["field"]}

But when I do this all i get is nil for every instance

Any help would be great. Thanks


Solution

  • I would be using nokogiri for this - rexml I always found to be a problem, and my impression is that it's falling out of use