加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – XPath和TXmlDocument

发布时间:2020-12-15 10:13:59 所属栏目:大数据 来源:网络整理
导读:在Delphi XE中可以使用带有 TXmlDocument 组件的XPath? 我知道我可以使用后期绑定访问MSXML2,然后使用XPath: XML := CreateOleObject('MSXML2.DOMDocument.3.0') ;XML.async := false;XML.SetProperty('SelectionLanguage','XPath'); 但是我想知道 TXmlDo
在Delphi XE中可以使用带有 TXmlDocument组件的XPath?

我知道我可以使用后期绑定访问MSXML2,然后使用XPath:

XML := CreateOleObject('MSXML2.DOMDocument.3.0') ;
XML.async := false;
XML.SetProperty('SelectionLanguage','XPath');

但是我想知道TXmlDocument安装的Delphi XE是否支持XPath。

解决方法

关于XPath的TXMLDocument文档中找不到任何内容。

来自OmniXML XPath演示的XML示例:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book>
    <title lang="eng">Harry Potter</title>
  </book>
  <book>
    <title lang="eng">Learning XML</title>
  </book>
  <book>
    <title lang="slo">Z OmniXML v lepso prihodnost</title>
    <year>2006</year>
  </book>
  <book>
    <title>Kwe sona standwa sam</title>
  </book>
</bookstore>

尝试这样的东西:

uses 
  XMLDoc,XMLDom,XMLIntf;

// From a post in Embarcadero's Delphi XML forum.
function 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;


var
  IDoc: IXMLDocument;
  INode: IXMLNode;
begin
  IDoc := LoadXMLDocument('.books.xml');
  INode := SelectNode(IDoc.DocumentElement,'/bookstore/book[2]/title'); 
end;

就像其他人的FYI一样,我会留下来:OmniXML支持XPath,并有一个非常好的演示,如何使用它。它也是免费的,来源,支持Unicode,并通过它的论坛有很好的支持。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读