gpt4 book ai didi

delphi - XPath 和 TXmlDocument

转载 作者:行者123 更新时间:2023-12-03 14:35:16 37 4
gpt4 key购买 nike

在 Delphi XE 中是否可以使用带有 TXmlDocument 的 XPath组件?

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

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

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

最佳答案

我在 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;

仅供其他人引用,我将其保留在:OmniXML支持 XPath,并且有一个演示很好地展示了如何使用它。它也是免费的,附带源代码,支持 Unicode,并且通过其论坛获得了很好的支持。

关于delphi - XPath 和 TXmlDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5383919/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com