3

我是 XML 新手,我们需要使用新的Bing Spatial Data API进行地理编码。我已经设法以 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>

我正在使用德尔福 XE。

问候,彼得

4

4 回答 4

6

使用一些简单的 XPATH 来获取请求的值怎么样?

//Link[1]/node()- 从整个文档中选择第一个“链接”节点,然后选择任何类型的第一个子节点。恰好第一个子节点是包含实际https链接的未命名节点。

假设 XML 文档已加载到Doc: TXMLDocument中,您可以使用以下代码提取链接:

(Doc.DOMDocument as IDomNodeSelect).selectNode('//Link[1]/node()').nodeValue

您可以在 MSDN 上阅读这些 XPath 示例找到一些有关 XPath 的文档。您可能会在 w3schools找到更好的文档。最重要的是,这是一个简单(但完整)的控制台应用程序,它使用 XPath 提取和显示 3 个请求的值:

program Project14;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Xmldoc,
  xmldom,
  ActiveX;

var X: TXMLDocument;
    Node: IDOMNode;
    Sel: IDomNodeSelect;

begin
  try
    CoInitialize(nil);

    X := TXMLDocument.Create(nil);
    try

      // Load XML from a string constant so I can include the exact XML sample from this
      // question into the code. Note the "SomeNode" node, it's required to make that XML
      // valid.

      X.LoadFromXML(
        '<SomeNode>'+
        '  <Link role="self">' +
        '    https://spatial.virtualearth.net/REST/v1/dataflows/Geocode/jobid' +
        '  </Link>' +
        '  <Status>Aborted</Status>' +
        '  <ErrorMessage>The data uploaded in this request was not valid.</ErrorMessage>' +
        '</SomeNode>'
      );

      // Shortcut: Keep a reference to the IDomNodeSelect interface

      Sel := X.DOMDocument as IDomNodeSelect;

      // Extract and WriteLn() the values. Painfully simple!

      WriteLn(Sel.selectNode('//Link[1]/node()').nodeValue);
      WriteLn(Sel.selectNode('//Status[1]/node()').nodeValue);
      WriteLn(Sel.selectNode('//ErrorMessage[1]/node()').nodeValue);

      ReadLn;
    finally X.Free;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
于 2011-03-30T09:02:44.873 回答
3

由于这些 Bing 空间数据服务有一个XML 模式,最简单的方法是使用 Delphi XML 数据绑定向导导入该模式,然后使用生成的 Delphi 类和接口从 XML 中获取数据,或放入数据在 XML 中。

这类似于Jørn E. Angeltveit的建议,但他的建议使用纯 XML 来生成类。
如果您没有架构,那没关系,但是当您有架构时,导入架构总是更好。

有很多关于使用Delphi XML Data Binding Wizard的示例,所以首先从那里开始。

如果您在装订方面需要帮助:请在此处提出新的具体问题。

于 2011-03-30T09:03:31.667 回答
3

如果 XML 结构相当稳定,您可以使用 XML 绑定工具生成普通的 Delphi 类来访问 xml 文档。

看看这个页面

于 2011-03-30T07:59:35.760 回答
2

现在您应该解析 XML 文件。在最简单的情况下(您知道 XML 标签),它可能如下所示:

var
  XMLDoc: IXMLDocument;
  Node: IXMLNode;
  I: Integer;
  role, link: string;

begin
  XMLDoc:= TXMLDocument.Create(nil);
  XMLDoc.LoadFromFile(AFileName);

  for I:= 0 to XMLDoc.DocumentElement.ChildNodes.Count - 1 do begin
    Node:= XMLDoc.DocumentElement.ChildNodes[I];
    if Node.NodeType = ntElement then begin
      if Node.NodeName = 'Link' then begin
        if Node.HasAttribute('role') then
          role:= Node.Attributes['role'];
        if not VarIsNull(Node.NodeValue) then
          link:= Node.NodeValue;
[..]
      end;
    end;
  end;
end;
于 2011-03-30T07:29:05.420 回答