🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.delphi.xml
2 messages
2 total messages Started by DelphiGuy Wed, 09 Jul 2008 00:14
getting parameters from a XML node
#75
Author: DelphiGuy
Date: Wed, 09 Jul 2008 00:14
102 lines
3614 bytes
Hi everyone, my name is Luc and I'm currently looking for a way to get a
specific information from this file:

http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

I need to get the currency and rate from this line:
<Cube currency="USD" rate="1.5687" />

I have a little experience with XML files but I'm used to get information
directly like this line
<gesmes:name>European Central Bank</gesmes:name>

I have joined a short and simple example of how I'm reading XML files and
I'm sure I can add a line or two in this code to get the information I need
in the TravelChildren function but this is beyond my current abilities.

If someone could help me out on this it would be greatly appreciated.   This
code is displaying on a memo field the values of the searched node which in
my case is 'Cube'.

Thank you.



---------------------------------------------------------------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, MSXML2_TLB, comobj;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { D�clarations priv�es }
  public
    { D�clarations publiques }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
   i,j,ret_val,count : Integer;
   strData : String;
   datalist : tStringList;
   doc : iXMLDomDocument;
   nList : iXMLDomNodeList;

procedure travelChildren(nlist1:IXMLDOMNodeList);
var
   j:Integer;
   temp:String;
begin
    for j:=0 to nlist1.Get_length-1 do begin
       //node type 1 means an entity and node type 5 means EntityRef
       if((nlist1.Get_item(j).Get_nodeType= 1) or 
(nlist1.Get_item(j).Get_nodeType=5)) then
          travelChildren(nlist1.Get_item(j).Get_childNodes)
         //node Type 3 means a text node,
       else if(nlist1.Get_item(j).Get_nodeType=3) then begin
          temp:= trim(nlist1.Get_item(j).Get_nodeValue);
          DataList.Add(temp);
       end
    end;
end;

begin
    count:=1;
    DataList:=TStringList.Create;
    memo1.Clear;
    doc := CreateOleObject('Microsoft.XMLDOM') as IXMLDomDocument;
    doc.load('eurofxref-daily.xml');

    nlist:=doc.getElementsByTagName('Cube');

    for i:=0 to nlist.Get_length-1 do begin
       travelChildren(nlist.Get_item(i).Get_childNodes);

       for j := 0 to datalist.Count - 1 do begin
          memo1.lines.append(datalist[i]);
       end;
       memo1.lines.append('=======(Record no. :'+inttostr(count)+')');

       DataList.Clear;
       count:=count+1;
    end;
end;

end.
 
Re: getting parameters from a XML node
#76
Author: Remy Lebeau (TeamB)
Date: Wed, 09 Jul 2008 22:56
61 lines
2958 bytes
"DelphiGuy" <luc.co@sympatico.ca> wrote in message
news:487402eb$2@newsgroups.borland.com...

> Hi everyone, my name is Luc and I'm currently looking for a way
> to get a specific information from this file:
>
> http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml
>
> I need to get the currency and rate from this line:
> <Cube currency="USD" rate="1.5687" />

Since you are using MSXML directly, you can perform XPath queries via the
selectNodes() and selectSingleNode() methods instead of iterating the nodes
manually.  For example (untested):

    procedure TForm1.Button1Click(Sender: TObject);
    var
        i : Integer;
        Doc: IXMLDomDocument2;
        List : IXMLDomNodeList;
        Node: IXMLDomNode;
        Attrs: IXMLDOMNamedNodeMap;
    begin
        Memo1.Clear;

        Doc := CreateOleObject('Microsoft.XMLDOM') as IXMLDomDocument2;
        Doc.load('eurofxref-daily.xml');

        Doc.setProperty('SelectionLanguage', 'XPath');
        Doc.setProperty('SelectionNamespaces', 
'xmlns:gesmes=''http://www.gesmes.org/xml/2002-08-01'' 
xmlns:ecb=''http://www.ecb.int/vocabulary/2002-08-01/eurofxref''');

        // to retreive all currencies
        List := 
Doc.selectNodes('/gesmes:Envelope/ecb:Cube/ecb:Cube/ecb:Cube');
        for i := 0 to List.get_length-1 do
        begin
            Node := List.get_item(i);
            Attrs := Node.get_attributes;
            Memo1.Lines.Append(Format('Currency: %s, Rate: %s', 
[Attrs.getNamedItem('currency').get_text, 
Attrs.getNamedItem('rate').get_text]);
        end;

        // to retreive USD currency only
        Node := 
Doc.selectSingleNode('/gesmes:Envelope/ecb:Cube/ecb:Cube/ecb:Cube[@currency="USD"]');
        if Node <> nil then
            Memo1.Lines.Append('USD Currency is: ' + 
Node.get_attributes.getNamedItem('rate').get_text);
    end;

Refer to MSDN for more details:

    XPath Reference
    http://msdn.microsoft.com/en-us/library/ms256115.aspx


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads