【发布时间】:2017-02-21 12:35:41
【问题描述】:
我在解析 XML 时遇到问题。 我设法进入“TXMLDocument”,但它在 Android 上不起作用。
如何获取字段值? 我需要拨打 9240-221 我需要值:“9240-221”
我在 Google 中没有找到如何操作(也没有找到关于如何使用 IXMLDOMDocument 的手册)。
代码:
uses ComObj, MSXML;
procedure TForm2.Button1Click(Sender: TObject);
var
xml: IXMLDOMDocument;
node: IXMLDomNode;
nodes_row, nodes_se: IXMLDomNodeList;
i, j: Integer;
url: string;
begin
// put url or file name
//url := 'https://reverse.geocoder.cit.api.here.com/6.2/reversegeocode.xml?prox=32.791288%2C-17.045887&mode=retrieveAddresses&maxresults=1&gen=8&app_id=ZHsaRDKOhKQKjKOba0cS&app_code=RPlNCmcST6RICWUMk2OzYQ';
xml := CreateOleObject('Microsoft.XMLDOM') as IXMLDOMDocument;
xml.async := False;
//xml.load(url); // or use loadXML to load XML document using a supplied string
xml.loadXML
(
'<ns2:Search xmlns:ns2="http://www.navteq.com/lbsp/Search-Search/4">'+
'<Response>'+
'<MetaInfo>...</MetaInfo>'+
'<View xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:SearchResultsViewType"> '+
'<ViewId>0</ViewId>'+
'<Result>'+
'<Relevance>1.0</Relevance>'+
'<Distance>-1996.0</Distance>'+
'<Direction>358.6</Direction>'+
'<MatchLevel>city</MatchLevel>'+
'<MatchQuality>...</MatchQuality>'+
'<Location>'+
'<LocationId>NT_yT.xGXLRj-bHQLe8aMmP2A</LocationId>'+
'<LocationType>area</LocationType>'+
'<DisplayPosition>...</DisplayPosition>'+
'<MapView>...</MapView>'+
'<Address>'+
'<Label>São Vicente, Portugal</Label>'+
'<Country>PRT</Country>'+
'<County>Ilha da Madeira</County>'+
'<City>São Vicente</City>'+
'<PostalCode>9240-221</PostalCode> '+
'<AdditionalData key="CountryName">Portugal</AdditionalData>'+
'<AdditionalData key="CountyName">Ilha da Madeira</AdditionalData>'+
'</Address>'+
'<MapReference>...</MapReference>'+
'</Location> '+
'</Result>'+
'</View>'+
'</Response>'+
'</ns2:Search>'
);
if xml.parseError.errorCode <> 0 then
raise Exception.Create('XML Load error:' + xml.parseError.reason);
nodes_row := xml.selectNodes('/ns2');
for i := 0 to nodes_row.length - 1 do
begin
node := nodes_row.item[i];
showmessage('phrase=' + node.selectSingleNode('ViewId').text);
nodes_se := node.selectNodes('.....');
for j := 0 to nodes_se.length - 1 do
begin
node := nodes_se.item[j];
end;
showmessage('--------------');
end;
end;
【问题讨论】:
-
IXMLDOMDocument 是一个 Windows XML Parser 对象的接口(参见msdn.microsoft.com/en-us/library/windows/desktop/…)。您为什么希望在 Android 上使用它?
-
在“Embarcadero RadStudio 10.1Berlin - Delphi”中为 windows 编译时我可以使用“TXMLDocument”获得“PostalCode”,但在 Android 上执行“X:= TXMLDocument.Create (self);运行时它给出了一个错误,我无法获得该值。所以我试图找出另一种获取它的方法。
-
@MartynA:OmniXML 供应商是跨平台的。
-
@whosrdaddy:当然,我在评论对 IXMLDOMDocument 的引用是因为
MSXML在代码的 Uses 子句中(更不用说ComObj)。 -
@MartynA,哦,我现在明白了,没看代码,他在问题中提到了TXmlDocument :)
标签: android xml delphi delphi-10.1-berlin