Ruby:将XML解析为CSV
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ruby:将XML解析为CSV相关的知识,希望对你有一定的参考价值。
I had an XML file that I needed to convert to CSV. In order to make things easier for myself, I used a pipe | symbol instead of a comma so that there would be no parsing errors later. I also ended up writing the file to a Txt file and importing it as such to Microsoft Excel.
require 'rexml/document' xml_file = File.open("showcase.xml", "r") csv_file = File.new("showcase.txt", "w") xml = REXML::Document.new( xml_file ) counter = 0 xml.elements.each("images") do |e| e.elements.each("image") do |f| counter = counter + 1 csv_file.puts f.elements['src'].text + "|" + f.elements['caption'].text + "|" + f.elements['uri'].text + "|" end end
以上是关于Ruby:将XML解析为CSV的主要内容,如果未能解决你的问题,请参考以下文章
使用 powershell 将嵌套的复杂 XML 解析为 CSV