2011-11-19 221 views
0

在我的Inno Setup的腳本將節點添加到現有的XML文件有一個[代碼]部分,我需要添加一些代碼:使用Inno Setup的

  1. 打開XML文件
  2. 再加入在一個特定的地方
  3. 將文件保存回硬盤

我需要能夠編輯一個名爲config.xml文件中\文檔文件中的單個節點\ docotype

在文件中有一些像這樣的代碼:

<References> 
    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <string>System.dll</string> 
    <string>System.Core.dll</string> 
    <string>System.Drawing.dll</string> 
    <string>System.Windows.Forms.dll</string> 
    <string>System.XML.dll</string> 
    </ArrayOfString> 
</References> 

我需要它看起來像這樣:

<References> 
    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <string>System.dll</string> 
    <string>System.Core.dll</string> 
    <string>System.Drawing.dll</string> 
    <string>System.Windows.Forms.dll</string> 
    <string>System.XML.dll</string> 
    <string>C:\\bin\Custom\cutty109.dll</string> 
    </ArrayOfString> 
</References> 

所以我真的只需要下面一行添加到該文件中的「 ArrayOfString」部分

<string>C:\\bin\Custom\cutty109.dll</string> 

我敢肯定,這一定是可能的,但我不知道如何..

感謝

+0

既然您確切知道文件最終會是什麼樣子,爲什麼不直接提供帶有安裝包的文件並讓安裝程序覆蓋目標文件。 –

+1

與[此問題]幾乎相同的問題和答案(http://stackoverflow.com/questions/8141886/inno-setup-modify-xml-file-based-on-custom-input) – Deanna

+1

這是爲什麼標記爲各種VB標籤呢? – Deanna

回答

0

嘗試是這樣的:

Dim sXPath : sXPath = "/configuration/References/ArrayOfString" 
    Dim sAdd : sAdd  = "C:\\bin\Custom\cutty109.dll" 
    Dim sElm : sElm  = "string" 
    Dim sFSpec : sFSpec = resolvePath("..\data\config.xml") 
    Dim oXDoc : Set oXDoc = CreateObject("Msxml2.DOMDocument") 
    oXDoc.setProperty "SelectionLanguage", "XPath" 
    oXDoc.async = False 
    oXDoc.load sFSpec 

    If 0 = oXDoc.ParseError Then 
    WScript.Echo sFSpec, "looks ok" 
    Dim ndFnd : Set ndFnd = oXDoc.selectSingleNode(sXPath) 
    If ndFnd Is Nothing Then 
     WScript.Echo "|", sXPath, "| not found" 
    Else 
     WScript.Echo "found |" & ndFnd.tagName & "|" 
     Dim ndNew : Set ndNew = oXDoc.createElement(sElm) 
     ndNew.appendChild oXDoc.createTextNode(sAdd) 
     ndFnd.appendChild ndNew 
     WScript.Echo "After appending:" 
     WScript.Echo oXDoc.xml 
     oXDoc.Save Replace(sFSpec, ".xml", "-2.xml") 
    End If 
    Else 
    WScript.Echo oXDoc.ParseError.Reason 
    End If 

步驟:

  • 創建Msxml2.DOMDocument
  • 使用XPath來查找節點改變
  • 創建一個字符串,現在元素並附加文本
  • 將新節點追加到找到的節點
  • 保存修改後的XML
+0

良好的開端,但InnoSetup使用PascalScript –

1

我假設你真的需要一些動態的方式添加到該配置文件,如果沒有的話當然覆蓋舊的是最簡單的方法。

要動態部分添加到配置文件中,你有一些選擇:

  1. 您可以創建自己的命令行實用程序(EXE或腳本),做文件操作和調用實用程序安裝腳本的[Run]部分。這可能是這個樣子:

    • [Files]部分,你必須爲你的效用一行:

      來源:「myUtil.exe」; DESTDIR:「{}應用」

    • [Run]部分,你必須爲每個你需要在你的配置做的,這樣操作一個行:

      文件名:「{應用} \ myUtil。exe文件「;參數 」/ Addsection的:「

    OR

  2. 您可以使用帕斯卡爾腳本來操縱你的配置文件,您可以創建一個使用CreateOleObject調用msxml.dll XML的一個Pascal。文件操作然後,在你[Files]部分,您可以使用AfterInstall來打電話給你的Pascal函數,像這樣:

    Source: "myFileThatNeedsConfigManipulation.dll"; DestDir: ... ; 
        AfterInstall: MyPascalFunctionThatDoesTheManipulation 
    
3

請參考CodeAutomation.iss example隨inno安裝一起提供。並使用此代碼代替「修改XML文檔」部分下的原始代碼。

{ Modify the XML document } 

    NewNode := XMLDoc.createElement('string'); 
    XMLDoc.setProperty('SelectionLanguage', 'XPath'); 
    RootNode := XMLDoc.selectSingleNode('//References/ArrayOfString'); 
    RootNode.appendChild (NewNode); 
    RootNode.lastChild.text :='C:\\bin\Custom\cutty109.dll'; 

    { Save the XML document }