views:

52

answers:

1

I have something like this:

uses MSXML2_TLB;
type TDocumentType = (dtOrder, dtInvoice, dtStatus, dtError); // And a few more, actually
function DetermineDocumentType(doc: IXMLDOMDocument2): TDocumentType;
...

It must do something simple: determine what kind of XML is contained in the doc parameter. The XML could be something like: <Order>...</Order>, <Invoice>...</Invoice>, <Status>...</Status>, <Error>...</Error> or something else. The internal structure of these files is very similar so the only reliable way to determine it's type by getting the first tag name from the XML. Doc.nodeName and Doc.baseName don't work. I could just read the XML as text, parsse it by code to determine the tagname but it's a dirty trick that I refuse to use. So I need a method which will return the tag name through proper methods of the IXMLDOMDocument2 interface. To make it worse, I have no control over the code outside this function and as a second limit, this root tag happens to be case insensitive, making a trick like using selectNode() unworkable. This happens to be a problem because of legacy code and bad decisions that have been made in the past by the previous developers. (Fortunately, only the root tag seems to be this weird in this project.) Btw, this function is to replace a similar function that would do well on the TheDailyWTF site. It previously would read the XML from the interface, convert it completely to uppercase and finally do a lot of pos() function calls to check for any of the possible tags. Not very efficient, especially since this code has to process a few thousands of XML files...

So, how to get the tag name of the root element in an object of type IXMLDOMDocument2?

+2  A: 

I think what you are searching for is

doc.documentElement.nodeName
Stijn Sanders
Right on the spot! Thanks.
Workshop Alex