xmldump.pp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // $Id$
  2. {$MODE objfpc}
  3. {$H+}
  4. program xmldump;
  5. uses sysutils, DOM, xmlread;
  6. const
  7. NodeNames: array[ELEMENT_NODE..NOTATION_NODE] of String = (
  8. 'Element',
  9. 'Attribute',
  10. 'Text',
  11. 'CDATA section',
  12. 'Entity reference',
  13. 'Entity',
  14. 'Processing instruction',
  15. 'Comment',
  16. 'Document',
  17. 'Document type',
  18. 'Document fragment',
  19. 'Notation'
  20. );
  21. procedure DumpNode(node: TDOMNode; spc: String);
  22. var
  23. i: Integer;
  24. attr: TDOMNode;
  25. begin
  26. Write(spc, NodeNames[node.NodeType]);
  27. if Copy(node.NodeName, 1, 1) <> '#' then
  28. Write(' "', node.NodeName, '"');
  29. if node.NodeValue <> '' then
  30. Write(' "', node.NodeValue, '"');
  31. if (node.Attributes <> nil) and (node.Attributes.Length > 0) then begin
  32. Write(',');
  33. for i := 0 to node.Attributes.Length - 1 do begin
  34. attr := node.Attributes.Item[i];
  35. Write(' ', attr.NodeName, ' = "', attr.NodeValue, '"');
  36. end;
  37. end;
  38. WriteLn;
  39. if node.FirstChild <> nil then
  40. DumpNode(node.FirstChild, spc + ' ');
  41. if node.NextSibling <> nil then
  42. DumpNode(node.NextSibling, spc);
  43. end;
  44. var
  45. xml: TXMLDocument;
  46. begin
  47. if ParamCount <> 1 then begin
  48. WriteLn('xmldump <xml or dtd file>');
  49. exit;
  50. end;
  51. if UpCase(ExtractFileExt(ParamStr(1))) = '.DTD' then
  52. ReadDTDFile(xml,ParamStr(1))
  53. else
  54. ReadXMLFile(xml,ParamStr(1));
  55. WriteLn('Successfully parsed the document. Structure:');
  56. WriteLn;
  57. DumpNode(xml, '| ');
  58. xml.Free;
  59. end.
  60. {
  61. $Log$
  62. Revision 1.5 2000-01-30 22:20:57 sg
  63. * The XML config object is now freed at the end of the program
  64. (this enables us to detect memory leaks with this test program)
  65. Revision 1.4 2000/01/06 01:20:36 peter
  66. * moved out of packages/ back to topdir
  67. Revision 1.1 2000/01/03 19:33:10 peter
  68. * moved to packages dir
  69. Revision 1.2 1999/08/27 15:52:49 michael
  70. * Adapted to new xmlread
  71. Revision 1.1 1999/07/09 21:06:59 michael
  72. + Initial implementation by sebastian Guenther
  73. }