Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Find and Update an XML Attribute Value

See more XML Examples

Demonstrates how to find an element in XML with a specified attribute name and then update the attribute value.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.StringBuilder,
  Chilkat.Xml;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  xml1: TXml;
  xml2: TXml;
  phrase_id: string;
  phrase_value: string;
  sbPath: TStringBuilder;
  i: Integer;
  count_i: Integer;

begin
  //  Let's say you have 2 XML files with identical structures as shown directly below,
  //  and you wish to update the values in the 1st XML file with those in the 2nd XML file.

  //  <global>
  //      <phrase id="2FADisabled" value="2factor authentication disabled"/>
  //      <phrase id="2FAEnabled" value="2factor authentication enabled"/>
  //      <phrase id="2FAResetFailed" value="Failed to disable two factor authentication"/>
  //  </global>

  //  <global>
  //      <phrase id="2FAResetFailed" value="Failed to reset two factor authentication"/>
  //      <phrase id="2FADisabled" value="2FA authentication disabled"/>
  //      <phrase id="2FAEnabled" value="2FA authentication enabled"/>
  //  </global>

  //  First, initialize each of the Chilkat XML objects.
  //  These could be loaded from files, but we'll just create them manually for this example..

  xml1 := TXml.Create;
  xml1.Tag := 'global';
  xml1.UpdateAttrAt('phrase',True,'id','2FADisabled');
  xml1.UpdateAttrAt('phrase',True,'value','2factor authentication disabled');
  xml1.UpdateAttrAt('phrase[1]',True,'id','2FAEnabled');
  xml1.UpdateAttrAt('phrase[1]',True,'value','2factor authentication enabled');
  xml1.UpdateAttrAt('phrase[2]',True,'id','2FAResetFailed');
  xml1.UpdateAttrAt('phrase[2]',True,'value','Failed to disable two factor authentication');

  xml2 := TXml.Create;
  xml2.Tag := 'global';
  xml2.UpdateAttrAt('phrase',True,'id','2FAResetFailed');
  xml2.UpdateAttrAt('phrase',True,'value','Failed to reset two factor authentication');
  xml2.UpdateAttrAt('phrase[1]',True,'id','2FADisabled');
  xml2.UpdateAttrAt('phrase[1]',True,'value','2FA authentication disabled');
  xml2.UpdateAttrAt('phrase[2]',True,'id','2FAEnabled');
  xml2.UpdateAttrAt('phrase[2]',True,'value','2FA authentication enabled');

  //  Iterate over attribute values in xml2 and update the same in xml1.

  sbPath := TStringBuilder.Create;

  i := 0;
  count_i := xml2.NumChildrenHavingTag('phrase');
  while i < count_i do
    begin
      xml2.I := i;
      phrase_id := xml2.ChilkatPath('phrase[i]|(id)');
      phrase_value := xml2.ChilkatPath('phrase[i]|(value)');

      //  Notice how the UpdateAttrAt method's 1st argument is a Chilkat path -- which is an expression
      //  to find the location of the XML element to be updated.
      //  Here we create a path of the form "/A/tagName,attributeName,attributeValuePattern", which means
      //  find the direct descendent of xml2 having tag=tagName, with an attribute=attributeName where the attribute value
      //  matches the attributeValuePattern.  If attributeValuePattern does not contain a "*", then it must match directly.
      //  We'll build a Chilkat path such as "/A/phrase,id,2FADisabled"
      sbPath.SetString('/A/phrase,id,');
      sbPath.Append(phrase_id);

      //  This assumes the corresponding element exists in xml1.
      xml1.UpdateAttrAt(sbPath.GetAsString(),True,'value',phrase_value);

      i := i + 1;
    end;

  //  Now see how xml1 has been updated..
  WriteLn(xml1.GetXml());

  //  <global>
  //      <phrase id="2FADisabled" value="2FA authentication disabled"/>
  //      <phrase id="2FAEnabled" value="2FA authentication enabled"/>
  //      <phrase id="2FAResetFailed" value="Failed to reset two factor authentication"/>
  //  </global>


  xml1.Free;
  xml2.Free;
  sbPath.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.