开发者

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-soft开发者_如何学JAVAware.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


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}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜