Solved

Delphi XE2 Parsing XML with TXMLDocument

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

777 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