2008. március 23., vasárnap
Importing XML DOM Parser in Delphi
Problem/Question/Abstract:
How to import XML DOM Parser component in Delphi?
Answer:
One of the new features of Delphi 5 is that we can install COM servers as components in Delphi IDE. There are already some COM servers installed on the Servers Palette like Microsoft word, Excel etc., Other than that, the user can install other COM servers through the Project | Import Type library. This will create a wrapper class for that component. We can use that wrapper class to build a package in Delphi and install as component in the IDE. This is really a great and cool feature in Delphi 5.
This option will be very helpful when you want to use a COM component written in other languages. You just need to import them through this Import Type Library option. You can access the methods/properties of that COM component from Delphi 5.
I used msxml.dll(Version 2.0) to install the XML parsing components in the IDE through the Import Type Library option.
Steps to import XML DOM parser component:
Go to Project|Import Type Library(See Fig. 1. Below)
Select Microsoft XML, version 2.0(Version 2.0)
Then choose Create Unit
This will create a wrapper class for that parser in a pascal file
You can put that pascal file in a package and install it.
You will be getting a set of components installed on the ActiveX palette in Delphi
Among them will be the component called DOMDocument
Now you can use the methods of that component to parse the XML. There are some more components like OMFreeThreadedDocument,XMLHTTPRequest,XMLDSOControl and XMLDocument.
There are two ways to load the XML into that component to parse.
1. Loading the XML as a string:
You can use the “loadXML” method to load a XML string
For eg. DOMDocument.loadXML(‘XML string’);
2. Loading the XML as a file:
You can use the “load” method to load a XML file.
For eg. DOMDocument.load(‘Path of the XML file’)
Once you load either the XML string or the XML file into that component, the XML will be parsed and if there are any errors during parsing, then those errors will be intimated to the user by the way of exception.
You can check the place where you get the parsing error and also the reason for that.
Finding the place where the parsing error occurred:
DOMDocument.parseError.srcText will give you the exact line in the XML where the error occurred.
The above two are really helpful in diagnosing the parsing errors.
Getting the reason for the parsing error:
DOMDocument.parseError.reason will tell you the reason for that parsing error.
If there are no errors reported by the parser, then we can get the parsed data through the methods like getElementsByTagName, Get_nodeName, Get_nodeValue etc.,
The DTD file used in that XML should be in the search path of the application or should be in the path where the exe resides. Only then the application will be able to see DTD info used in that XML file or string and parse correctly.
Important:
So when we deploy an application in a fresh machine, we should not forget to include this DTD file in the same path as the exe is.
Please make sure all the XML string/XML file you are sending to that component follow the DTD mentioned in that DTD file. Even a small spelling mistake or an extra letter will cause an exception.
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése