Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

delphi - XPath and TXmlDocument

In Delphi XE is it possible to use XPath with a TXmlDocument component?

I'm aware I can use late binding to access the MSXML2 and then use XPath:

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

But I wanna know if TXmlDocument installed with Delphi XE supports XPath.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can't find anything in the TXMLDocument documentation about XPath.

XML example, from the OmniXML XPath demo:

<?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>

Try something like this:

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('.ooks.xml');
  INode := SelectNode(IDoc.DocumentElement,'/bookstore/book[2]/title'); 
end;

Just as an FYI for others, I'll leave this in: OmniXML supports XPath, and has a demo that shows really well how to use it. It's also free, comes with source, supports Unicode, and has pretty good support through it's forums.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...