xmldump.pp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. {$MODE objfpc}
  2. {$H+}
  3. program xmldump;
  4. uses sysutils, DOM, xmlread;
  5. const
  6. NodeNames: array[ELEMENT_NODE..NOTATION_NODE] of String = (
  7. 'Element',
  8. 'Attribute',
  9. 'Text',
  10. 'CDATA section',
  11. 'Entity reference',
  12. 'Entity',
  13. 'Processing instruction',
  14. 'Comment',
  15. 'Document',
  16. 'Document type',
  17. 'Document fragment',
  18. 'Notation'
  19. );
  20. procedure DumpNode(node: TDOMNode; spc: String);
  21. var
  22. i: Integer;
  23. attr: TDOMNode;
  24. begin
  25. Write(spc, NodeNames[node.NodeType]);
  26. if Copy(node.NodeName, 1, 1) <> '#' then
  27. Write(' "', node.NodeName, '"');
  28. if node.NodeValue <> '' then
  29. Write(' "', node.NodeValue, '"');
  30. if (node.Attributes <> nil) and (node.Attributes.Length > 0) then begin
  31. Write(',');
  32. for i := 0 to node.Attributes.Length - 1 do begin
  33. attr := node.Attributes.Item[i];
  34. Write(' ', attr.NodeName, ' = "', attr.NodeValue, '"');
  35. end;
  36. end;
  37. WriteLn;
  38. if node.FirstChild <> nil then
  39. DumpNode(node.FirstChild, spc + ' ');
  40. if node.NextSibling <> nil then
  41. DumpNode(node.NextSibling, spc);
  42. end;
  43. var
  44. xml: TXMLDocument;
  45. begin
  46. if ParamCount <> 1 then begin
  47. WriteLn('xmldump <xml or dtd file>');
  48. exit;
  49. end;
  50. if UpCase(ExtractFileExt(ParamStr(1))) = '.DTD' then
  51. ReadDTDFile(xml,ParamStr(1))
  52. else
  53. ReadXMLFile(xml,ParamStr(1));
  54. WriteLn('Successfully parsed the document. Structure:');
  55. WriteLn;
  56. if Assigned(xml.DocType) then
  57. begin
  58. WriteLn('DocType: "', xml.DocType.Name, '"');
  59. WriteLn;
  60. end;
  61. DumpNode(xml, '| ');
  62. xml.Free;
  63. end.