Delphi Xpath XML 查询

2023-12-01

我正在尝试找到的值<Link role="self">在以下 XML 文件中使用XPath query:

<?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>

我看到了一个 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符合指定的标准。

更多运算符请参见此处:http://msdn.microsoft.com/en-us/library/ms256122.aspx

请注意,正如 Runner 建议的那样,您还可以查询完整的 XML 路径。这将比//运算符,因为它不必盲目搜索每个节点。


Edit:您为什么请求第三个匹配节点([3]少量)? AFAICS,只有一个;如果你的真实文件does如果你有更多,并且你确定你想要第三个,那就可以了。否则,删除[3] query.


此外,根据您使用的 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]');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Delphi Xpath XML 查询 的相关文章

随机推荐