Delphi's support for XML --- txmldocument class
Delphi7 supports operations on XML documents. You can use the txmldocument class to read and write XML documents. You can use txmldocument to read XML documents to the memory for editing and saving. The txmldocument class uses the DOM (Document Object Model) interface to access each element in the XML document. There are multiple methods to implement the DOM interface. Delphi supports the following methods: 1) Microsoft's msxml sdk, which is implemented through the COM object; 2) Apache's xerces implementation method; 3) The other is the Open Source openxml implementation method. You can set the domvender of txmldocument to control different interface implementation methods.
The Delphi unit supporting XML mainly exists... The/Borland/Delphi7/source/XML directory contains the following elements: xmlintf, xmldoc, xmldom, msxmldom, xercesxmldom, xdom, and oxmldom.
L xmlintf-including interfaces for borland's custom XML documents;
L xmldoc-is the Borland Implementation of interfaces defined in xmlintf;
L xmldom-defines the DOM (Document Object Model) interface, where Borland is implemented for the DOM interface;
L msxmldom-implements Microsoft's implementation of interfaces defined in xmldom, mainly by calling Microsoft's COM Object and encapsulating interfaces defined in xmldom;
L xercesxmldom-Borland encapsulates the interface defined in xmldom through xerces xml dom;
L oxmldom-Borland uses openxml to encapsulate interfaces defined in xmldom;
For the attributes of the txmldocument class, see the Borland help file;
Read and Write XML documents
L read XML documents
Generally, you do not directly use the txmldocument object to read XML files. Instead, you can use several useful functions provided in the xmldoc unit to read XML documents. 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 these functions return the ixmldocument interface, and the ixmldocument interface is used for document operations;
These functions read XML documents by creating txmldocument objects. newxmldocument only creates an ixmldocument interface.
Newxmldocument can be used to read XML documents as follows:
Xmldoc: = newxmldocument;
Xmldoc. loadfromfile (filename );
L save the XML document
You can save the XML document as follows:
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;
Select different types of XML parsing Methods
As mentioned above, there are three ways to implement Dom, that is, you can use three different XML Parser provided by Borland to parse XML documents;
L three Resolvers
1. Microsoft Parser (msxml sdk)
The Microsoft parser is mainly used in Windows. The parser is installed when msxml sdk is installed, and the IE browser also provides the parser, which is a COM.
2. Apache xerces parser
Borland implements an xerces parser by calling xercesxmldom. DLL module; if this parser is used, it may need to distribute xercesxmldom together with the application. DLL, xerceslib. DLL, cc3260mt. three DLL files
3. openxml parser
The source code of this parser is stored in the xdom. Pas unit. You can download the source code from http://www.philo.de/xml/. this is a German xmlreader;
L Comparison of Different Resolvers
Comparison of Three Types of parser is as follows:
1. Microsoft parser
Of course, Microsoft's parser is good, but it cannot eliminate unexpected situations. In my personal experience, at least our company's XML parsing method, only Versions later than ie6.0 can work normally;
Borland also introduces MSXML. DLL interfaces, so we can infer that the same problem exists. This is proved by studying the implementation of tmsdomimplementation (in the msxmldom unit, during implementation, the cocreateinstance function interface is called for resolution;
When releasing the XML parsing code, it may be difficult to publish ie6.0 together because of the different ie versions;
2. Borland's xerces parser
In this way, the parser is through loadlibrary (pchar (libname); function, libname content is xercesxmldom. dll (Windows Platform), libxercesxmldom. so.1 (Linux platform ). The dll that needs to be released along with the application includes xercesxmldom. dll, xerceslib. dll, and cc3260mt. dll;
This release is relatively simpler than ie6.0 of different versions;
3. openxml parser
Because xdom exists. the PAS unit, which contains the source code completely parsed by XML, can be used to avoid various problems of software release, this is because the parsed code is statically compiled within the application. The only bad thing is that the application size may be larger;
L how to use different Resolvers
We can write a function to use different Resolvers;
Function newdiffxmldocument (domvender: string;
Version: domstring = '1. 0'): ixmldocument;
VaR
Xmldoc: txmldocument;
Begin
Xmldoc: = txmldocument. Create (NiL );
Xmldoc. domvendor: = getdomvender (domvender );
Result: = xmldoc;
Result. Active: = true;
If version <> ''then
Result. Version: = version;
End;
If domvender is parsed using the three methods provided by Borland, the values are:
Microsoft-smsxml constant in msxmldom. Pas unit;
Xerces-The sxercesxml constant in the xercesxmldom. Pas unit;
Openxml-sopenxml constant in the oxmldom. Pas unit;
This is because the initailization section of the msxmldom, xercesxmldom, and oxmldom units has registered different parser interfaces by calling the registerdomvendor function;
Of course, Borland also provides a flexible extension mechanism to expand the user's own parser, which requires inheritance, the tdomvendor class (exists in the xmldom unit ). In fact, Borland implements the parser in different ways through this method. For specific implementation process, refer to the xdom encapsulation in oxmldom units;
Conclusion
Delphi, as a successful development tool, must implement more stable and efficient XML support than some implementations on the network, we do not need to encapsulate MSXML. dll com interface. Of course, you can implement different XML parsers by yourself, or you can apply existing parsers. At the same time, we can see that Delphi's support for XML is also very complete.