17350


取得したい属性の表示はできたのですが，それらをcsv形式で保存するにはどうすればよいでしょうか？  
以下に，rubyとxmlのファイルを示します．

また，示したxmlファイルについてですが，要素は2つしかありませんが，この要素の数が増えても問題なく処理が可能でしょうか？

[code]

    <!-- language: lang-xml -->
    <?xml version="1.0" encoding="utf-8"?>
        <posts>
          <row Id="1" PostTypeId="1" CreationDate="2010-08-17T19:22:37.890" Score="12" ViewCount="14120" Body="&lt;p&gt;What is the hardware and software differences between Intel and PPC Macs?&lt;/p&gt;&#xA;" OwnerUserId="10" LastEditorUserId="15" LastEditDate="2010-09-08T15:12:04.097" LastActivityDate="2015-07-03T01:23:24.217" Title="What is the difference between Intel and PPC?" Tags="&lt;hardware&gt;&lt;mac&gt;&lt;powerpc&gt;&lt;intel&gt;" AnswerCount="8" CommentCount="0" FavoriteCount="2" />
          <row Id="2" PostTypeId="1" AcceptedAnswerId="130" CreationDate="2010-08-17T19:24:59.630" Score="6" ViewCount="3980" Body="&lt;p&gt;The VPN software I use for work (&lt;a href=&quot;http://www.lobotomo.com/products/IPSecuritas/&quot;&gt;IPSecuritas&lt;/a&gt;) requires me to turn off Back To My Mac to start it's connection, so I frequently turn off Back To My Mac in order to use my VPN connection (the program does this for me). I forget to turn it back on however and I'd love to know if there was something I could run (script, command) to turn it back on.&lt;/p&gt;&#xA;" OwnerUserId="17" LastEditorUserId="17" LastEditDate="2010-08-28T17:59:50.107" LastActivityDate="2013-07-10T14:19:12.677" Title="Turn on Back To My Mac via a Script or Command Line" Tags="&lt;osx&gt;&lt;mobileme&gt;&lt;terminal&gt;&lt;back-to-my-mac&gt;&lt;script&gt;" AnswerCount="1" CommentCount="0" FavoriteCount="4" />
          <row Id="7" PostTypeId="2" ParentId="5" CreationDate="2010-08-17T19:30:45.577" Score="5" Body="&lt;p&gt;I originally wanted to do this with my first Mac a couple years ago as well, since that's how my Linux and Windows environments behave.  But I think the driving force preventing this from becoming a reality is in how OS X handles application menus.&lt;/p&gt;&#xA;&#xA;&lt;p&gt;What if you want to go to the menu at the top of the screen for an application you're using, but in the process briefly hover over another application?  That would become infuriating quickly.&lt;/p&gt;&#xA;&#xA;&lt;p&gt;In short, I don't think its doable for that and potentially other reasons.&lt;/p&gt;&#xA;" OwnerUserId="41" LastActivityDate="2010-08-17T19:30:45.577" CommentCount="4" />
        </posts>
    
[/code]

と

[code]

    <!-- language: lang-ruby -->
    require 'rexml/document'
        require 'csv'
    
        doc = REXML::Document.new(open("ruby_csv.xml"))
    
    
        doc.elements.each('posts/row') do |element|
          puts element.attributes["Id"]
          puts element.attributes["PostTypeId"]
          puts element.attributes["AcceptedAnswerId"]
          puts element.attributes["Body"]
        end
    
[/code]

です．

（追記）  
rubyファイルを以下のように変更してcsv形式の保存を検討しています．  
["a","b","c"]の部分を指定した属性にしたいのですがどうすればよいでしょうか？

[code]

    <!-- language: lang-ruby -->
    require 'rexml/document'
        require 'csv'
    
        doc = REXML::Document.new(open("ruby_csv.xml"))
    
    
        doc.elements.each('posts/row') do |element|
          puts element.attributes["Id"]
          puts element.attributes["PostTypeId"]
          puts element.attributes["AcceptedAnswerId"]
          puts element.attributes["Body"]
        end
    
        #CSVファイルを作成
        CSV.open("posts.csv","w") do |csv|
         csv << ["a","b","c"]
         csv << ["d","e","f"]
         csv << ["g","h","i"]
        end
    
[/code]

よろしくお願いいたします．


CC BY-SA 3.0
2015-10-07T06:38:14.643
17344
2015-10-09T18:06:57.667
2015-10-09T18:06:57.667
754
9505
question
ruby xml csv
xmlファイル内の要素の属性をcsv形式で保存したい．使用言語はrubyです．


指定した属性値を row element ごとに CSV 形式で出力すれば良いのであれば、以下の様になります。

[code]

    CSV.open("posts.csv", "w") do |csv|
      doc.elements.each('posts/row') do |element|
        csv << ["Id", "PostTypeId", "AcceptedAnswerId", "Body"].map { |attr|
                 element.attributes[attr] }
      end
    end
    
[/code]

追加

"PostTypeId" が"1" の場合のみを出力するには以下の様にします。

[code]

    CSV.open("posts.csv","w") do |csv|
      doc.elements.each('posts/row') do |element|
        if element.attributes["PostTypeId"] == "1"
          csv << ["Id", "PostTypeId", "AcceptedAnswerId", "Body"].map { |attr|
                   element.attributes[attr] }
        end
      end
    end
    
[/code]


CC BY-SA 3.0
2015-10-07T07:52:06.590
17350
2015-10-07T08:30:33.800
2015-10-07T08:30:33.800
17344
answer