简体   繁体   English

Ruby REXML 混淆

[英]Ruby REXML confusion

Hi I am trying to parse data out of an XML file I exported from mysql command line tool.嗨,我正在尝试从从 mysql 命令行工具导出的 XML 文件中解析数据。 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我正在关注位于此处的教程: http://www.germane-software.com/software/rexml/docs/tutorial.html以提取我想要的数据,这就是标签中的内容

XML FILE: XML 文件:

<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但是当我这样做时,我得到的所有实例都是 nil

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我会为此使用nokogiri - rexml 我总是发现这是一个问题,我的印象是它已经不再使用了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM