从 Delphi 中的 IXMLDOMDocument2 对象读取标签名称
我有这样的事情:
uses MSXML2_TLB;
type TDocumentType = (dtOrder, dtInvoice, dtStatus, dtError); // And a few more, actually
function DetermineDocumentType(doc: IXMLDOMDocument2): TDocumentType;
...
它必须做一些简单的事情:确定 doc 参数中包含哪种 XML。 XML 可能类似于:
、
、<状态>...
、<错误>...
或其他内容。这些文件的内部结构非常相似,因此确定其类型的唯一可靠方法是从 XML 中获取第一个标签名称。 Doc.nodeName 和 Doc.baseName 不起作用。我可以将 XML 作为文本读取,通过代码解析它以确定标记名,但这是一个我拒绝使用的肮脏技巧。因此,我需要一个方法,该方法将通过 IXMLDOMDocument2 接口的正确方法返回标签名称。 更糟糕的是,我无法控制该函数之外的代码,并且作为第二个限制,该根标记恰好不区分大小写,使得像使用 selectNode() 这样的技巧无法工作。由于遗留代码和以前的开发人员过去做出的错误决定,这恰好是一个问题。 (幸运的是,这个项目中只有根标签看起来很奇怪。) 顺便说一句,这个功能是为了取代在 TheDailyWTF 网站上表现良好的类似功能。之前它会从接口读取 XML,将其完全转换为大写,最后执行大量 pos() 函数调用来检查任何可能的标签。效率不高,特别是因为此代码必须处理数千个 XML 文件...
那么,如何获取 IXMLDOMDocument2 类型的对象中根元素的标记名称?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你正在寻找的是
I think what you are searching for is