xmlwrite.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999 Sebastian Guenther, [email protected]
  5. XML writing routines
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$MODE objfpc}
  13. unit xmlwrite;
  14. interface
  15. uses DOM;
  16. procedure WriteXMLFile(doc: TXMLDocument; var AFile: Text);
  17. implementation
  18. procedure WriteElement(node: TDOMNode); forward;
  19. procedure WriteAttribute(node: TDOMNode); forward;
  20. procedure WriteText(node: TDOMNode); forward;
  21. procedure WriteCDATA(node: TDOMNode); forward;
  22. procedure WriteEntityRef(node: TDOMNode); forward;
  23. procedure WriteEntity(node: TDOMNode); forward;
  24. procedure WritePI(node: TDOMNode); forward;
  25. procedure WriteComment(node: TDOMNode); forward;
  26. procedure WriteDocument(node: TDOMNode); forward;
  27. procedure WriteDocumentType(node: TDOMNode); forward;
  28. procedure WriteDocumentFragment(node: TDOMNode); forward;
  29. procedure WriteNotation(node: TDOMNode); forward;
  30. type
  31. TWriteProc = procedure(node: TDOMNode);
  32. const
  33. WriteProcs: array[ELEMENT_NODE..NOTATION_NODE] of TWriteProc =
  34. (WriteElement, WriteAttribute, WriteText, WriteCDATA, WriteEntityRef,
  35. WriteEntity, WritePI, WriteComment, WriteDocument, WriteDocumentType,
  36. WriteDocumentFragment, WriteNotation);
  37. procedure WriteNode(node: TDOMNode);
  38. begin
  39. WriteProcs[node.NodeType](node);
  40. end;
  41. var
  42. f: ^Text;
  43. indent: String;
  44. procedure IncIndent;
  45. begin
  46. indent := indent + ' ';
  47. end;
  48. procedure DecIndent;
  49. begin
  50. indent := Copy(indent, 1, Length(indent) - 2);
  51. end;
  52. procedure WriteElement(node: TDOMNode);
  53. var
  54. i: Integer;
  55. attr, child: TDOMNode;
  56. begin
  57. Write(f^, Indent, '<', node.NodeName);
  58. for i := 0 to node.Attributes.Length - 1 do begin
  59. attr := node.Attributes.Item[i];
  60. Write(f^, ' ', attr.NodeName, '="', attr.NodeValue, '"');
  61. end;
  62. child := node.FirstChild;
  63. if child = nil then
  64. WriteLn(f^, '/>')
  65. else begin
  66. WriteLn(f^, '>');
  67. IncIndent;
  68. repeat
  69. WriteNode(child);
  70. child := child.NextSibling;
  71. until child = nil;
  72. DecIndent;
  73. WriteLn(f^, Indent, '</', node.NodeName, '>');
  74. end;
  75. end;
  76. procedure WriteAttribute(node: TDOMNode);
  77. begin
  78. WriteLn('WriteAttribute');
  79. end;
  80. procedure WriteText(node: TDOMNode);
  81. begin
  82. WriteLn('WriteText');
  83. end;
  84. procedure WriteCDATA(node: TDOMNode);
  85. begin
  86. WriteLn('WriteCDATA');
  87. end;
  88. procedure WriteEntityRef(node: TDOMNode);
  89. begin
  90. WriteLn('WriteEntityRef');
  91. end;
  92. procedure WriteEntity(node: TDOMNode);
  93. begin
  94. WriteLn('WriteEntity');
  95. end;
  96. procedure WritePI(node: TDOMNode);
  97. begin
  98. WriteLn('WritePI');
  99. end;
  100. procedure WriteComment(node: TDOMNode);
  101. begin
  102. WriteLn('WriteComment');
  103. end;
  104. procedure WriteDocument(node: TDOMNode);
  105. begin
  106. WriteLn('WriteDocument');
  107. end;
  108. procedure WriteDocumentType(node: TDOMNode);
  109. begin
  110. WriteLn('WriteDocumentType');
  111. end;
  112. procedure WriteDocumentFragment(node: TDOMNode);
  113. begin
  114. WriteLn('WriteDocumentFragment');
  115. end;
  116. procedure WriteNotation(node: TDOMNode);
  117. begin
  118. WriteLn('WriteNotation');
  119. end;
  120. procedure WriteXMLFile(doc: TXMLDocument; var AFile: Text);
  121. var
  122. child: TDOMNode;
  123. begin
  124. f := @AFile;
  125. Write(f^, '<?xml version="');
  126. if doc.XMLVersion <> '' then Write(f^, doc.XMLVersion)
  127. else Write(f^, '1.0');
  128. Write(f^, '"');
  129. if doc.Encoding <> '' then Write(f^, ' encoding="', doc.Encoding, '"');
  130. WriteLn(f^, '?>');
  131. indent := '';
  132. child := doc.FirstChild;
  133. while child <> nil do begin
  134. WriteNode(child);
  135. child := child.NextSibling;
  136. end;
  137. end;
  138. end.
  139. {
  140. $Log$
  141. Revision 1.1 1999-07-09 08:35:09 michael
  142. + Initial implementation by Sebastian Guenther
  143. }