我在TXMLDocument文档中找不到与XPath相关的内容。
以下是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;
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;
仅供其他人参考,我会将这个链接放在这里:OmniXML支持XPath,并提供了一个演示非常好的示例,展示如何使用它。它也是免费的,带有源代码,支持Unicode,并且通过其论坛具有相当好的支持。