unit1.pas

来自「简单的程序:关于XML文件的简单操作」· PAS 代码 · 共 82 行

PAS
82
字号
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, NativeXML, DateUtils;

type
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  i        : Integer;
  file_name: string;
  xml_file  : TNativeXml;
  xml_node  : TXmlNode;
  gps_node  : TXmlNode;
  comm_no   : string;
  gps_time_string: string;
  rec_time_string: string;
  gps_time  : TDateTime;
  rec_time  : TDateTime;
  m_type    : string;
begin
  if not OpenDialog1.Execute then
    Exit;

  file_name := OpenDialog1.FileName;

  xml_file := TNativeXml.Create;
  xml_file.LoadFromFile(file_name);
  try
    for i := 0 to xml_file.Root.NodeCount - 1 do
    begin
      xml_node := xml_file.Root.Nodes[i];
      gps_node := xml_node.NodeByName('gps');

      if not Assigned(gps_node) then
        Continue;

      gps_time_string := gps_node.ReadAttributeString('time');
      gps_time := StrToDateTimeDef(gps_time_string, 0);

      rec_time_string := gps_node.ReadAttributeString('recv');
      rec_time := StrToDateTimeDef(rec_time_string, 0);

      if gps_time <= rec_time then
        Continue;

      if MinutesBetween(gps_time, rec_time) < 55 then
        Continue;

      comm_no := xml_node.ReadAttributeString('Code');
      m_type  := xml_node.ReadAttributeString('Type');
      Memo1.Lines.Append(Format('%s:%s: %s    %s', [m_type, comm_no, gps_time_string, rec_time_string]) );

    end;

  finally
    xml_file.Free;
  end;
end;

end.

⌨️ 快捷键说明

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