qxmlwriter.pas

来自「Advanced.Export.Component.v4.01.rar,delp」· PAS 代码 · 共 103 行

PAS
103
字号
unit QXMLWriter;

interface

uses Classes, QExport4, QExport4Common;

type
  TQXMLWriter = class
  private
    FStream: TStream;
  public
    constructor Create(AStream: TStream); virtual;

    procedure Write(const WS: WideString);
    procedure WriteLn(const WS: WideString);
    procedure CreateProcessingInstruction(Version, Encoding,
      Standalone: WideString);
    procedure BeginNode(Name, Text: WideString;
      AttNames, AttValues: array of WideString);
    procedure EndNode(Name: WideString);
    procedure CreateFullNode(Name, Text: WideString;
      AttNames, AttValues: array of WideString);
  end;

implementation

uses SysUtils;


{ TXMLWriter }

procedure TQXMLWriter.CreateProcessingInstruction(Version, Encoding,
  Standalone: WideString);
var
  Body: WideString;
begin
  Body := '';
  if Version <> '' then
    Body := Body + Format('version="%s" ', [Version]);
  if Encoding <> '' then
    Body := Body + Format('encoding="%s" ', [Encoding]);
  if Standalone <> '' then
    Body := Body + Format('standalone="%s" ', [Standalone]);
  WriteLn('<?xml ' + Body + '?>');
end;

procedure TQXMLWriter.BeginNode(Name, Text: WideString; AttNames,
  AttValues: array of WideString);
var
  i: Integer;
begin
  Write('<' + Name);
  if Length(AttNames) > 0 then Write(' ');
  for i := 0 to Length(AttNames) - 1 do
    Write(AttNames[i] + '="' + AttValues[i] + '" ');
  Write('>' + Text);
end;

procedure TQXMLWriter.EndNode(Name: WideString);
begin
  WriteLn('</' + Name + '>')
end;

procedure TQXMLWriter.CreateFullNode(Name, Text: WideString;
  AttNames, AttValues: array of WideString);
var
  i: Integer;
begin
  Write('<' + Name);
  if Length(AttNames) > 0 then Write(' ');
  for i := 0 to Length(AttNames) - 1 do
    Write(AttNames[i] + '="' + AttValues[i] + '" ');
  if Text <> '' then
    Write('>' + Text + '</' + Name + '>')
  else
    Write('/>');
end;

procedure TQXMLWriter.Write(const WS: WideString);
var
  str: string;
begin
  {$IFDEF QE_UNICODE}
    str := UTF8Encode(WS);
  {$ELSE}
    str := WS;
  {$ENDIF}
  FStream.WriteBuffer(str[1], Length(str));
end;

procedure TQXMLWriter.WriteLn(const WS: WideString);
begin
  Write(WS + CRLF);
end;

constructor TQXMLWriter.Create(AStream: TStream);
begin
  inherited Create;
  FStream := AStream;
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?