📄 nativexml.pas
字号:
// Call SaveToStream to save the XML document to the Stream. Stream
// can be any TStream descendant. Set XmlFormat to xfReadable if you want
// the stream to contain indentations to make the XML more human-readable. This
// is not the default and also not compliant with the XML specification. See
// SaveToFile for information on how to save in special encoding.
procedure SaveToStream(Stream: TStream); virtual;
// Call SaveToFile to save the XML document to a file with FileName. If the
// filename exists, it will be overwritten without warning. If the file cannot
// be created, a standard I/O exception will be generated. Set XmlFormat to
// xfReadable if you want the file to contain indentations to make the XML
// more human-readable. This is not the default and also not compliant with
// the XML specification.<p>
// Saving to special encoding types can be achieved by setting two properties
// before saving:
// * ExternalEncoding
// * EncodingString
// ExternalEncoding can be se8bit (for plain ascii), seUtf8 (UTF-8), seUtf16LE
// (for unicode) or seUtf16BE (unicode big endian).<p> Do not forget to also
// set the EncodingString (e.g. "UTF-8" or "UTF-16") which matches with your
// ExternalEncoding.
procedure SaveToFile(const FileName: string); virtual;
// Call WriteToString to save the XML document to a string. Set XmlFormat to
// xfReadable if you want the string to contain indentations to make the XML
// more human-readable. This is not the default and also not compliant with
// the XML specification.
function WriteToString: string; virtual;
// Set AbortParsing to True if you use the OnNodeNew and OnNodeLoaded events in
// a SAX-like manner, and you want to abort the parsing process halfway. Example:
// <code>
// procedure MyForm.NativeXmlNodeLoaded(Sender: TObject; Node: TXmlNode);
// begin
// if (Node.Name = 'LastNode') and (Sender is TNativeXml) then
// TNativeXml(Sender).AbortParsing := True;
// end;
// </code>
property AbortParsing: boolean read FAbortParsing write FAbortParsing;
// Choose what kind of binary encoding will be used when calling TXmlNode.BufferRead
// and TXmlNode.BufferWrite. Default value is xbeBase64.
property BinaryEncoding: TBinaryEncodingType read FBinaryEncoding write FBinaryEncoding;
// A comment string above the root element \<!--{comment}--\> can be accessed with
// this property. \Assign a comment to this property to add it to the XML document.
// Use property RootNodeList to add/insert/extract multiple comments.
property CommentString: string read GetCommentString write SetCommentString;
// Set DropCommentsOnParse if you're not interested in any comment nodes in your object
// model data. All comments encountered during parsing will simply be skipped and
// not added as a node with ElementType = xeComment (which is default). Note that
// when you set this option, you cannot later reconstruct an XML file with the comments
// back in place.
property DropCommentsOnParse: boolean read FDropCommentsOnParse write FDropCommentsOnParse;
// Encoding string (e.g. "UTF-8" or "UTF-16"). This encoding string is stored in
// the header.
// Example: In order to get this header:
// <?xml version="1.0" encoding="UTF-16" ?>
// enter this code:
// <CODE>MyXmlDocument.EncodingString := 'UTF-16';</CODE>
// When reading a file, EncodingString will contain the encoding used.
property EncodingString: string read GetEncodingString write SetEncodingString;
// Returns the value of the named entity in Name, where name should be stripped
// of the leading & and trailing ;. These entity values are parsed from the
// Doctype declaration (if any).
property EntityByName[AName: string]: string read GetEntityByName;
// ExternalEncoding defines in which format XML files are saved. Set ExternalEncoding
// to se8bit to save as plain text files, to seUtf8 to save as UTF8 files (with
// Byte Order Mark #EF BB FF) and to seUTF16LE to save as unicode (Byte Order
// Mark #FF FE). When reading an XML file, the value of ExternalEncoding will
// be set according to the byte order mark and/or encoding declaration found.
property ExternalEncoding: TStringEncodingType read FExternalEncoding write FExternalEncoding;
// IndentString is the string used for indentations. By default, it is two
// spaces: ' '. Set IndentString to something else if you need to have
// specific indentation, or set it to an empty string to avoid indentation.
property IndentString: string read FIndentString write FIndentString;
// Root is the topmost element in the XML document. Access Root to read any
// child elements. When creating a new XML document, you can automatically
// include a Root node, by creating using CreateName.
property Root: TXmlNode read GetRoot;
// RootNodeList can be used to directly access the nodes in the root of the
// XML document. Usually this list consists of one declaration node followed
// by a normal node which is the Root. You can use this property to add or
// delete comments, stylesheets, dtd's etc.
property RootNodeList: TXmlNode read FRootNodes;
// Set or get the stylesheet string used for this XML document.
property StyleSheetString: string read GetStyleSheetString write SetStyleSheetString;
// Set UseFullNodes to True before saving the XML document to ensure that all
// nodes are represented by <Node>...</Node> instead of the short version
// <Node/>. UseFullNodes is False by default.
property UseFullNodes: boolean read FUseFullNodes write FUseFullNodes;
// When Utf8Encoded is True, all strings inside the document represent
// UTF-8 encoded strings. Use function ToWidestring to convert strings to
// widestring (without loss) or ToAnsiString to convert to ANSI string
// (with loss). When Utf8Encoded is False (default), all strings represent
// normal ANSI strings. Set Utf8Encoded to True before adding info to the XML
// file to ensure internal strings are all UTF-8. Use methods FromWidestring,
// sdAnsiToUTF8 or sdUnicodeToUtf8 before setting any strings in that case.
property Utf8Encoded: boolean read FUtf8Encoded write FUtf8Encoded;
// After reading, this property contains the XML version (usually "1.0").
property VersionString: string read GetVersionString write SetVersionString;
// Set WriteOnDefault to False if you do not want to write default values to
// the XML document. This option can avoid creating huge documents with
// redundant info, and will speed up writing.
property WriteOnDefault: boolean read FWriteOnDefault write FWriteOnDefault;
// XmlFormat by default is set to xfCompact. This setting is compliant to the spec,
// and XmlDocument will only generate XML files with #$0A as control character.
// By setting XmlFormat to xfReadable, you can generate easily readable XML
// files that contain indentation and carriage returns after each element.
property XmlFormat: TXmlFormatType read FXmlFormat write FXmlFormat;
// ParserWarnings by default is True. If True, the parser will raise an
// exception in cases where the XML document is not technically valid. If False,
// the parser will try to ignore non-critical warnings. Set ParserWarnings
// to False for some types of XML-based documents such as SOAP messages.
property ParserWarnings: boolean read FParserWarnings write FParserWarnings;
// SortAttributes by default is set to False. Attributes will appear in the
// String list in the same order that they appear in the XML Document. Setting
// this to true will cause the TStringList that holds the attributes to be sorted
// This can help speed lookup and allow you to iterate the list looking for
// specific attributes.
property SortAttributes: boolean read FSortAttributes write FSortAttributes;
// This event is called whenever a node's SortChildNodes method is called and
// no direct compare method is provided. Implement this event if you want to
// use object-event based methods for comparison of nodes.
property OnNodeCompare: TXmlNodeCompareEvent read FOnNodeCompare write FOnNodeCompare;
// This event is called whenever the parser has encountered a new node.
property OnNodeNew: TXmlNodeEvent read FOnNodeNew write FOnNodeNew;
// This event is called when the parser has finished parsing the node, and
// has created its complete contents in memory.
property OnNodeLoaded: TXmlNodeEvent read FOnNodeLoaded write FOnNodeLoaded;
// OnProgress is called during loading and saving of the XML document. The
// Size parameter contains the position in the stream. This event can be used
// to implement a progress indicator during loading and saving. The event is
// called after each node that is read or written.
property OnProgress: TXmlProgressEvent read FOnProgress write FOnProgress;
// This event is called if there is a warning for unicode conversion loss,
// when reading from Unicode streams or files.
property OnUnicodeLoss: TNotifyEvent read FOnUnicodeLoss write FOnUnicodeLoss;
end;
// This enumeration defines the conversion stream access mode.
TsdStreamModeType = (
umUnknown, // The stream access mode is not yet known
umRead, // UTF stream opened for reading
umWrite // UTF stream opened for writing
);
// TBigByteArray is an array of bytes like the standard TByteArray (windows
// unit) but which can contain up to MaxInt bytes. This type helps avoiding
// range check errors when working with buffers larger than 32768 bytes.
TBigByteArray = array[0..MaxInt - 1] of byte;
PBigByteArray = ^TBigByteArray;
{$IFDEF CLR}
// not implemented
TsdBufferedStream = class(TStream)
private
FStream: TStream;
FOwned: Boolean;
protected
procedure SetSize(NewSize: Int64); override;
public
constructor Create(AStream: TStream; Owned: Boolean = False);
destructor Destroy; override;
function Read(var Buffer: array of Byte; Offset, Count: Longint): Longint; override;
function Write(const Buffer: array of Byte; Offset, Count: Longint): Longint; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
end;
TsdBufferedReadStream = TsdBufferedStream;
TsdBufferedWriteStream = TsdBufferedStream;
{$ELSE}
// TsdBufferedReadStream is a buffered stream that takes another TStream
// and reads only buffer-wise from it, and reads to the stream are first
// done from the buffer. This stream type can only support reading.
TsdBufferedReadStream = class(TStream)
private
FStream: TStream;
FBuffer: PBigByteArray;
FPage: integer;
FBufPos: integer;
FBufSize: integer;
FPosition: longint;
FOwned: boolean;
FMustCheck: boolean;
protected
procedure CheckPosition;
public
// Create the buffered reader stream by passing the source stream in AStream,
// this source stream must already be initialized. If Owned is set to True,
// the source stream will be freed by TsdBufferedReadStream.
constructor Create(AStream: TStream; Owned: boolean{$IFDEF D4UP} = False{$ENDIF});
destructor Destroy; override;
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: Word): Longint; override;
end;
// TsdBufferedWriteStream is a buffered stream that takes another TStream
// and writes only buffer-wise to it, and writes to the stream are first
// done to the buffer. This stream type can only support writing.
TsdBufferedWriteStream = class(TStream)
private
FStream: TStream;
FBuffer: PBigByteArray;
FBufPos: integer;
FPosition: longint;
FOwned: boolean;
protected
procedure Flush;
public
// Create the buffered writer stream by passing the destination stream in AStream,
// this destination stream must already be initialized. If Owned is set to True,
// the destination stream will be freed by TsdBufferedWriteStream.
constructor Create(AStream: TStream; Owned: boolean{$IFDEF D4UP} = False{$ENDIF});
destructor Destroy; override;
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: Word): Longint; override;
end;
{$ENDIF}
// TsdCodecStream is the base codec class for reading and writing encoded files.
// See TsdAnsiStream and TsdUtf8Stream for more information.
TsdCodecStream = class(TStream)
private
FBuffer: string; // Buffer that holds temporary utf8 characters
FBufferPos: integer; // Current character in buffer
FEncoding: TstringEncodingType; // Type of string encoding used for the external stream
FMode: TsdStreamModeType; // Access mode of this UTF stream, determined after first read/write
FPosMin1: integer; // Position for seek(-1)
FPosMin2: integer; // Position for seek(-2)
FStream: TStream; // Referenced stream
FSwapByteOrder: boolean;
FWarningUnicodeLoss: boolean; // There was a warning for a unicode conversion loss
FWriteBom: boolean;
FOnUnicodeLoss: TNotifyEvent; // This event is called if there is a warning for unicode conversion loss
protected
function ReadByte: byte; virtual;
procedure StorePr
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -