我是 XML 新手,我们需要使用新的进行地理编码必应空间数据 API http://msdn.microsoft.com/en-us/library/gg585131.aspx。我已经设法以 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。
问候, 彼得
使用一些简单的 XPATH 来获取请求的值怎么样?
//Link[1]/node()
- 从整个文档中选择第一个“链接”节点,然后选择任何类型的第一个子节点。只是碰巧第一个子节点是包含实际的未命名节点https
link.
假设 XML 文档被加载到Doc: TXMLDocument
,您可以使用以下代码提取链接:
(Doc.DOMDocument as IDomNodeSelect).selectNode('//Link[1]/node()').nodeValue
您可以找到一些有关 XPath 的文档阅读 MSDN 上的 XPath 示例 http://msdn.microsoft.com/en-us/library/ms256086.aspx。您可能会找到更好的文档在w3学校 http://www.w3schools.com/XPath/xpath_syntax.asp。最重要的是,这是一个简单(但完整)的控制台应用程序,它使用 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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)