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