Delphi and the DOM - Coding with the DOM in Delphi
(Page 2 of 4 )
The tree-like structure of an XML document makes it a fantastic fit in memory; this is exactly what the DOM is good for. It is a standard interface, so when you have written code that uses a DOM, you can switch DOM implementations without changing your source code. One of the most commonly used DOM engines on Windows is the one provided by Microsoft as part of the MSXML SDK, and also installed by Internet Explorer and many other Microsoft applications.Other DOM engines that are available in Delphi7 include Apache Foundation's Xerces and the open-source OpenXML.
Delphi embeds the DOM implementation into a component called XMLdocument. The idea behind using this component is that you remain independent from the implementation and can work with some simplified methods or helpers. You will probably want to work with these because the DOM interface is very complex to use.
A document is a collection of nodes, each having a name, a text element, a collection of attributes, and a collection of child nodes. Each collection of nodes lets you access elements by position or search for them by name. Notice that the text within the tags of a node, if any, is rendered as a child of that node and listed in its collection of child nodes. The root nodes have some extra methods for creating new nodes, values or attributes.
Delphi's XMLDocument components gives you two levels to work on:
At the lower level you are able to use the DOMDocument property to access a standard W3C Document Object Model Interface. The Official DOM is defined in the xmldom unit and includes interfaces such as IDOMNode, IDOMNodeList, IDOMAttr, IDOMElement, and IDOMText. With these DOM interfaces, Delphi supports a lower-level, but standard programming model. The DOM implementation is indicated by the XMLDocument component in the DOMVendor property.
As a higher-level alternative, the XMLDocument component also implements the IXMLDocument interface. This is a custom DOM-like API defined by Borland in the XMLintf unit, and comprising interfaces like XMLNode, XMLNodeList, and XMLNodeCollection. This Borland interface simplifies some of the DOM operations by replacing multiple method calls, which are repeated often in sequence, with a single property or method.
Below is a partial listing of the XMLDOM.pas unit:
Unit xmldom;
interface
uses
{$IFDEF MSWINDOWS}
Windows, ActiveX,
{$ENDIF}
{$IFDEF LINUX}
Types,
{$ENDIF}
SysUtils, Variants, Classes, XMLConst;
const
{ Wrapper Versioning }
DOMWrapperVersion = 1.2;
{ NodeType Values }
ELEMENT_NODE = 1;
ATTRIBUTE_NODE = 2;
TEXT_NODE = 3;
CDATA_SECTION_NODE = 4;
ENTITY_REFERENCE_NODE = 5;
ENTITY_NODE = 6;
PROCESSING_INSTRUCTION_NODE = 7;
COMMENT_NODE = 8;
DOCUMENT_NODE = 9;
DOCUMENT_TYPE_NODE = 10;
DOCUMENT_FRAGMENT_NODE = 11;
NOTATION_NODE = 12;
{ ExceptionCode Values }
INDEX_SIZE_ERR = 1;
DOMSTRING_SIZE_ERR = 2;
HIERARCHY_REQUEST_ERR = 3;
WRONG_DOCUMENT_ERR = 4;
INVALID_CHARACTER_ERR = 5;
NO_DATA_ALLOWED_ERR = 6;
NO_MODIFICATION_ALLOWED_ERR = 7;
NOT_FOUND_ERR = 8;
NOT_SUPPORTED_ERR = 9;
INUSE_ATTRIBUTE_ERR = 10;
INVALID_STATE_ERR = 11; { DOM Level 2 }
SYNTAX_ERR = 12; { DOM Level 2 }
INVALID_MODIFICATION_ERR = 13; { DOM Level 2 }
NAMESPACE_ERR = 14; { DOM Level 2 }
INVALID_ACCESS_ERR = 15; { DOM Level 2 }
const
NSDelim = ':';
SXML = 'xml';
SVersion = 'version';
SEncoding = 'encoding';
SStandalone = 'standalone';
SXMLNS = 'xmlns';
SHttp = 'http:/';
SXMLNamespaceURI = SHttp+'/www.w3.org/2000/xmlns/';
SXMLPrefixNamespaceURI = SHttp+'/www.w3.org/XML/1998/namespace';
type
{ Misc. Types }
DOMNodeType = Word;
{$EXTERNALSYM DOMNodeType}
DOMString = WideString;
{$EXTERNALSYM DOMString}
DOMTimeStamp = Int64;
{ Forward Declarations }
IDOMImplementation = interface;
IDOMNode = interface;
IDOMNodeList = interface;
IDOMNamedNodeMap = interface;
IDOMCharacterData = interface;
IDOMAttr = interface;
IDOMElement = interface;
IDOMText = interface;
IDOMComment = interface;
IDOMCDATASection = interface;
IDOMDocumentType = interface;
IDOMNotation = interface;
IDOMEntity = interface;
IDOMEntityReference = interface;
IDOMProcessingInstruction = interface;
IDOMDocumentFragment = interface;
IDOMDocument = interface;
{ DOM Extensions }
IDOMNodeEx = interface;
IDOMPersist = interface;
IDOMParseError = interface;
{ DOMException }
DOMException = class(Exception)
public
code: Word;
end;
Next: XMLDOM.pas Unit continued >>
More Delphi-Kylix Articles
More By David Web