Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

xml - Inno Setup - How to save a node in a specific line

i need help with inno setup, i need to save some xml nodes in a specific line, but i dont know how to.

This is my code

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}HellConfig.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:HKCUSOFTWARECraps,InstallPath}Test.exe'));
    end;
  end;
end;

This is my XML file:

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

The problem is that my script always write in the first line. What i need is to always save the nodes in the line that start with <stuff ident="666"

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You will need to use the setAttribute method insted of setting text property. Here is a procedure for modifying node attribute values:

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;

And here is how to query a node whose ident parameter value is 666 and whose param attribute value will be changed to -alive:

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

For more information about the XPath query used here refer e.g. to this article.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...