Home > ruby > Convert XML to CSV with Nokogiri Ruby gem

Convert XML to CSV with Nokogiri Ruby gem

Once upon a time, in an exciting world of software testing… Exist QA team had been using Testlink 1.8.3 as an open-source tool for test management. They were happy and it serves them well not until their client request for a copy of the testcases with complete details in EXCEL format. Doomed! Testlink only offers generation of test specification in HTML, OpenOffice Writer and MS Word but unfortunately not in EXCEL.

But just like a princess with a prince charming… then came Nokogiri(saw in Japanese) gem from Ruby which is an HTML, XML, SAX  and Reader parser. It supports document searching via XPATH and CSS3 Selectors. Not to mention FasterCSV also a Ruby gem which provides a complete interface to CSV files and data.  It offers tools to enable you to read and write to and from Strings or IO objects, as needed.

First they install these precious gems in their Windows machine by executing the following commands:

gem install nokogiri
gem install fastercsv

With these tools Exist QA carefully plans a plot to solve their problem. Since Testlink has the ability to export testsuite together with its testcases in XML format, they use this advantage to pass it as an input file in their Testlink parser code in Ruby. Here’s their gameplan:

require ‘rubygems’
require ‘nokogiri’
require ‘fastercsv’

FIELDS = %w{Testsuite ID Name Summary Steps Expected_Result }

def new_testcase(csv, suite, id, name, summary, steps, expectedresult)

testcases = []
testcases << suite testcases << "GPC - #{id}" testcases << name testcases << summary testcases << steps testcases << expectedresult csv << FasterCSV::Row.new(FIELDS, testcases) end csv = FasterCSV.open(ARGV[1],"w") csv << FIELDS doc = Nokogiri::XML(open(ARGV[0])) doc.xpath('//testsuite').each do |tsuite| puts "#{tsuite.attribute('name')}\n" doc.xpath('//testcase').each do |tcase| new_testcase(csv, tsuite.attribute('name'), tcase.css('externalid').inner_text, tcase.attribute('name'), tcase.css('summary').inner_text, tcase.css('steps').inner_text, tsuite.css('expectedresults').inner_text) end end [/sourcecode] All they need to do is run the program in their console following this format: ruby <filename> "<input>" "<output>" Where filename is the name of the Testlink parser code; input is the xml filename(generated XML file from Testlink) and output is the csv filename(file where the parsed xml data will be saved).

ruby tlparser.rb “test.xml” “test.csv”

Nokogiri and FasterCSV saves the day! Now they can provide the testcase report in no time, every time their client request for it. And Exist QA lives happily ever after…

Categories: ruby Tags: , ,
  1. a2aks
    March 29, 2012 at 4:46 am


    Thank you for a good blog like this. But I was requiring something else. I wanted to take in as an input a Xml file and generate a csv file format after the parsing is done. Something like a GUI for users.

    Is there anyway as such apart from testsuites.?


  2. bob
    May 10, 2014 at 11:49 am

    Great entry, But what is the testlink parser code file? “filename is the name of the Testlink parser code”. How does one derive this .rb file?

  3. Jonathan Ebb
    May 16, 2014 at 1:31 am

    As of Ruby 1.9 It won’t use FasterCSV;

    “Please switch to Ruby 1.9′s standard CSV ” +
    “library. It’s FasterCSV plus support for ” +
    “Ruby 1.9′s m17n encoding engine.”

    Any idea how to modify script to use the standard csv library?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: