Personal Information Portal - a.k.a Blog
December 6, 2002
by Sergey Kucherov
There are several ways you can use XML in your Delphi programs. You can download Microsoft XML Core Services library, which supports the W3C final recommendation for XML Schema, with both DOM and SAX. You may use one of the open-source libraries like Open XML or even write your own object model and parser (see my articles).
In any case you have make-up your mind to choose an ugly, fat, inadequate, and HTML-oriented Document Object Model (DOM) or to rely on a proprietary object interface. If you more attracted by the second choice, then XMLDATA library is for you.
XMLDATA - is Delphi library, which implements simple XML parser and XML data class. My goal is to keep the interface simple and flexible. The library utilizes features of the Object Pascal language to simplify access to XML data.
Starting from version 1.1 XMLDATA supports both internal and Microsoft SAX interfaces. If you are planning to use the Microsoft Parser with XMLDATA library, then do not forget to distribute the MS Core XML Services files with your application.
Each element of an XML document is represented by instance of TxdataNode class. XML node has embedded parser implemented as a single property XML:
Node := TxdataNode.Create; Node.XML := Memo1.Lines.Text;
To provide flexible access to XML data the library implements a subset of XPath interface. Use path string to define position of an element inside XML document. For example, to display the employee position title use the following syntax:
D.Value['Employee/Position/Title'];
Whenever you access elements by path TxdataNode returns the first element of specified name. If you need to work with a set of elements, then you can use ForEach statement:
procedure SendResume(X: TxdataNode);
...
Node.SortChildren('ID',True);
D := Node['Companies[@ID="1234"]'];
D.ForEach('HR/Contacts','Address',SendResume);
Because of XML text nature all elements can contain is text. The XML data library provides a developer with data conversion methods. The interface supports Integer, Float, and TDateTime types.
Sometimes we have to consider the performance issue when selecting appropriate XML tools. If your project requirements include processing of high-volume XML data, then you want to use fastest parser available.
The following test compares four parsers we have discussed in this paper. The testing program calculated time a parser spent to read 37 Plays of Shakespeare encoded in XML format*. The total volume of XML data is 7.29 MB or 249,986 lines. The results of this test displayed in the following table:
| Parser | Total time (sec) | Lines per second |
| XDATA native | 5.207 | 48,012 |
| XDATA with MSXML SAX | 6.850 | 36,496 |
| MSXML4 (DOM) | 4.576 | 54,632 |
| Open XML (DOM) | 20.059 | 12,463 |
For those of you, who is wondering what have happened with old FxOM library I have bad news. It is gone and I do not support it anymore. You can use XMLDATA instead. The interface is much more flexible and it is still an open source. Here the overview of the changes:
1) XDATA is compatible with Delphi 4, Delphi 5, and Delphi 6. The Contnrs unit is removed by replacing TobjectList with TList class.
2) Single unit CxmlData contains both parser and XML data routines. An embedded XML parser is implemented as XML property.
3) Attributes are separate entities in XDATA library. You can add or delete attributes but you cannot "convert" element to an attribute type.
4) Types conversion is significantly simplified. XDATA does not use Variants. xtBoolean and xtAttribute types are gone.