2013-03-30 39 views
2

我需要inno setup的幫助,我需要在特定行中保存一些xml節點,但我不知道如何去做。Inno Setup - 如何在特定行中保存節點

這是我的代碼

procedure SaveValueToXML(const AFileName, APath, AValue: string); 
var 
    XMLNode: Variant; 
    XMLDocument: Variant; 
begin 
    XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0'); 
    try 
    XMLDocument.async := False; 
    XMLDocument.load(AFileName); 
// if (XMLDocument.parseError.errorCode <> 0) then 
//  MsgBox('Install the software. ' + 
//  XMLDocument.parseError.reason, mbError, MB_OK) 
// else 
    begin 
     XMLDocument.setProperty('SelectionLanguage', 'XPath'); 
     XMLNode := XMLDocument.selectSingleNode(APath); 
     XMLNode.text := AValue; 
     XMLDocument.save(AFileName); 
    end; 
    except 
    MsgBox('Install the software', mbError, MB_OK); 
    end; 
end; 

function NextButtonClick(PageID: Integer): Boolean; 
var 
    XMLFile: string; 
begin 
    Result := True; 
    if (PageId = wpFinished) then 
    begin 
    XMLFile := ExpandConstant('{pf}\Hell\0\Config.xml'); 
    if FileExists(XMLFile) then 
    begin 
     SaveValueToXML(XMLFile, '//@param', PEdit.Text); //PEdit.text is from a custom input text box in the installer, ignore. 
     SaveValueToXML(XMLFile, '//@path', 
     ExpandConstant('{reg:HKCU\SOFTWARE\Craps,InstallPath}\Test.exe')); 
    end; 
    end; 
end; 

這是我的XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<stuffs> 
     <stuff ident="555" path="C:\Program Files (x86)\Other thing\Other.exe" param="-alive" display="1" priority="0"/> 
     <stuff ident="666" path="C:\Program Files (x86)\Craps\test.exe" param="-dead" display="1" priority="0"/>  
</stuffs> 

的問題是,我的腳本總是在第一行寫。我需要的是始終將節點保存在以<stuff ident="666"

開頭的行中。

回答

4

您將需要使用setAttribute方法insted設置text屬性。下面是修改節點的屬性值的過程:

procedure SaveAttributeValueToXML(const AFileName, APath, AAttribute, 
    AValue: string); 
var 
    XMLNode: Variant; 
    XMLDocument: Variant; 
begin 
    XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0'); 
    try 
    XMLDocument.async := False; 
    XMLDocument.load(AFileName); 
    if (XMLDocument.parseError.errorCode <> 0) then 
     MsgBox('The XML file could not be parsed. ' + 
     XMLDocument.parseError.reason, mbError, MB_OK) 
    else 
    begin 
     XMLDocument.setProperty('SelectionLanguage', 'XPath'); 
     XMLNode := XMLDocument.selectSingleNode(APath); 
     XMLNode.setAttribute(AAttribute, AValue); 
     XMLDocument.save(AFileName); 
    end; 
    except 
    MsgBox('An error occured!' + #13#10 + GetExceptionMessage, 
     mbError, MB_OK); 
    end; 
end; 

這裏是如何查詢其ident參數值是666,其param屬性值將被更改爲-alive節點:

SaveAttributeValueToXML('d:\File.xml', '//stuffs/stuff[@ident=''666'']', 
    'param', '-alive'); 

對於有關此處使用的XPath查詢的更多信息,請參閱例如到this article

+0

感謝您的回答! – Dielo

+1

不客氣! – TLama