Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Load XML Object from HTTP Response Body

Demonstrates how to load the HTTP response body directly into an XML object.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.Http,
  Chilkat.Xml,
  Chilkat.HttpResponse;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  resp: THttpResponse;
  xml: TXml;

begin
  success := False;

  http := THttp.Create;

  resp := THttpResponse.Create;
  success := http.HttpStr('GET','https://www.chilkatsoft.com/exampledata/inventory.xml','','','',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  xml := TXml.Create;

  //  If we wish to transfer (instead of copy) the XML from the HttpResponse to the Xml, then add the keyword "TakeResponseBody" to UncommonOptions
  //  This could save memory for extremely large XML responses.
  resp.UncommonOptions := 'TakeResponseBody';

  resp.GetBodyXml(xml);

  WriteLn(xml.GetXml());

  //  Note: If UncommonOptions contained "TakeResponseBody", then the response BodyStr will now be empty:
  WriteLn('----');
  WriteLn(resp.BodyStr);


  http.Free;
  resp.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.