Solved

Delphi XE2 Parsing XML with TXMLDocument

Posted on 2014-02-17
3
6,044 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 27

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 27

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
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.
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…

752 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