delphi xpath xml查询
发布时间:2020-12-15 09:43:55 所属栏目:大数据 来源:网络整理
导读:我正在尝试找到 Link role =“self”的值.在使用 XPath查询的以下XML文件中. ?xml version="1.0" encoding="utf-8"?Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.m
我正在尝试找到< Link role =“self”>的值.在使用
XPath查询的以下XML文件中.
<?xml version="1.0" encoding="utf-8"?> <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1"> <Copyright>Copyright ? 2011 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used,reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright> <BrandLogoUri>http://spatial.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri> <StatusCode>201</StatusCode> <StatusDescription>Created</StatusDescription> <AuthenticationResultCode>ValidCredentials</AuthenticationResultCode> <TraceId>ID|02.00.82.2300|</TraceId> <ResourceSets> <ResourceSet> <EstimatedTotal>1</EstimatedTotal> <Resources> <DataflowJob> <Id>ID</Id> <Link role="self">https://spatial.virtualearth.net/REST/v1/dataflows/Geocode/ID</Link> <Status>Pending</Status> <CreatedDate>2011-03-30T08:03:09.3551157-07:00</CreatedDate> <CompletedDate xsi:nil="true" /> <TotalEntityCount>0</TotalEntityCount> <ProcessedEntityCount>0</ProcessedEntityCount> <FailedEntityCount>0</FailedEntityCount> </DataflowJob> </Resources> </ResourceSet> </ResourceSets> </Response> 我在previous post中显示了一个XPath查询,但我在以下代码中继续获得未分配的iNode. function TForm1.QueryXMLData(XMLFilename,XMLQuery: string): string; var iNode : IDOMNode; Sel: IDOMNodeSelect; begin try XMLDoc.Active := False; XMLDoc.FileName := XMLFilename; XMLDoc.Active := True; Sel := XMLDoc.DOMDocument as IDomNodeSelect; Result := ''; iNode := Sel.selectNode('Link[@role = "self"]'); if Assigned(iNode) then if (not VarisNull(iNode.NodeValue)) then Result := iNode.NodeValue; XMLDoc.Active := False; Except on E: Exception do begin MessageDlg(E.ClassName + ': ' + E.Message,mtError,[mbOK],0); LogEvent(E.Message); end; end; end; 任何帮助将不胜感激. 问候,彼得 解决方法
如果要在文档中的任何位置找到“链接”,则必须在其前面加上//;像这样:
iNode := Sel.selectNode('//Link[@role = "self"][3]'); 这将开始在文档的根目录进行搜索,并遍历整个文档,查找名为Link的节点,该节点符合指定的条件. 有关更多操作符,请参阅此 请注意,正如Runner建议的那样,您还可以查询完整的XML路径.这将比//运算符更快,因为它不必盲目搜索每个节点. 编辑:为什么要请求第三个匹配节点([3]位)? AFAICS,只有一个;如果您的真实文档确实有更多,并且您确定您想要第三个,那么就可以了.否则,删除[3]查询. 此外,根据您使用的XML实现(供应商和版本),您还可能必须指定XML命名空间.在MSXML 4到6(IIRC)中,您必须使用 XMLDoc.setProperty('SelectionNamespaces','xmlns:ns="http://schemas.microsoft.com/search/local/ws/rest/v1"'); 这意味着在您的查询中也使用该前缀: iNode := Sel.selectNode('//ns:Link[@role = "self"][3]'); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |