Solved

Delphi XE2 Parsing XML with TXMLDocument

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

Accepted Solution

by:
Sinisa Vuk earned 500 total points
Comment Utility
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
Comment Utility
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 25

Expert Comment

by:Sinisa Vuk
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

743 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

15 Experts available now in Live!

Get 1:1 Help Now