xmlファイル内の要素の属性をcsv形式で保存したい.使用言語はrubyです.
取得したい属性の表示はできたのですが,それらをcsv形式で保存するにはどうすればよいでしょうか?
以下に,rubyとxmlのファイルを示します.
また,示したxmlファイルについてですが,要素は2つしかありませんが,この要素の数が増えても問題なく処理が可能でしょうか?
<!-- 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="<p>What is the hardware and software differences between Intel and PPC Macs?</p>
" 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="<hardware><mac><powerpc><intel>" 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="<p>The VPN software I use for work (<a href="http://www.lobotomo.com/products/IPSecuritas/">IPSecuritas</a>) 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.</p>
" 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="<osx><mobileme><terminal><back-to-my-mac><script>" AnswerCount="1" CommentCount="0" FavoriteCount="4" />
<row Id="7" PostTypeId="2" ParentId="5" CreationDate="2010-08-17T19:30:45.577" Score="5" Body="<p>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.</p>

<p>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.</p>

<p>In short, I don't think its doable for that and potentially other reasons.</p>
" OwnerUserId="41" LastActivityDate="2010-08-17T19:30:45.577" CommentCount="4" />
</posts>
と
<!-- 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
です.
(追記)
rubyファイルを以下のように変更してcsv形式の保存を検討しています.
["a","b","c"]の部分を指定した属性にしたいのですがどうすればよいでしょうか?
<!-- 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
よろしくお願いいたします.