在Delphi的XmlDom中使用XPath选中单个IXMLNode / TXmlNode 节点

来源:互联网 发布:网络边锋游戏大厅 编辑:程序博客网 时间:2024/05/16 03:10

原文:http://delphi.about.com/od/delphi-tips-2011/qt/select-single-node-ixmlnode-txmlnode-xpath-delphi-xml


为了选中与XPath表达式中匹配的单个节点,可以使用XmlDom.pas中定义的IDomNodeSelect接口。下面就是如何通过封装XPath的selectNode方法

得到 IXMLNode (TXMLNode)。

class function TXMLNodeHelper.SelectNode(xnRoot: IXmlNode; const nodePath: WideString): IXmlNode;var  intfSelect : IDomNodeSelect;  dnResult : IDomNode;  intfDocAccess : IXmlDocumentAccess;  doc: TXmlDocument;begin  Result := nil;  if not Assigned(xnRoot)    or not Supports(xnRoot.DOMNode, IDomNodeSelect, intfSelect) then    Exit;  dnResult := intfSelect.selectNode(nodePath);  if Assigned(dnResult) then  begin    if Supports(xnRoot.OwnerDocument, IXmlDocumentAccess, intfDocAccess) then      doc := intfDocAccess.DocumentObject    else      doc := nil;    Result := TXmlNode.Create(dnResult, nil, doc);  end;end;

注意:上面是一个类方法

0 0
原创粉丝点击