TXMLDocument Class: XML tool for Delphi7

xiaoxiao2021-03-05  37

Delphi7 supports the operation of the XML document, can be read and written to the XML document through the TXMLDocument class. You can read the XML document in memory using TXMLDocument to edit and save operations. The TXMLDocument class is an interface through the DOM (Document Object Model) interface to access the individual elements in the XML document. There are a variety of ways for the DOM interface, and the way Delphi supports:

1) Microsoft's MSXML SDK, this way is achieved by COM objects;

2) APACHE 's Xerces implementation;

3) The other is open source OpenXML implementation. Control can be made by setting the TXMLDocument DomvenDER for different interface implementations.

Supported XML Delphi units mainly exist with ... / borland / delphi7 / source / xml directory, including: XMLINTF, XMLDOC, XMLDOM, MSXMLDOM, XerceSxmldom, xdom, oxmldom and other units.

· XMLINTF - includes the interface of the XML document defined by Borland;

• Xmldoc - is a Borland implementation for the interface defined in XMLINTF;

· XMLDOM - defines the DOM (Document Object Model) interface, here the DOM interface is implemented by Borland;

· Msxmldom - Implement Microsoft's implementation of the interface defined in XMLDOM, mainly calling Microsoft's COM object to implement, defining the package in XMLDOM;

Xercesxmldom - Borland implements the encapsulation of the interface defined in XMLDOM by Xerces XML DOM; l oxmldom - Borland implements the encapsulation of the interface defined in XMLDom by using OpenXML;

For the property of the TXMLDocument class, please refer to the help file for Borland;

Read and write XML documents

· Read XML documentation

Normally, the XML file is not performed by directing the TXMLDocument object directly using the TXMLDocument object, but using several useful functions provided in the XMLDoc unit, these functions 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 of these functions returned to the IXMLDocument interface, get the IXMLDocument interface in the document; these functions are read by creating a TXMLDocument object; where NEWXMLDocument only creates an IXMLDocument interface. You can use newXmldocument to read the XML document:

Xmldoc: = newxmldocument; xmldoc.loadfromfile (filename); • Save XML documentation can save XML documents by the following manner:

XMLDoc: = newxmldocument; iroot: = ixmldoc.createnode ('testxmldocument'); xmldoc.documentElement: = Iroot; ... XmLDoc.savetofile (filename); It can be seen that the XML document is easy to operate through the interface; select different types The XML parsing has been mentioned in three ways to implement DOM, which is to apply 3 different XML parsers provided by Borland to parse XML documents; · Three parser 1, Microsoft parser (MSXML SDK) Microsoft The parser is primarily applied to Windows. When installing MSXML SDK, the parser is installed, and the IE browser also provides a parser, which is a COM. 2, Apache's Xercept parser Borland implements an Xerces parser, which can be implemented by calling the XerceSxmLDom.dll module; if using this parser may need to distribute XercesXmldom.dll, Xerceslib.dll, CC3260MT with the application with the application. DLL three DLL files 3, OpenXML parser This parser source code exists in xdom.pas unit, this can be downloaded from http://www.philo.de/xml/, this is a German writing XML parser; • Comparison of different parsers Compare the parsers in three ways are as follows: 1, Microsoft's parser Microsoft's parser is of course good, but can not exclude the existence of accidents, in my personal experience In the way, at least our company's way of XML parsing can only work normally above IE6.0; as for Borland, it is also achieved by introducing MSXml.dll interface, so it can be reasonably existing. Question; This can be proved by studying the implementation of TMSDomimplementation (MSXMLDOM cell) to achieve parsing by calling the CocreateInstance function interface; when the code is released, there may be this version of IE when issuing an XML code. Different, you need to release IE6.0, more trouble; 2, Borland's Xercept parser This method of ways is through LoadLibrary (Pchar (libname)); function, libName content is Xercesxmldom.dll (Windows platform), LIBXERCESXMLDOM.SO.1 (Linux platform). Then you need the DLL that will be released along with the application, it includes XerceSxmLDom.dll, XercesLib.dll, CC3260MT.dll; this release is relatively simple relative to the release of different versions of IE6.0; 3. OpenXML parser There is an xdom.pas unit that includes a complete XML parsing source code, then applies this method to avoid the problem of software release, which is due to the resolution of the code is static in the application inside the application. The only thing is that the size of the application may be large; • How to use different parsers We can write a function to use different parsers;

function NewDiffXmlDocument (DOMVender: string; Version: DOMString = '1.0'): IXMLDocument; varXMLDoc: TXMLDocument; beginXMLDoc: = TXMLDocument.Create (nil); XMLDoc.DOMVendor: = GetDOMVendor (DOMVender); Result: = XMLDoc; Result.Active : = True; if Version: = Version; END; where Domvender is parsed if Domvender is parsed in three ways provided by Borland, the value is: Microsoft - SMSXML in MSXMLDom.Pas unit. constant;

XERCES - SolicsXML constant in XercesXmldom .Pas unit;

OpenXML - SopenXML constant in oxmldom.Pas unit;

This is due to the INITAILIZATION section of MSXmldom, XerceSxmldom, Oxmldom, is registered with different parser interfaces by calling the RegisterDomvendor function; of course, Borland also provides a mechanism that can be flexibly expanded to extend the user's own resolution. This needs to be inherited, and the TDomvendor class (existing in the XMLDOM unit). In fact, Borland himself achieves different ways of parsers in this way; the specific implementation process can be encapsulated in the OXMLDOM cell.

in conclusion

As a successful development tool, Delphi, its own support for XML, certainly more stable, efficient than some of the network, we don't have to carry out another package of MsXml.dll. COM interface. Of course, you can achieve different XML parsers you can apply existing parsers. At the same time, it can be seen that the support of Delphi for XML is also very complete.

转载请注明原文地址:https://www.9cbs.com/read-33259.html

New Post(0)