Using the Document Object Model

From RAD Studio
Jump to: navigation, search

Go Up to Working with XML Documents Index


The Document Object Model (DOM) is a set of standard interfaces for representing a parsed XML document. These interfaces are implemented by a number of different third-party vendors. If you do not want to use the default implementation that ships with RAD Studio, there is a registration mechanism that lets you integrate additional XML vendors into the XML framework.

The Xml.xmldom unit includes declarations for all the DOM interfaces defined in the W3C XML DOM level 2 specification. Each XML vendor provides an implementation for these interfaces.

You can work directly with the DOM interfaces to parse and edit XML documents. Simply call the GetDOM function to obtain an IDOMImplementation interface, which you can use as a starting point.

Note: For detailed descriptions of the DOM interfaces, see the declarations in the Xml.xmldom unit, the documentation supplied by your XML vendor, or the specifications provided on the W3C web site (www.w3.org).

You may find it more convenient to use special XML classes rather than working directly with the DOM interfaces. These are described in:

List of Built-in XML Vendors

The following table shows a comparison of the XML vendors that RAD Studio supports by default:

Implementation Unit Global Variable Description

MSXML

Xml.Win.msxmldom

SMSXML

Fastest of the built-in RAD Studio XML vendors. Windows only. Default.

For cross-platform support, you must choose a different XML vendor. If you do not specify a different XML vendor, your application does not have XML support on other platforms than Windows, and you see a run-time exception when you run your application in other platforms.

OmniXML

Xml.omnixmldom

sOmniXmlVendor

Much faster than ADOM, but slightly slower than MSXML. Cross-platform.

ADOM

Xml.adomxmldom

sAdom4XmlVendor

Slower than the other built-in RAD Studio XML vendors. Cross-platform.

Selecting an XML Vendor

When you build an application, RAD Studio uses the default built-in XML vendor, MSXML.

If you do not specify a different XML vendor, your application does not have XML support on other platforms than Windows, and you see a run-time exception when you run your application in other platforms. For cross-platform applications, OmniXML is currently the best choice, as it shows much better performance than ADOM.

To choose a different XML vendor, add a reference to the unit of the vendor into the unit where you use the RTL XML features, such as the TXMLDocument class. If you add more than one XML vendor unit, the first unit referenced is used as XML vendor. If you need to override this behavior, change the value of the DefaultDOMVendor global variable to the global variable of the XML vendor that you want to use.

Note: You can look up the unit and the global variable of each XML vendor in the List of Built-in XML Vendors above.

When you use the TXMLDocument component, you can choose an XML vendor using its DOMVendor property. When you change the value of DOMVendor, the unit that uses the component is configured to use the specified XML vendor, so that you do not need to change unit references or the DefaultDOMVendor global variable manually.

Using a Different XML Vendor for each Platform

To use a different XML vendor for each platform (e.g. MSXML in Windows and OmniXML in other platforms) use conditional compilation:

For example, to enjoy the best possible performance on each platform, you can use the following code:

Delphi:

uses
    // …
    {$IFDEF MSWINDOWS}Xml.Win.msxmldom{$ELSE}Xml.omnixmldom{$ENDIF};

C++:

#ifdef MSWINDOWS
    #include <Xml.Win.msxmldom.hpp>
#else
    #include <Xml.omnixmldom.hpp>
#endif

Using a Custom XML Vendor

To use an XML vendor other than the built-in XML vendors listed above, you must create a unit that defines a descendant of the Xml.xmldom.TDOMVendor class. This unit can then work like one of the built-in XML vendors, making your XML vendor available when it is included in a project.

In your descendant class, you must override two methods: the Description method, which returns a string identifying the vendor, and the DOMImplementation method, which returns the top-level interface (IDOMImplementation).

Your new unit must call the global RegisterDOMVendor procedure to register your XML vendor. This call typically goes in the initialization section of the unit.

When your unit is unloaded, it needs to unregister itself to indicate that your XML vendor is no longer available. Call the global UnRegisterDOMVendor procedure to unregister your XML vendor. This call typically goes in the finalization section of the unit.

Some XML vendors supply extensions to the standard DOM interfaces. To allow you to use these extensions, the Xml.xmldom unit also defines an IDOMNodeEx interface. IDOMNodeEx is a descendant of the standard IDOMNode that includes the most useful of these extensions.

See Also