如何使用InstallScript更新XML文件中的属性 [英] How to update attributes in an XML file with InstallScript
问题描述
问题:
我正在努力弄清楚如何使用InstallScript更新XML文件中的某些属性.我想使用适当的XML解析功能,但是找不到任何暗示此版本的InstallScript能够胜任的功能.我怎样才能做到这一点?我是否应该仅尝试使用FileInsertLine和FileGrep的组合?我缺少图书馆吗?
I am struggling trying to figure out how to update some attributes in an XML file using InstallScript. I would like to use proper XML parsing functions, but I can't find anything that suggests this version of InstallScript is capable of it. How can I do this? Should I just attempt falling back on a combination of FileInsertLine and FileGrep? Is there a library I'm missing?
背景:
什么软件版本?
我在Windows Server 2003 R2上使用InstallShield 11.
What software versions?
I am using InstallShield 11 on Windows Server 2003 R2.
为什么我不仅仅使用现有的"XML文件更改"功能?
因为我正在进行升级并遇到这个错误.它影响XML文件更改功能,因为它与组件绑定在一起(嗯,这是我的猜测).我花了很长时间尝试官方解决方法,但无法哄骗它正常工作.我最终发现,使用InstallScript +单行批处理文件来复制文件要容易得多,也更可靠.感觉有些古怪,但这实际上是可行的.
Why am I not just using the existing 'XML File Changes' feature?
Because I am doing an upgrade and running into this bug. It affects the XML File Change feature because it is tied to a Component (well, that's my guess). I spent too long trying the official workaround, but couldn't coax it to work. I eventually found out it was much easier and more reliable to copy the files over using InstallScript + a single line batch file. It feels a bit hacky, but it is something that actually works.
现在,我正在尝试找出最简单,最简单的数字显示方法,以便在InstallScript中复制"XML文件更改"功能的效果.
Now I am trying to figure out the easiest and simplest-to-figure-out-years-later way to replicate the effects of the 'XML File Changes' feature in InstallScript.
如果您需要任何更多信息,请告诉我,我们很乐意提供.
Please let me know if you need any more information, I will be glad to provide it.
我最终还是用InstallScript的方法来完成它-它往往是该安装程序项目中其他所有内容的实现方式,而且看起来(很快)就可以完成.我从该线程中的代码显示开始,修改它以满足我的需求.
I ended up going with the InstallScript way to do it after all - it has tended to be the way that everything else in this installer project was implemented, and it looked (and turned out to be) pretty quick to do. I started out with the code shown by TheTraveler in that thread and modified it to suit my needs.
这是代码:
prototype UpdateWebConfigAttributes();
prototype ReplaceValueOf(OBJECT, STRING, STRING);
function UpdateWebConfigAttributes()
OBJECT oDoc, oNode;
NUMBER i;
STRING sWebConfigFilePath;
BOOL successfulLoad;
begin
sWebConfigFilePath = "Path\\To\\Web.config";
if Is(FILE_EXISTS, sWebConfigFilePath) = FALSE then
MessageBox("Could not find Web.config file.", 0);
endif;
// get values from public properties
set oDoc = CreateObject("Msxml2.DOMDocument.4.0");
if !IsObject(oDoc) then
MessageBox("Could not create XML Document", 0);
return -1;
endif;
oDoc.async = FALSE;
oDoc.setProperty("SelectionLanguage", "XPath");
successfulLoad = oDoc.load(sWebConfigFilePath);
if !successfulLoad then
MessageBox("Could not load Web.config as an xml file", SEVERE);
return -1;
endif;
ReplaceValueOf(oDoc, "//add[@key=\"ConnectionDriver\"]", CONNECT_DRIVER);
ReplaceValueOf(oDoc, "//add[@key=\"ConnectionType\"]", CONNECT_TYPE);
ReplaceValueOf(oDoc, "//add[@key=\"ConnectionString\"]", CONNECT_STRING_WEBCONFIG);
ReplaceValueOf(oDoc, "//add[@key=\"ShowConnection\"]", "False");
oDoc.save(sWebConfigFilePath);
set oDoc = NOTHING;
end;
function ReplaceValueOf(oDoc, xPath, valueToPutIn)
OBJECT oNode;
begin
set oNode = oDoc.selectNodes(xPath)(0);
try
oNode.attributes.getNamedItem("value").value = valueToPutIn;
catch
MessageBox("Could not set '" + xPath + "' with '" + valueToPutIn + "'", SEVERE);
endcatch;
end;
推荐答案
我记得在那些日子里,XML更改还存在很多漏洞.您正在使用哪种项目类型?如果它是MSI类型,那么我将在WiX中创建一个合并模块,并使用它的XML更改模式.然后,您可以将该合并模块添加到InstallShield项目中.
I recall the XML Changes being pretty buggy back in those days. What project type are you using? If it's an MSI type then I would create a merge module in WiX and use it's XML changes pattern. You can then add that merge module to your InstallShield project.
如果您确实想在InstallScript中执行此操作,则必须使用CoCreateObject()来调用XML DOM.
If you really do want to do it in InstallScript then you are going to have to use CoCreateObject() to call into an XML DOM.
在 XML安装脚本上有一个讨论区和示例.
There is a discussino and example over at XML Installscript
但是,我不会那样做.我要么升级到InstallShield的较新版本,要么利用WiX中的Util扩展来保持声明性.我不喜欢编写这些类型的自定义操作,因为通常情况下效果不佳.
But again, I wouldn't do it this way. I'd either upgrade to a newer version of InstallShield or I'd leverage the Util extension in WiX to keep it declarative. I don't like writing these types of custom actions as it usually doesn't end well.
这篇关于如何使用InstallScript更新XML文件中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!