htmlwithsax.lpr 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. program htmlwithsax;
  2. uses sysutils, classes, sax,sax_html, custapp;
  3. Type
  4. { TMyApp }
  5. TMyApp = Class(TCustomApplication)
  6. Private
  7. Indent : string;
  8. procedure DoEndDocument(Sender: TObject);
  9. procedure DoEndElement(Sender: TObject; const NamespaceURI, LocalName, QName: SAXString);
  10. procedure DoFile(const aFileName: String);
  11. procedure DoStartDocument(Sender: TObject);
  12. procedure DoStartElement(Sender: TObject; const NamespaceURI, LocalName, QName: SAXString; Atts: TSAXAttributes);
  13. Protected
  14. Procedure DoRun; override;
  15. end;
  16. { TMyApp }
  17. procedure TMyApp.DoFile(const aFileName : String);
  18. var
  19. F : TFileStream;
  20. MyReader : THTMLReader;
  21. begin
  22. MyReader:=Nil;
  23. F:=TFileStream.Create(aFileName,fmOpenRead or fmShareDenyWrite);
  24. try
  25. MyReader:=THTMLReader.Create;
  26. MyReader.OnStartDocument:=@DoStartDocument;
  27. MyReader.OnStartElement:=@DoStartElement;
  28. MyReader.OnEndElement:=@DoEndElement;
  29. MyReader.OnEndDocument:=@DoEndDocument;
  30. MyReader.ParseStream(F);
  31. finally
  32. FreeAndNil(MyReader);
  33. F.Free;
  34. end;
  35. end;
  36. procedure TMyApp.DoRun;
  37. var
  38. I : Integer;
  39. begin
  40. StopOnException:=True;
  41. Terminate;
  42. if ParamCount<1 then
  43. begin
  44. Writeln('Usage : ',ExtractFileName(ExeName),' <htmlfile1> [htmlfile2 [htmlfile3]]');
  45. Exit;
  46. end;
  47. for I:=1 to ParamCount do
  48. DoFile(Params[i]);
  49. end;
  50. procedure TMyApp.DoStartDocument(Sender: TObject);
  51. begin
  52. Writeln('Document start');
  53. Indent:='';
  54. end;
  55. procedure TMyApp.DoEndElement(Sender: TObject; const NamespaceURI, LocalName, QName: SAXString);
  56. begin
  57. Indent:=Copy(Indent,1,Length(Indent)-2);
  58. end;
  59. procedure TMyApp.DoEndDocument(Sender: TObject);
  60. begin
  61. Writeln('Document end');
  62. Indent:='';
  63. end;
  64. procedure TMyApp.DoStartElement(Sender: TObject; const NamespaceURI, LocalName, QName: SAXString; Atts: TSAXAttributes);
  65. Var
  66. I : Integer;
  67. S : unicodestring;
  68. begin
  69. S:='';
  70. if Assigned(Atts) then
  71. for I:=0 to Atts.Length-1 do
  72. begin
  73. if S<>'' then S:=S+', ';
  74. S:=S+Atts.LocalNames[i];
  75. end;
  76. Write(Indent,'Tag: <',LocalName,'>');
  77. if NameSpaceURI<>'' then
  78. Write(' xmlns: ',NameSpaceURI);
  79. if QName<>'' then
  80. Write(', full tag: ',QName);
  81. If S<>'' then
  82. Write(', attrs: ',S);
  83. Writeln;
  84. Indent:=Indent+' ';
  85. end;
  86. begin
  87. With TMyApp.Create(Nil) do
  88. try
  89. Initialize;
  90. Run;
  91. finally
  92. Free;
  93. end;
  94. end.