Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update XML file in Ruby?

Tags:

xml

ruby

nokogiri

I have two XML files that I need to update using Ruby. It would be great if it is done using Nokogiri. Could somebody help me how to do so?

Q1: I need to update <runName> tag

<?xml version="1.0" encoding="ISO-8859-1"?><taftasks>
<taftask environment="Local_edu" password="12324" username="edu">
<testsuite name="login">
<rowsToRun>0</rowsToRun>
<runName>Login_Run 201107041433</runName>
<runDescription>ant</runDescription>
</testsuite>
</taftask>
</taftasks>

Q2: In the second file I need to change the path for logs. So

<property name="reportDir" value="C:\Program Files\TestPro\TestPro Automation Framework\Output Files\builds\basics\logs\" />

would look like

<property name="reportDir" value="C:\Program Files\TestPro\TestPro Automation Framework\Output Files\builds\basics\logs\201107060928" />

<?xml version="1.0"?>
<!-- build script for TAF command line execution  
<property name="lib.dir" value="${basedir}/lib" />
<project name="Login" default="taf" basedir=".">
-->

<project name="Basics of Edu" default="taf" basedir="C:/Program Files/TestPro/TestPro Automation Framework">
<description>Login_cycle</description>
<property name="lib.dir" value="${basedir}/lib" />
<property name="testPlan" value="C:\Program Files\TestPro\TestPro Automation Framework\Output Files\builds\basics\basics.xml" />
<property name="reportDir" value="C:\Program Files\TestPro\TestPro Automation Framework\Output Files\builds\basics\logs\" />
<property name="format" value="csv" />
<property name="category" value="All" />

</project>

This is the source code related to these questions:

Q1:

require 'rubygems' 
require 'nokogiri'

file_name = 'C:\web\playground\login.xml'
@doc = Nokogiri::XML(File.open(file_name))
runName = @doc.at_css "runName"
puts runName.content 
runName.content = "New run name"
puts runName.content
File.open(file_name, 'w') {|f| f.write(@doc) }

Q2: any suggestions how to make the search for value one line only?

require 'rubygems' 
require 'nokogiri'

file_name = 'C:\web\playground\login_build.xml'
@doc = Nokogiri::XML(File.open(file_name))

property = @doc.css("property")

property.each {|item|
  if (item['name'] == 'reportDir')
    puts item['value']
    item['value'] = item['value']+'\timestamp'
    puts item['value']
  end
}

File.open(file_name, 'w') {|f| f.write(@doc) }
like image 938
Radek Avatar asked Jul 06 '11 00:07

Radek


1 Answers

Q1:

Replace:

File.open(file_name, 'w') {|f| f.write(@doc) }

with:

File.open(file_name, 'w') {|f| f.puts @doc.to_xml }

Or better:

File.write(file_name, @doc.to_xml)

You don't want the object @doc, you want its XML representation.

Q2:

property = @doc.css("property")

should be:

property = @doc.at('property[name="reportDir"]')

You want only that particular node so look for it explicitly.

Replace:

property.each {|item|
  if (item['name'] == 'reportDir')
    puts item['value']
    item['value'] = item['value']+'\timestamp'
    puts item['value']
  end
}

with:

property['value'] = property['value'] + 'timestamp'

Replace:

File.open(file_name, 'w') {|f| f.write(@doc) }

with:

File.open(file_name, 'w') {|f| f.puts @doc.to_xml }

Again, you don't want the object @doc, you want its XML representation.

like image 62
the Tin Man Avatar answered Sep 29 '22 15:09

the Tin Man