Welcome to the second part of a four-part series on Delphi and the DOM. In the first article, we discussed Delphi and XML. Now that you have some idea of the core elements of XML documents, we can talk about how to manipulate them in Delphi.
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: