How to update XML file in Ruby?

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"?>
<!-- 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:

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) }
+5
source share
1 answer

1:

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 do not need an object @doc; you want its XML representation.

2:

property = @doc.css("property")

it should be:

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

You want only this node to 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 }

, @doc, XML.

+9

All Articles