1

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

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170

2 Answers2

1

I am guessing that you gave up on this question a long time ago, but your problem was that there was no attribute 'field' for your 'row' elements. However, your 'field' elements do have 'name' attributes.

Try this:

doc.elements.each("//row/field") do {|e| puts e.attributes["name"] + ' : ' + e.text}

Doug
  • 563
  • 4
  • 10
-1

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

chrispanda
  • 3,204
  • 1
  • 21
  • 23