我在解析XML时有问题。我设法进入了"TXMLDocument“,但它在Android上不起作用。
如何获得字段值?我需要得到9240-221我需要价值:"9240-221“
我没有在谷歌找到如何做到这一点(也没有找到一个手册,如何使用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;发布于 2017-02-21 14:33:40
如果希望跨平台支持XML文档,可以使用TXmlDocument,供应商将其设置为OmniXML。来自文档 (重点雷):
MSXML:内置RAD供应商中最快的。只有窗户。默认设置。对于跨平台支持,必须选择不同的XML供应商。如果不指定不同的XML供应商,则应用程序在Windows以外的其他平台上不支持XML,并且在其他平台上运行应用程序时会看到运行时异常。 OmniXML:比ADOM快得多,但比MSXML稍慢。Cross-platform. ADOM:比其他内置的RAD供应商慢。Cross-platform.
下面是一个使用OmniXML并在所有平台上工作的解决方案的完整示例(至少需要德尔菲XE7 ):
unit FrmMain;
interface
uses
Xml.Xmldom,
Xml.Omnixmldom,
Xml.Xmldoc,
Xml.Xmlintf,
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function selectNode(xnRoot: IXmlNode; const nodePath: WideString): IXmlNode;
var
intfSelect : IDomNodeSelect;
dnResult : IDomNode;
intfDocAccess : IXmlDocumentAccess;
doc: TXmlDocument;
begin
Result := nil;
if not Assigned(xnRoot) or not Supports(xnRoot.DOMNode, IDomNodeSelect, intfSelect) then
Exit;
dnResult := intfSelect.selectNode(nodePath);
if Assigned(dnResult) then
begin
if Supports(xnRoot.OwnerDocument, IXmlDocumentAccess, intfDocAccess) then
doc := intfDocAccess.DocumentObject
else
doc := nil;
Result := TXmlNode.Create(dnResult, nil, doc);
end;
end;
function XPathQuery(Doc : IXMLDocument; Query : String) : String;
var
Node : IXMLNode;
begin
Result := '';
Node := SelectNode(Doc.DocumentElement, Query);
if Assigned(Node) then
Result := Node.Text
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Xml: IXMLDocument;
Str : String;
begin
DefaultDOMVendor := sOmniXmlVendor;
Xml := TXMLDocument.Create(nil);
Xml.LoadFromXML
(
'<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>'
);
Str := XPathQuery(Xml, '//PostalCode');
ShowMessage(Str);
end;
end.https://stackoverflow.com/questions/42367527
复制相似问题