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

Convert MIME to XML

See more MIME Examples

Demonstrates the Chilkat Mime.GetXml method, which converts the MIME tree to Chilkat's XML representation and returns it as a string. It takes no arguments. Header names become element names, structured header parameters become attributes, and duplicate headers become repeated elements.

Tip: Code to parse the returned XML can be generated at Chilkat Tools.

Background: Turning a MIME tree into XML exposes its structure in a form that ordinary XML tools can query and transform — useful for inspection, debugging, or feeding a message into an XML-based pipeline. It is the inverse of LoadXml, so a message can be round-tripped through XML. For a quick human-readable outline rather than a full representation, see GetStructure.

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

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

procedure RunDemo;
var
  success: Boolean;
  mime: TMime;
  xml: string;

begin
  success := False;

  //  Demonstrates the Mime.GetXml method, which converts the MIME tree to Chilkat's XML
  //  representation and returns it as a string.  It takes no arguments.

  mime := TMime.Create;

  success := mime.LoadMimeFile('qa_data/message.eml');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Header names become XML element names and structured header parameters become XML attributes.
  xml := mime.GetXml();
  if (mime.LastMethodSuccess = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;
  WriteLn(xml);


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