Analysis of DELPHI7 support for XML

Source: Internet
Author: User
Tags object constant functions implement interface object model string version
Xml

DELPHI7 support FOR XML---txmldocument class
DELPHI7 supports the operation of XML documents and can be used to read and write XML documents through the Txmldocument class. You can use Txmldocument to read an XML document into memory so that you can edit and save the operation. The Txmldocument class accesses individual elements of an XML document through the DOM (Document Object model) interface. For the implementation of the DOM interface in a variety of ways, Delphi supported the way: 1 Microsoft's MSXML SDK, this way is achieved through COM objects, 2 Apache xerces implementation; 3 The other is open source OPENXML implementation. The implementation of different interfaces can be controlled by setting Txmldocument Domvender.

XML-supported Delphi units mainly exist with the \borland\delphi7\source\xml directory, mainly including: Xmlintf,xmldoc,xmldom,msxmldom,xercesxmldom,xdom, Oxmldom and other units.

L Xmlintf―― includes interfaces to Borland XML documents that are defined by itself;

L Xmldoc―― is the Borland implementation of the interface defined in xmlintf;

L Xmldom―― defines the DOM (Document Object model) interface, where the DOM interface is Borland implemented;

L Msxmldom―― realizes Microsoft's implementation of the interface defined in XMLDOM, mainly calls Microsoft COM object to implement, and encapsulates the interface in XMLDOM;

L XERCESXMLDOM――BORLAND The encapsulation of the interface defined in XMLDOM by xerces the XML Dom method;

L OXMLDOM――BORLAND The encapsulation of the interface defined in XMLDOM by using OpenXML;

Txmldocument the properties of the class, refer to the Help file for the Borland.

Read and write XML documents
L Reading XML documents

Instead of reading XML files directly using the Txmldocument object, the XML document is read using several useful functions provided in the XmlDoc cell, which include:

function loadxmldocument (const filename:domstring): ixmldocument;

function LoadXMLData (const xmldata:domstring): ixmldocument; overload;

function LoadXMLData (const xmldata:string): ixmldocument; overload;

function newxmldocument (version:domstring = ' 1.0 '): ixmldocument;

It can be seen that all these functions return the Ixmldocument interface, and get the Ixmldocument interface in the operation of the document;

These functions are created by creating a Txmldocument object to read the XML document, where Newxmldocument only creates a ixmldocument interface.

You can use Newxmldocument to read XML documents in this way:

XmlDoc: = newxmldocument;

Xmldoc.loadfromfile (FileName);

L Save XML documents

You can save an XML document in the following ways:

XmlDoc: = newxmldocument;

Iroot: = Ixmldoc.createnode (' testxmldocument ');

Xmldoc.documentelement: = Iroot;

...

Xmldoc.savetofile (FileName);

It can be seen that the interface to manipulate XML documents is very convenient;

Choosing different types of XML parsing methods
It has been mentioned that there are three ways to implement DOM, that is, you can use the 3 different XML parsers provided by Borland to parse XML documents;

L Three kinds of parsers

1, Microsoft's parser (MSXML SDK)

The Microsoft parser is mainly used in Windows, the parser is installed when the MSXML SDK is installed, and the parser is provided by IE browser, which is a COM.

2, Apache Xerces Parser

Borland has implemented a xerces parser of its own, This can be achieved by invoking the Xercesxmldom.dll module, which may need to be distributed with the application Xercesxmldom.dll,xerceslib.dll,cc3260mt.dll three DLL files

3. OPENXML Parser

The source code for this parser exists in the Xdom.pas unit, which can be downloaded with the new http://www.philo.de/xml/, a German-written XML parser;

L use different parsers for comparison

For the three-way parser comparisons are as follows:

1, Microsoft's parser

Microsoft's parser is certainly good, but also can not exclude the existence of the accident, in my personal experience, at least our company for XML parsing mode, only in the IE6.0 version can work properly;

As for, the Borland is also implemented by introducing the MSXML.DLL interface, so it can be inferred that the same problem exists, and this can be proved by studying the implementation of Tmsdomimplementation (Msxmldom unit). In the process of realization, the CoCreateInstance function interface is invoked to realize the parsing;

In the release of parsing XML code may exist due to the different version of IE, the need to IE6.0 together to release, more trouble;

2, Borland xerces Parser

The parser in this way is through LoadLibrary (Pchar (Libname)), functions, libname content is Xercesxmldom.dll (Windows platform), Libxercesxmldom.so.1 (Linux platform). Then you need to publish the DLL along with the application, including the Xercesxmldom.dll,xerceslib.dll,cc3260mt.dll;

This release is relatively simple compared to the release of different versions of IE6.0;

3. OPENXML Parser

Because of the existence of the Xdom.pas unit, which contains the source code of the complete XML parsing, this approach avoids the problems of software publishing because the parsed code is statically compiled inside the application. The only downside is that the size of the application may be larger;

L How to use a different parser

We can write a function to use a different parser;

function Newdiffxmldocument (domvender:string;

version:domstring = ' 1.0 '): ixmldocument;

Var

Xmldoc:txmldocument;

Begin

XmlDoc: = Txmldocument.create (nil);

Xmldoc.domvendor: = Getdomvendor (Domvender);

Result: = xmldoc;

Result.active: = True;

If Version <> ' then

Result.version: = Version;

End

Where Domvender is parsed using the three methods provided by Borland, the values are:

The Smsxml constant in the Msxmldom.pas cell is microsoft――;

Xerces―― exists in the xercesxmldom. PAS unit sxercesxml constant;

The Sopenxml constant in the Oxmldom.pas cell is openxml――;

This is because in the initailization part of the Msxmldom,xercesxmldom,oxmldom three units, the different parser interfaces are registered by calling the Registerdomvendor function;

Of course, Borland also provides a flexible mechanism to extend the user's own parser, which needs to be inherited, Tdomvendor class (existing in the XMLDOM unit). In fact, Borland itself is the way to implement different ways parser, the specific implementation process can be referenced oxmldom unit of the Xdom encapsulation;

Conclusion
Delphi as a successful development tool, its own implementation of XML support, must be more stable than some of the implementation of the network, more efficient, we do not need to do another package of what MSXML.DLL COM interface. Of course, you can implement a different XML parser yourself, or you can apply a parser that already exists. At the same time, you can see that Delphi support for XML is also very perfect.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.