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

XML Sort by Tag

See more XML Examples

Demonstrates the SortByTag method.

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

<root>
    <fruits>
        <apple>
            <fuji>blah</fuji>
            <gala>blah</gala>
            <grannySmith>blah</grannySmith>
            <honeycrisp>blah</honeycrisp>
            <mcIntosh>blah</mcIntosh>
        </apple>
        <banana>blah</banana>
        <blackberry>blah</blackberry>
        <blueberry>blah</blueberry>
        <orange>blah</orange>
        <pear>blah</pear>
    </fruits>
</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;
  xSortRoot: TXml;
  bAscending: Boolean;

begin
  success := False;

  xml := TXml.Create;

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

  //  Sort the direct children under the "fruits" node by tag:
  xSortRoot := xml.FindChild('fruits');

  //  Sort in ascending order.
  bAscending := True;
  xSortRoot.SortByTag(bAscending);

  //  Show the result:
  WriteLn(xml.GetXml());

  //  <root>
  //      <fruits>
  //          <apple>
  //              <grannySmith>blah</grannySmith>
  //              <gala>blah</gala>
  //              <fuji>blah</fuji>
  //              <mcIntosh>blah</mcIntosh>
  //              <honeycrisp>blah</honeycrisp>
  //          </apple>
  //          <banana>blah</banana>
  //          <blackberry>blah</blackberry>
  //          <blueberry>blah</blueberry>
  //          <orange>blah</orange>
  //          <pear>blah</pear>
  //      </fruits>
  //  </root>

  //  Sort the direct children under the "apple" node:
  success := xSortRoot.FindChild2('apple');

  xSortRoot.SortByTag(bAscending);
  WriteLn(xml.GetXml());

  //  <root>
  //      <fruits>
  //          <apple>
  //              <fuji>blah</fuji>
  //              <gala>blah</gala>
  //              <grannySmith>blah</grannySmith>
  //              <honeycrisp>blah</honeycrisp>
  //              <mcIntosh>blah</mcIntosh>
  //          </apple>
  //          <banana>blah</banana>
  //          <blackberry>blah</blackberry>
  //          <blueberry>blah</blueberry>
  //          <orange>blah</orange>
  //          <pear>blah</pear>
  //      </fruits>
  //  </root>

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