Advertisement
TLama

Untitled

Aug 20th, 2015
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.37 KB | None | 0 0
  1. [Code]
  2. function TryGetAttrValuesByIndices(const FileName, Path, Attribute: string;
  3.   const Indices: TArrayOfInteger; out Values: TArrayOfString): Boolean;
  4. var
  5.   I: Integer;
  6.   XMLDocument: Variant;
  7.   XMLNodeList: Variant;
  8. begin
  9.   Result := False;
  10.  
  11.   XMLDocument := CreateOleObject('Msxml2.DOMDocument');
  12.   try
  13.     XMLDocument.async := False;
  14.     XMLDocument.load(FileName);
  15.     if (XMLDocument.parseError.errorCode <> 0) then
  16.       RaiseException(XMLDocument.parseError.reason)
  17.     else
  18.     begin
  19.       XMLDocument.setProperty('SelectionLanguage', 'XPath');
  20.       XMLNodeList := XMLDocument.selectNodes(Path);
  21.      
  22.       SetArrayLength(Values, GetArrayLength(Indices));
  23.       for I := 0 to GetArrayLength(Indices) - 1 do
  24.         Values[I] := XMLNodeList.item(Indices[I]).getAttribute(Attribute);
  25.  
  26.       Result := True;
  27.     end;
  28.   except
  29.     MsgBox('An error occured!' + #13#10 + GetExceptionMessage, mbError, MB_OK);  
  30.   end;
  31. end;
  32.  
  33. procedure InitializeWizard;
  34. var
  35.   StrArray: TArrayOfString;
  36.   IntArray: TArrayOfInteger;
  37. begin
  38.   SetArrayLength(IntArray, 2);
  39.   IntArray[0] := 1;
  40.   IntArray[1] := 2;
  41.  
  42.   if TryGetAttrValuesByIndices('C:\File.xml',
  43.     '//context/object/*', 'value', IntArray, StrArray) then
  44.   begin
  45.     MsgBox(Format(
  46.       'Item 1: %s' + #13#10 + 'Item 2: %s', [
  47.       StrArray[0], StrArray[1]]
  48.     ), mbInformation, MB_OK);
  49.   end;
  50. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement