xml - delphi读取xml元素

标签 xml delphi geospatial bing-api

我是 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>

我正在使用 Delphi XE。

问候,彼得

最佳答案

如何使用一些简单的 XPATH 来获取请求的值?

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

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

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

您可以找到一些关于 XPath 的文档 reading those XPath Examples on MSDN .您可能会找到更好的文档 at 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.

关于xml - delphi读取xml元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5483045/

相关文章:

javascript - IE8中如何获取XML节点的文本值?

css - 如何将CSS文件附加到xml文件格式?

delphi - 打开网络共享上的 Firebird 数据库文件

xml - 如何在Delphi 2009中解析此XML字符串?

mysql - C# mysql 75英里半径 map 覆盖整个美国

Android 布局预览未在 android studio 3.1.3 中加载

c# - 使用 Dynamic 或 Reflection.emit

delphi - 如何隐藏注册表上的值(如 sysinternals RegHide 工具)

indexing - 地理空间索引如何工作?

r - 从数百万个 GPS 坐标中确定国家的最快方法 [R]