Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delphi XE2 Parsing XML with TXMLDocument

Posted on 2014-02-17
3
Medium Priority
?
6,545 Views
Last Modified: 2014-02-19
Hi
 I am trying to parse an XML file using TXMLDocument and Delphi XE2. I am new to XML with Delphi and having much trouble accessing the attribute values from this file:

http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

I have no trouble reaching the first level <Cube> and can return an IXMLNode instance but I cannot get to anything below that ie. second and third level child nodes (both <Cube>) and their attribute values.

Basically I'm doing this, for example, to get the "time" attribute value from the second level <Cube> ...

      StartingItemNode := XMLDocument.DocumentElement.ChildNodes['Cube'];
      aNode := StartingItemNode;
      sDATE := aNode.ChildNodes['Cube'].Attributes['time'];

... line 1 is fine but sDATE in line 3 is always null.

Please could anyone tell me where this is going wrong ?

thanks and best wishes

RR
0
Comment
Question by:RetroRocker
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
Sinisa Vuk earned 2000 total points
ID: 39865910
This is my example:

uses ComObj, MSXML;

....
var
  XMLDOMDocument  : IXMLDOMDocument;
  XMLDOMNode      : IXMLDOMNode;
  XMLDOMNodeList  : IXMLDOMNodeList;
  aXMLDOMNode     : IXMLDOMNode;
  xml : TStringList;
  i: Integer;
begin
  xml := TStringList.Create;
  try
    xml.LoadFromFile('C:\Delphi6\Projects\Temp\test\eurofxref-daily.xml');

    XMLDOMDocument:=CoDOMDocument.Create;
    try
      XMLDOMDocument.loadXML(xml.Text);

      if (XMLDOMDocument.parseError.ErrorCode<>0) then
        raise Exception.CreateFmt('Error in Xml Data %s', [XMLDOMDocument.parseError]);
        
      XMLDOMNode := XMLDOMDocument.selectSingleNode('//gesmes:Envelope/Cube/Cube');
      if XMLDOMNode<>nil then
      begin
        ListBox3.Items.Add(XMLDOMNode.attributes.getNamedItem('time').Text);
        XMLDOMNodeList := XMLDOMNode.selectNodes('.//Cube');

        if XMLDOMNodeList<>nil then
        begin
          for i:=0 to XMLDOMNodeList.length-1 do
          begin
            aXMLDOMNode := XMLDOMNodeList.item[i];
            ListBox3.Items.Add(aXMLDOMNode.attributes.getNamedItem('currency').Text + ' = '+
              aXMLDOMNode.attributes.getNamedItem('rate').Text);
          end;
        end;
      end;
    finally
      XMLDOMDocument:=nil;
    end;
  finally
    xml.Free;
  end;

Open in new window


Main goal is to select main Cube node to get time attribute. The select relative node Cube and get list of sub-nodes.
0
 

Author Closing Comment

by:RetroRocker
ID: 39868697
What can I say ...  a thoroughly detailed, fully working solution ... I am genuinely very grateful :-)
I probably would have continued to struggle with TXMLDocument to parse this file and even now, I have still not found a way to do it. I believe that TXMLDocument is a 'wrapper' for MSXML and I applaud your ingenuity in employing MSXML directly. It seems to have methods which are more suitable for addressing the nodes in this case. With TXMLDocument I still have never been able to get beyond the first level of <Cube> in this file.
Once again, many thanks for your time and effort with this.

RR
0
 
LVL 28

Expert Comment

by:Sinisa Vuk
ID: 39869633
Thanks. I'm glad to help you. Yes, this is MS parser - and is fast enough, but depends on external dll (part of windows). But there are more other third part components like this one:
http://www.kluug.net/oxml.php
Look at Performance tab.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Progress

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question