xmldump.pp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. if Assigned(xml.DocType) then
  58. begin
  59. WriteLn('DocType: "', xml.DocType.Name, '"');
  60. WriteLn;
  61. end;
  62. DumpNode(xml, '| ');
  63. xml.Free;
  64. end.
  65. {
  66. $Log$
  67. Revision 1.2 2000-07-13 11:33:06 michael
  68. + removed logs
  69. }