如何在Ruby中更新XML文件?

时间:2022-04-03 13:36:37

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?

我有两个XML文件,我需要使用Ruby更新。如果使用Nokogiri完成它会很棒。有人可以帮我怎么做?

Q1: I need to update <runName> tag

Q1:我需要更新 标签

<?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

Q2:在第二个文件中,我需要更改日志的路径。所以

<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:

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?

Q2:有关如何仅搜索价值一行的任何建议吗?

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) }

1 个解决方案

#1


9  

Q1:

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.

你不想要对象@doc,你想要它的XML表示。

Q2:

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.

同样,你不想要对象@doc,你想要它的XML表示。

#1


9  

Q1:

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.

你不想要对象@doc,你想要它的XML表示。

Q2:

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.

同样,你不想要对象@doc,你想要它的XML表示。