2017-02-21 61 views
0

解析XML時遇到問題。 我設法進入「TXMLDocument」,但它不適用於AndroidAndroid中的XML解析,IXMLDOMDocument

如何獲取字段值? 我需要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; 
+0

的IXMLDOMDocument是Windows XML解析器的接口對象(請參閱https://msdn.microsoft.com/en-us/library/windows/desktop/dd892951(v=vs.85).aspx)。你爲什麼期望在Android上使用它? – MartynA

+0

在「Embarcadero公司RadStudio 10.1Berlin - 德爾福」編譯窗戶時,我能得到「郵編」使用「TXMLDocument的」,但在Android上做當 「X:= TXMLDocument.Create(個體經營);在運行時,它給出了一個錯誤,我不能得到的值 所以我試圖找出另一種方式來獲得它 –

+0

@MartynA:。OmniXML廠商是跨平臺 – whosrdaddy

回答

0

如果你想爲XML文檔的跨平臺支持,可以使用TXmlDocument設置爲OmniXML供應商。從documentation(重點煤礦):

MSXML:最快的內置RAD Studio的XML供應商。僅限Windows。 默認。對於跨平臺支持,您必須選擇不同的XML 供應商。如果不指定一個不同的XML技術廠商,你的應用程序 沒有在其他平臺上比Windows XML的支持,你可以看到一個 運行時異常,當你運行在其他平臺應用程序。

OmniXML:比ADOM快得多,但比MSXML稍慢。 跨平臺

ADOM:比其他內置RAD Studio XML供應商慢。 跨平臺

下面是一個使用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. 
+0

我定義了TXMLDocument與你交談。 設置DOMVendor以使用OmniXML。 Xml:= TXMLDocument.Create(nil); Xml.DOMVendor:= GetDOMVendor('Omni XML'); 它工作正常,非常感謝。 再次感謝您,對於長時間延遲的回覆感到抱歉。 –