XML 在 Android 中解析,IXMLDOMDocument
XML parsing in Android, IXMLDOMDocument
我在解析 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;
如果您想要 XML 文档的跨平台支持,您可以使用 TXmlDocument
并将供应商设置为 OmniXML
。来自 documentation(强调我的):
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.
这是一个使用 OmniXML 并适用于所有平台的解决方案的完整示例(您至少需要 Delphi 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.
我在解析 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;
如果您想要 XML 文档的跨平台支持,您可以使用 TXmlDocument
并将供应商设置为 OmniXML
。来自 documentation(强调我的):
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.
这是一个使用 OmniXML 并适用于所有平台的解决方案的完整示例(您至少需要 Delphi 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.