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

Auto-Trim XML Content when Loading

See more XML Examples

This example explains the "autoTrim" argument that is passed to a method such as LoadXml2.

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;
  autoTrim: Boolean;

begin
  success := False;

  xml := TXml.Create;

  //  If autoTrim is True, then the content inside an leaf element is trimmed.
  //  For example:
  autoTrim := True;
  xml.LoadXml2('<abc><xyz>  123   </xyz></abc>',autoTrim);
  WriteLn(xml.GetXml());

  //  Output is:
  //  (notice the SPACE chars before and after "xyz" are trimmed)

  //  <?xml version="1.0" encoding="utf-8" ?>
  //  <abc>
  //      <xyz>123</xyz>
  //  </abc>

  //  If autoTrim is False, then the content inside leaf elements are not trimmed.
  autoTrim := False;
  xml.LoadXml2('<abc><xyz>  123   </xyz></abc>',autoTrim);
  WriteLn(xml.GetXml());

  //  Output is:

  //  <?xml version="1.0" encoding="utf-8" ?>
  //  <abc>
  //      <xyz>  123   </xyz>
  //  </abc>

  //  --------------------------------------------------------------------
  //  The EmitCompact property controls whether XML is emitted indented (pretty-printed)
  //  or compact.  For example:

  //  Auto-trim + emit compact:
  autoTrim := True;
  xml.LoadXml2('<abc><xyz>  123   </xyz></abc>',autoTrim);
  xml.EmitCompact := True;
  WriteLn(xml.GetXml());

  //  Output is:

  //  <?xml version="1.0" encoding="utf-8" ?>
  //  <abc><xyz>123</xyz></abc>

  //  No Auto-trim + emit compact:
  autoTrim := False;
  xml.LoadXml2('<abc><xyz>  123   </xyz></abc>',autoTrim);
  xml.EmitCompact := True;
  WriteLn(xml.GetXml());

  //  Output is:

  //  <?xml version="1.0" encoding="utf-8" ?>
  //  <abc><xyz>  123   </xyz></abc>


  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.