在Delphi中,XE是否可以将XPath与TXmlDocument
组件一起使用?
我知道我可以使用后期绑定来访问MSXML2,然后使用XPath:
XML := CreateOleObject('MSXML2.DOMDocument.3.0') ;
XML.async := false;
XML.SetProperty('SelectionLanguage','XPath');
但我想知道安装Delphi XE的TXmlDocument
是否支持XPath。
答案 0 :(得分: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>
尝试这样的事情:
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,并通过它的论坛提供了很好的支持。