2012-06-28 10 views
11

मैं एक एक्सएमएल फ़ाइल के कुछ नोड्स पढ़ना चाहता हूं और कुछ कस्टम इनपुट फ़ील्ड में अपने मान दिखाना चाहता हूं। यदि आवश्यक हो तो उपयोगकर्ता मूल्यों को बदल सकता है, और Next बटन पर क्लिक करके इन मानों को वापस एक्सएमएल में सहेजा जाना चाहिए।XML दस्तावेज़ नोड मानों को कैसे पढ़ और लिखना है?

इनोसेटअप स्क्रिप्ट में यह कैसे करें?

+0

संबंधित प्रश्न: [इनो सेटअप कस्टम इनपुट के आधार पर एक्सएमएल फ़ाइल संशोधित करें] (http://stackoverflow.com/q/8141886/588306)। – Deanna

उत्तर

23

मानक MSXML2.DOMDocument COM ऑब्जेक्ट को तुरंत चालू करने के लिए CreateOleObject फ़ंक्शन का उपयोग करें। निम्न स्क्रिप्ट लोड और एक्सएमएल फ़ाइल नीचे (स्क्रिप्ट ही MSDN से उदाहरण से प्रेरित था) पोस्ट से एक भी नोड के लिए एक पाठ मान को बचाने के लिए कैसे पता चलता है:

[Setup] 
AppName=My Program 
AppVersion=1.5 
DefaultDirName={pf}\My Program 
DefaultGroupName=My Program 
UninstallDisplayIcon={app}\MyProg.exe 
Compression=lzma2 
SolidCompression=yes 
OutputDir=userdocs:Inno Setup Examples Output 

[Files] 
Source: "MyProg.exe"; DestDir: "{app}" 
Source: "MyProg.chm"; DestDir: "{app}" 

[Icons] 
Name: "{group}\My Program"; Filename: "{app}\MyProg.exe" 

[Code] 
var 
    CustomEdit: TEdit; 
    CustomPageID: Integer; 

function LoadValueFromXML(const AFileName, APath: string): string; 
var 
    XMLNode: Variant; 
    XMLDocument: Variant; 
begin 
    Result := ''; 
    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); 
     Result := XMLNode.text; 
    end; 
    except 
    MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK); 
    end; 
end; 

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('The XML file could not be parsed. ' + 
     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('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK); 
    end; 
end; 

procedure InitializeWizard; 
var 
    CustomPage: TWizardPage; 
begin 
    CustomPage := CreateCustomPage(wpWelcome, 'Custom Page', 
    'Enter the new value that will be saved into the XML file'); 
    CustomPageID := CustomPage.ID; 
    CustomEdit := TEdit.Create(WizardForm); 
    CustomEdit.Parent := CustomPage.Surface; 
end; 

procedure CurPageChanged(CurPageID: Integer); 
begin 
    if CurPageID = CustomPageID then 
    CustomEdit.Text := LoadValueFromXML('C:\Setup.xml', '//Setup/FirstNode'); 
end; 

function NextButtonClick(CurPageID: Integer): Boolean; 
begin 
    Result := True; 
    if CurPageID = CustomPageID then 
    SaveValueToXML('C:\Setup.xml', '//Setup/FirstNode', CustomEdit.Text); 
end; 

यहाँ एक्सएमएल फ़ाइल में प्रयोग किया जाता है स्क्रिप्ट:

<?xml version="1.0" encoding="UTF-8"?> 
<Setup> 
    <FirstNode>First node value!</FirstNode> 
    <SecondNode>Second node value!</SecondNode> 
</Setup> 
+0

पीएस [Olecheck'] (http://www.jrsoftware.org/ishelp/topic_isxfunc_olecheck.htm) के साथ इस स्क्रिप्ट में प्रत्येक ओएलई ऑब्जेक्ट फ़ंक्शन कॉल को लपेटना अच्छा होगा, जो फ़ंक्शन कॉल करते समय अपवाद (पहले) उठाएगा विफल रहता है (जब परिणाम 'S_OK' मान से अलग होगा)। – TLama

+0

उदाहरण भी देखें [CodeAutomation.iss] (https://woofy.googlecode.com/hg/tools/Inno%20Setup/Examples/CodeAutomation.iss)। – Deanna

+0

@Deanna, मैं इसे पोस्ट करने से पहले उस उदाहरण को देख रहा था, लेकिन यह नोड को एक XML फ़ाइल में जोड़ने के तरीके के बारे में है, जबकि यह मौजूदा नोड मान को लोड और सहेजने के तरीके के बारे में है ;-) – TLama

संबंधित मुद्दे