Delphi7 support analysis of xml zhj
Delphi7 support for XML ---TXMLDocument class
Delphi7 supports operations on XML documents, and can use the TXMLDocument class to read and write XML documents. You can use TXMLDocument to read XML documents into memory, so that you can edit and save operations. The TXMLDocument class accesses each element in an XML document through the DOM (Document Object Model) interface. There are many ways to implement the DOM interface. Delphi supports: 1) Microsoft's MSXML SDK, which is implemented through COM objects; 2) Apache's Xerces implementation method; 3) Another is open source OpenXML Implementation method. For different interface implementation methods, you can control it by setting the DOMVender of TXMLDocument. The Delphi units that support XML mainly exist in the directory …/Borland/Delphi7/Source/Xml, mainly including: XMLIntf, XMLDoc, xmldom, msxmldom, xercesxmldom, xdom, oxmldom and other units.
l XMLIntf—including the interface of the XML document defined by Borland; l XMLDoc—is a Borland implementation of the interface defined in XMLIntf; l Xmldom—defines the DOM (document object model) interface, where the DOM interface is performed Borland implementation; l Msxmldom—Implements Microsoft’s implementation of interfaces defined in Xmldom, mainly calling Microsoft’s COM object to implement them, encapsulating interfaces defined in Xmldom; l Xercesxmldom—Borland implements Xmldom through Xerces XML DOM encapsulation of interface defined in l Oxmldom—Borland uses OpenXML to implement encapsulation of interface defined in Xmldom;
For properties of TXMLDocument, please refer to Borland's help file;
Read and write XML documents
l Reading XML documents. Usually, instead of directly using the TXMLDocument object to read XML files, you use several useful functions provided in the XMLDoc unit to read XML documents, including: 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; These can be seen All functions return the IXMLDocument interface, which obtains the IXMLDocument interface for document operations; these functions all realize the reading of XML documents by creating a TXMLDocument object; among which NewXMLDocument only creates an IXMLDocument interface. You can use NewXMLDocument to read XML documents in this way: XMLDoc := NewXMLDocument; XMLDoc.LoadFromFile(FileName);
l Saving XML documents can be saved in the following ways:
XMLDoc := NewXMLDocument; iRoot := IXMLDoc.CreateNode('TestXMLDocument'); XMLDoc.DocumentElement := iRoot; … XMLDoc.SaveToFile(FileName); It can be seen that it is very convenient to operate XML documents through interfaces;
Choose different types of XML parsing methods
It has been mentioned above that there are three ways to implement DOM, that is, you can use three different XML parsers provided by Borland to parse XML documents; l Three parsers 1. Microsoft parser (MSXML SDK) Microsoft parser is mainly In Windows, a parser will be installed when installing the MSXML SDK. At the same time, the IE browser also provides a parser, which is a COM. 2. Apache's Xerces parser Borland implements an Xerces parser itself, which can be implemented by calling the xercesxmldom.dll module; if you use this parser, you may need to distribute xercesxmldom.dll, XercesLib.dll, CC3260MT together with the application. DLL three DLL files 3. OpenXML parser The source code of this parser exists in the xdom.pas unit. This can be downloaded via http://www.philo.de/xml/. This is written by a German. XML parser; l Comparison of different parsers for the three ways of parser comparison are as follows: 1. Microsoft parser Microsoft parser is of course good, but it cannot rule out the existence of unexpected situations, in my personal experience At least our company's XML parsing method can only work normally in IE6.0 or above; as for Borland, it is also implemented by introducing the MSXML.DLL interface, so it can be inferred that there is the same Problem; This can be proved by studying the implementation method of TMSDOMImplementation (msxmldom unit). During the implementation process, parsing is achieved by calling the CoCreateInstance function interface; when releasing code to parse XML, there may be any existence due to this version of IE. Different, IE6.0 needs to be released together, which is more troublesome; 2. Borland's Xerces parser parser is the LoadLibrary(PChar(LibName)); function. The content of LibName is xercesxmldom.dll (Windows platform). libxercesxmldom.so.1 (linux platform). Then the Dll that needs to be released with the application includes xercesxmldom.dll, XercesLib.dll, CC3260MT.DLL; this release is relatively simple compared to the release of different versions of IE6.0; 3. The OpenXML parser is because There is an xdom.pas unit, which contains the source code for complete XML parsing. Therefore, applying this method can avoid various problems of software release, because the parsed code is statically compiled inside the application. The only downside is that the app may be larger;
l How to use different parsers We can write a function to use different parsers; 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;
If DOMVender is parsed in the three methods provided by Borland, the values are: Microsoft - SMSXML constants existing in the msxmldom.pas unit; Xerces - SXercesXML constants existing in the xercesxmldom.pas unit; OpenXML - SOpenXML constants present in the oxmldom.pas unit;
This is because initalization parts of the three units msxmldom, xercesxmldom, and oxmldom are all registered with different parser interfaces by calling the RegisterDOMVendor function;
Of course, Borland also provides a mechanism that can flexibly extend to extend the user's own parser, which requires inheritance, the TDOMVendor class (existing in the xmldom unit). In fact, Borland itself implements parsers in different ways in this way; the specific implementation process can be through reference to the encapsulation of xdom in the oxmldom unit;
in conclusion
As a successful development tool, Delphi's own support for XML is definitely much more stable and efficient than some implementations on the network. We don't need to encapsulate any COM interface of MSXML.DLL. Of course, you can implement different XML parsers yourself, or you can apply existing parsers. At the same time, it can be seen that Delphi's support for XML is also very complete.
Contact information: [email protected]