Difference between revisions of "xmlwrite"

From Free Pascal wiki
Jump to navigationJump to search
m
m (Fixed syntax highlighting; deleted category included in page template)
 
Line 1: Line 1:
 +
{{xmlwrite}}
 +
 
Writes a DOM structure as XML data into a file or stream. It can deal both with XML files and XML fragments.
 
Writes a DOM structure as XML data into a file or stream. It can deal both with XML files and XML fragments.
  
Line 5: Line 7:
 
Declarations excerpted from XMLWrite.pas.
 
Declarations excerpted from XMLWrite.pas.
  
<source>
+
<syntaxhighlight lang=pascal>
 
procedure WriteXMLFile(doc: TXMLDocument; const AFileName: String); overload;
 
procedure WriteXMLFile(doc: TXMLDocument; const AFileName: String); overload;
 
procedure WriteXMLFile(doc: TXMLDocument; var AFile: Text); overload;
 
procedure WriteXMLFile(doc: TXMLDocument; var AFile: Text); overload;
Line 13: Line 15:
 
procedure WriteXML(Element: TDOMNode; var AFile: Text); overload;
 
procedure WriteXML(Element: TDOMNode; var AFile: Text); overload;
 
procedure WriteXML(Element: TDOMNode; AStream: TStream); overload;
 
procedure WriteXML(Element: TDOMNode; AStream: TStream); overload;
</source>
+
</syntaxhighlight>
  
 
== Encodings ==
 
== Encodings ==
 +
 
At the moment it supports only the [[UTF-8]] output encoding. The encoding label is not written because it is optional for UTF-8.
 
At the moment it supports only the [[UTF-8]] output encoding. The encoding label is not written because it is optional for UTF-8.
  
 
== Character escaping ==
 
== Character escaping ==
 +
 
The writer provides proper character escaping as required by XML:
 
The writer provides proper character escaping as required by XML:
  
Line 36: Line 40:
 
== End-of-line handling ==
 
== End-of-line handling ==
  
At the moment always uses [[End of Line|line endings]] which are default for the target platform (#13#10 for Windows, #10 for Linux).
+
At the moment always uses [[End of Line|line endings]] which are default for the target platform (#13#10 for Windows, #10 for Linux). Moreover, any #13, #10 or #13#10 in input data is treated as a single line-ending.
Moreover, any #13, #10 or #13#10 in input data is treated as a single line-ending.
 
  
 
== Notes ==
 
== Notes ==
Line 46: Line 49:
 
Back to [[fcl-xml]] overview.
 
Back to [[fcl-xml]] overview.
  
[[Category:XML]]
 
  
 
== See also ==
 
== See also ==
[[xmlread]]
+
 
 +
* [[xmlread]]

Latest revision as of 09:13, 3 March 2020

English (en)

Writes a DOM structure as XML data into a file or stream. It can deal both with XML files and XML fragments.

Declarations

Declarations excerpted from XMLWrite.pas.

procedure WriteXMLFile(doc: TXMLDocument; const AFileName: String); overload;
procedure WriteXMLFile(doc: TXMLDocument; var AFile: Text); overload;
procedure WriteXMLFile(doc: TXMLDocument; AStream: TStream); overload;

procedure WriteXML(Element: TDOMNode; const AFileName: String); overload;
procedure WriteXML(Element: TDOMNode; var AFile: Text); overload;
procedure WriteXML(Element: TDOMNode; AStream: TStream); overload;

Encodings

At the moment it supports only the UTF-8 output encoding. The encoding label is not written because it is optional for UTF-8.

Character escaping

The writer provides proper character escaping as required by XML:

  • for normal text nodes, the following replacements will be done:
    • '<' => '&lt;'
    • '>' => '&gt;'
    • '&' => '&amp;'
  • For attribute values, additionally '"' (#34) gets replaced by '&quot;', and characters #9, #10 and #13 are escaped using numerical references.
  • Single apostrophes (') don't need to get converted, as values are already written using "" quotes.

The XML reader (in xmlread.pp) will convert these entity references back to their original characters.

Exceptions

Will raise EConvertError if any DOM node contains an unpaired UTF-16 surrogate in its name or value.

End-of-line handling

At the moment always uses line endings which are default for the target platform (#13#10 for Windows, #10 for Linux). Moreover, any #13, #10 or #13#10 in input data is treated as a single line-ending.

Notes

  • The sets of WriteXML() and WriteXMLFile() procedures duplicate each other, one of them is actually redundant.


Back to fcl-xml overview.


See also