Solved

Delphi XE2 Parsing XML with TXMLDocument

Posted on 2014-02-17
3
5,679 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 26

Accepted Solution

by:
Sinisa Vuk earned 500 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 26

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get the parent node - XMLTYPE 9 56
MS Access XML Export Query Setup Multiple Tag Values 15 31
SQL XML ALL Nodes Compare in function 2 18
Help with Syntax 9 29
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now