Sal*_*dor 18 delphi xpath txmldocument delphi-xe
在Delphi中,XE是否可以将XPath与TXmlDocument组件一起使用?
我知道我可以使用后期绑定来访问MSXML2然后使用XPath:
XML := CreateOleObject('MSXML2.DOMDocument.3.0') ;
XML.async := false;
XML.SetProperty('SelectionLanguage','XPath');
Run Code Online (Sandbox Code Playgroud)
但我想知道TXmlDocument安装Delphi XE是否支持XPath.
Ken*_*ite 14
我在TXMLDocument文档中找不到有关XPath的任何内容.
XML示例,来自OmniXML XPath演示:
<?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>
Run Code Online (Sandbox Code Playgroud)
尝试这样的事情:
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;
Run Code Online (Sandbox Code Playgroud)
就像其他人的FYI一样,我将把它留在:OmniXML支持XPath,并有一个演示,非常好地展示了如何使用它.它也是免费的,附带源码,支持Unicode,并通过它的论坛提供了相当好的支持.