我正在开发一个DTS SSIS包,目的是使用Teamcity buildserver(通过Ruby / rake)...基本上我想为每个环境(Dev,UAT,Prod)定制不同的连接字符串..
例如在Test中我想指向SQL2008Test,并在Dev SQL2008Dev中。这意味着我需要在将dts包部署到相应的目录之前对其进行操作....
任何人都有使用REXML的经验 - 红宝石?
这是我的ruby代码(带有rakefile)
config = {}
File.open(File.join(BUILD_SSIS_DIR, IRS_DSS_USER_PACKAGE)) do |config_file|
config = REXML::Document.new(config_file)
ConfigTasks.set_dts_constring_irs config, 'ConnectionString',
"Data Source=SQL2008DEV;Initial Catalog=IRSDEV;Provider=SQLNCLI10.1;Integrated Security=SSPI;Auto Translate=False;Application Name=SSIS-IRS DSS User Data Package-{3CC00FAB-7009-402D-AE03-2426AFC6B7ED}SQL2008 IRS Multiple Connection;"
end
然后调用
def self.set_dts_constring_irs(config_file, name, connection_string)
conn_string_element = config_file.root.elements['DTS:ConnectionManager/DTS:Property'];
conn_string_element['DTS:Name="ConnectionString"'] = connection_string
end
这是我需要操作的DTS xml
<DTS:ConnectionManager>
<DTS:Property DTS:Name="DelayValidation">0</DTS:Property>
<DTS:Property DTS:Name="ObjectName">SQL2008 IRS Multiple Connection</DTS:Property>
<DTS:Property DTS:Name="DTSID">{3CC00FAB-7009-402D-AE03-2426AFC6B7ED}</DTS:Property>
<DTS:Property DTS:Name="Description"></DTS:Property>
<DTS:Property DTS:Name="CreationName">OLEDB</DTS:Property><DTS:ObjectData><DTS:ConnectionManager>
<DTS:Property DTS:Name="Retain">0</DTS:Property>
<DTS:Property DTS:Name="ConnectionString">Data Source=SQL2008DEV;Initial Catalog=IRSDEV;Provider=SQLNCLI10.1;Integrated Security=SSPI;Auto Translate=False;Application Name=SSIS-IRS DSS User Data Package-{3CC00FAB-7009-402D-AE03-2426AFC6B7ED}SQL2008 IRS Multiple Connection;</DTS:Property>
</DTS:ConnectionManager></DTS:ObjectData>
</DTS:ConnectionManager>
有人对我的工作有所了解吗?例如如何使用REXML深入研究我需要的连接字符串的元素/属性?
答案 0 :(得分:1)
我假设您要使用REXML在XML文件中查找连接字符串,并将其替换为您自己的连接字符串。 REXML允许您使用XPath查找元素并设置它的值:
require 'rexml/document'
# assuming:
xml = '<DTS xmlns:DTS="www.microsoft.com/SqlServer/Dts">
<DTS:ConnectionManager>
<DTS:Property DTS:Name="DelayValidation">0</DTS:Property>
<DTS:Property DTS:Name="ObjectName">SQL2008 IRS Multiple Connection</DTS:Property>
<DTS:Property DTS:Name="DTSID">{3CC00FAB-7009-402D-AE03-2426AFC6B7ED</DTS:Property>
<DTS:Property DTS:Name="Description"></DTS:Property>
<DTS:Property DTS:Name="CreationName">OLEDB</DTS:Property>
<DTS:ObjectData>
<DTS:ConnectionManager>
<DTS:Property DTS:Name="Retain">0</DTS:Property>
<DTS:Property DTS:Name="ConnectionString">Data Source=SQL2008DEV;Initial Catalog=IRSDEV;Provider=SQLNCLI10.1;Integrated Security=SSPI;Auto Translate=False;Application Name=SSIS-IRS DSS User Data Package-{3CC00FAB-7009-402D-AE03-2426AFC6B7ED}SQL2008 IRS Multiple Connection;</DTS:Property>
</DTS:ConnectionManager>
</DTS:ObjectData>
</DTS:ConnectionManager>
</DTS>'
# find the connection string with XPath
connection_string_xpath = '/DTS/DTS:ConnectionManager/DTS:ObjectData/DTS:ConnectionManager/DTS:Property[@DTS:Name="ConnectionString"]'
new_connection_string = 'your new connection string here'
begin
doc = REXML::Document.new(xml)
root = doc.root
# check that you can find the path you are looking for
unless root.elements[connection_string_xpath].nil?
# set the .text of the element to your connection string:
root.elements[connection_string_xpath].text = new_connection_string
# test:
puts doc
end
rescue REXML::ParseException => rexml_exception
puts 'Invalid XML', rexml_exception
end
将输出(删除一些XML ...):
...
<DTS:Property DTS:Name='ConnectionString'>your new connection string here</DTS:Property>
...