我正在尝试<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>
Run Code Online (Sandbox Code Playgroud)
我在上一篇文章中看到了一个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;
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
问候,彼得
Mar*_*ijn 11
如果要在文档中的任何位置找到"链接",则必须在其前面添加//; 像这样:
iNode := Sel.selectNode('//Link[@role = "self"][3]');
Run Code Online (Sandbox Code Playgroud)
这将开始在文档的根目录进行搜索,并遍历整个文档,查找名为Link匹配指定条件的节点.
有关更多运营商的信息,请访问此处:http: //msdn.microsoft.com/en-us/library/ms256122.aspx
请注意,正如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"');
Run Code Online (Sandbox Code Playgroud)
这意味着在您的查询中也使用该前缀:
iNode := Sel.selectNode('//ns:Link[@role = "self"][3]');
Run Code Online (Sandbox Code Playgroud)
你应该这样写:
iNode := Sel.selectNode('//Link[@role = "self"]');
Run Code Online (Sandbox Code Playgroud)
这将使您获得文档中具有属性role ="self"的第一个Link节点(即使有多个).
或者你可以走绝对路径:
iNode := Sel.selectNode('/Response/ResourceSets/ResourceSet/Resources/DataflowJob/Link[@role = "self"]');
Run Code Online (Sandbox Code Playgroud)
甚至介于两者之间
iNode := Sel.selectNode('//Resources/DataflowJob/Link[@role = "self"]');
Run Code Online (Sandbox Code Playgroud)
最后我使用了OmniXML和以下代码。
uses
OmniXML, OmniXMLUtils, OmniXMLXPath;
...
function GetResultsURL(Filename: string): string;
var
FXMLDocument: IXMLDocument;
XMLElementList: IXMLNodeList;
XMLNode: IXMLNode;
XMLElement: IXMLElement;
i: integer;
begin
//Create and load the XML document
FXMLDocument := CreateXMLDoc;
FXMLDocument.Load(Filename);
//We are looking for: <Link role="output" name="failed">
XMLElementList := FXMLDocument.GetElementsByTagName('Link');
for i := 0 to Pred(XMLElementList.Length) do
begin
//Check each node and element
XMLNode := XMLElementList.Item[i];
XMLElement := XMLNode as IXMLElement;
if XMLElement.GetAttribute('role') = 'output' then
if Pos('failed', XMLNode.Text) > 0 then
Result := XMLNode.Text;
end;
end;
Run Code Online (Sandbox Code Playgroud)
收到的 XML 看起来像这样......
...
<DataflowJob>
<Id>12345</Id>
<Link role="self">https://spatial.virtualearth.net/REST/v1/dataflows/Geocode/12345</Link>
<Link role="output" name="failed">https://spatial.virtualearth.net/REST/v1/dataflows/Geocode/12345/output/failed</Link>
<Status>Completed</Status>
<CreatedDate>2011-04-04T03:57:49.0534147-07:00</CreatedDate>
<CompletedDate>2011-04-04T03:58:43.709725-07:00</CompletedDate>
<TotalEntityCount>1</TotalEntityCount>
<ProcessedEntityCount>1</ProcessedEntityCount>
<FailedEntityCount>1</FailedEntityCount>
</DataflowJob>
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6156 次 |
| 最近记录: |