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

Methods for Getting Attributes

See more XML Examples

Demonstrates some methods for getting attribute name/values.

The input XML, available at http://www.chilkatsoft.com/data/car.xml, is this:

<root>
    <car color="black" make="mercedes" model="C350" hp="302" engine="v6" type="sedan">Mercedes Benz C350</car>
</root>

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.Xml;

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

procedure RunDemo;
var
  success: Boolean;
  xml: TXml;
  carNode: TXml;
  numAttr: Integer;
  horsepower: Integer;
  i: Integer;

begin
  success := False;

  xml := TXml.Create;

  //  The sample input XML is available at http://www.chilkatsoft.com/data/car.xml
  success := xml.LoadXmlFile('car.xml');
  if (success <> True) then
    begin
      WriteLn(xml.LastErrorText);
      Exit;
    end;

  //  Navigate to the "car" node, which is the 1st child:
  carNode := xml.FirstChild();

  //  Get the value of the "model" attribute:
  WriteLn('model = ' + carNode.GetAttrValue('model'));

  //  Get the value of the "hp" attribute as an integer:
  horsepower := carNode.GetAttrValueInt('hp');
  WriteLn('horsepower = ' + horsepower);

  //  Iterate over the attributes and show the name/value of each:
  numAttr := carNode.NumAttributes;

  i := 0;
  while i < numAttr do
    begin
      WriteLn(carNode.GetAttributeName(i) + ': ' + carNode.GetAttributeValue(i));
      i := i + 1;
    end;

  carNode.Free;


  xml.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.