title: How to update XML file in Ruby?
body: <p>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?</p>
  
  <p>Q1: I need to update <code>&lt;runName&gt;</code> tag</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;&lt;taftasks&gt;
  &lt;taftask environment="Local_edu" password="12324" username="edu"&gt;
  &lt;testsuite name="login"&gt;
  &lt;rowsToRun&gt;0&lt;/rowsToRun&gt;
  &lt;runName&gt;Login_Run 201107041433&lt;/runName&gt;
  &lt;runDescription&gt;ant&lt;/runDescription&gt;
  &lt;/testsuite&gt;
  &lt;/taftask&gt;
  &lt;/taftasks&gt;
  </code></pre>
  
  <p>Q2: In the second file I need to change the path for logs. So </p>
  
  <pre><code>&lt;property name="reportDir" value="C:\Program Files\TestPro\TestPro Automation Framework\Output Files\builds\basics\logs\" /&gt;
  </code></pre>
  
  <p>would look like </p>
  
  <pre><code>&lt;property name="reportDir" value="C:\Program Files\TestPro\TestPro Automation Framework\Output Files\builds\basics\logs\201107060928" /&gt;
  
  &lt;?xml version="1.0"?&gt;
  &lt;!-- build script for TAF command line execution  
  &lt;property name="lib.dir" value="${basedir}/lib" /&gt;
  &lt;project name="Login" default="taf" basedir="."&gt;
  --&gt;
  
  &lt;project name="Basics of Edu" default="taf" basedir="C:/Program Files/TestPro/TestPro Automation Framework"&gt;
  &lt;description&gt;Login_cycle&lt;/description&gt;
  &lt;property name="lib.dir" value="${basedir}/lib" /&gt;
  &lt;property name="testPlan" value="C:\Program Files\TestPro\TestPro Automation Framework\Output Files\builds\basics\basics.xml" /&gt;
  &lt;property name="reportDir" value="C:\Program Files\TestPro\TestPro Automation Framework\Output Files\builds\basics\logs\" /&gt;
  &lt;property name="format" value="csv" /&gt;
  &lt;property name="category" value="All" /&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <hr>
  
  <p>This is the source code related to these questions:</p>
  
  <p>Q1:</p>
  
  <pre><code>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) }
  </code></pre>
  
  <p>Q2:
  any suggestions how to make the search for <code>value</code> one line only?</p>
  
  <pre><code>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) }
  </code></pre>