I have two XML files that I need to update using Ruby. It would be great if this is done with the help of Nokogiri. Can someone help me how to do this?
Q1: I need to update the tag <runName>
<?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 the logs. So,
<property name="reportDir" value="C:\Program Files\TestPro\TestPro Automation Framework\Output Files\builds\basics\logs\" />
will look like
<property name="reportDir" value="C:\Program Files\TestPro\TestPro Automation Framework\Output Files\builds\basics\logs\201107060928" />
<?xml version="1.0"?>
<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:
1:
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) }
2: any suggestions how to do a search for valueonly one line?
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) }
source
share