I have a problem with parsing XML. I managed to get into "TXMLDocument" but it does not work on Android .
How to get field values? I need to get 9240-221 I need the value: "9240-221"
I did not find in Google how to do it (also did not find a manual on how to work with IXMLDOMDocument).
Code:
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;
If you want cross platform support for XML documents, you can use TXmlDocument
with the vendor set to OmniXML
. From the documentation (emphasis mine):
MSXML : Fastest of the built-in RAD Studio XML vendors. Windows only. Default. For cross-platform support, you must choose a different XML vendor. If you do not specify a different XML vendor, your application does not have XML support on other platforms than Windows, and you see a run-time exception when you run your application in other platforms.
OmniXML : Much faster than ADOM, but slightly slower than MSXML. Cross-platform .
ADOM : Slower than the other built-in RAD Studio XML vendors. Cross-platform .
Here is a full blown example for a solution that uses OmniXML and that works on all platforms (you need at least Delphi XE7 for this):
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.