Delphi7 support analysis of XML
zhj
Delphi7 support for XML --- TXMLDocument class
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. For the implementation of the DOM interface, there are various ways, and the ways supported by Delphi are: 1) Microsoft's MSXML SDK, this way is implemented by COM objects; 2) Apache's XERCES implementation; 3) The other is open source OpenXML Method to realize. 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.
l XMLINTF - includes the interface of the XML document defined by Borland;
l Xmldoc - is a Borland implementation for the interface defined in XMLINTF;
l Xmldom - Defines the DOM (Document Object Model) interface, here the DOM interface is implemented by Borland;
l MsXmldom - Implement Microsoft's implementation of the interface defined in XMLDOM, mainly calling Microsoft's COM object to implement, and define the package in XMLDOM;
l XercesXmldom - Borland implements the package of defined interfaces in XMLDom through Xerces XML DOM.
l oxmldom - Borland implements the package of defined interfaces 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
l Read the XML document
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, got the IXMLDocument interface operation of the document;
These functions are implemented 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);
l Save an XML document
You can save an XML document in the following manner: xmldoc: = newxmldocument;
Iroot: = ixmldoc.createnode ('Testxmldocument');
XMLDoc.documentelement: = iroot;
...
XMLDoc.savetofile (filename);
It can be seen that it is very convenient to operate the XML document through an interface;
Select different types of XML analysis
The above has been mentioned that there are three ways to implement the DOM, which is to apply 3 different XML parsers provided by Borland to resolve the XML document;
l three parsers
1, Microsoft's parser (MSXML SDK)
Microsoft parsers are primarily applied to Windows, and the parser is installed when installing MSXML SDK, and the IE browser also provides a parser, which is a COM.
2, Apache's Xerces parser
Borland has implemented an Xercept parser, 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.dll, three DLL files with the application together.
3, OpenXML parser
The source code of this parser exists in the xdom.pas unit, which can be downloaded with new downloads via http://www.philo.de/xml/. This is a German XML parser;
l Comparison using different parsers
The parser for three ways is as follows:
1, Microsoft's parser
Microsoft's parsers are of course good, but they can't exclude the existence of accidents. In my personal experience, at least our company's way of analyzing XML is only available in IE6.0 or more.
As for Borland, Borland is also implemented by introducing the interface of MSXml.dll, so it can be reasonabated, the same problem is the same; this can be proved by studying the implementation of TMSDomimplementation (MSXMLDOM cell) can be proved, the implementation is called CoCreateInstance Function interface to implement parsing;
At the time of publishing the resolution XML code, there may be different IE, IE6.0 is needed, more troublesome;
2, Borland Xerces parser
The parser in this way is through loadLibrary (PCHAR (LIBNAME)); function, the content of libName is XerceSxmldom.dll (Windows platform), libxyercesxmldom.so.1 (Linux platform). Then you need the DLL released with the application, including XerceSxmldom.dll, Xerceslib.dll, CC3260MT.DLL;
This release is relatively simple relative to IE6.0 issued by different versions;
3, OpenXML parser
Due to the existence of XDom.Pas units, this unit contains a complete XML resolution source code, then applies this method to avoid various problems of software release, which is because the parsed code is static in the application inside. The only thing is that the volume of the application may be large;
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;
Where Domvender is parsed if Domvender is parsed in three ways provided by Borland, the value is:
Microsoft-SMSXML constants stored in msxmldom.pas units;
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 by calling the RegisterDomvendor function;
Of course, Borland also provides a mechanism that can be flexible to expand to expand the user's own parser, this requires inheritance, TDomvenDOR class (existing in XmLDom). In fact, Borland himself is to achieve different ways of parsers in this way; the specific implementation process can be encapsulated by XDOM by reference to the OXMLDOM unit;
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.
Contact: zhihongjie@126.com