How to import a XML file

What do I wrong in importing a XML-file.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    XMLDocument1: TXMLDocument;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  xmlRoot: IXMLNode;
  xmlOpdracht, xmlTarieven, xmlTariefkop: IXMLNode;
begin
  XMLDocument1.FileName := 'TEST.XML';
  XMLDocument1.Active := True;
  try
    xmlRoot := XMLDocument1.DocumentElement;
    xmlOpdracht := xmlRoot.ChildNodes.FindNode('Opdracht');
    xmlTarieven := xmlOpdracht.ChildNodes.FindNode('Tarieven');
    xmlTariefkop := xmlTarieven.ChildNodes.FindNode('Tariefkop');

    while Assigned(xmlOpdracht) do
    begin
      xmlTarieven := xmlOpdracht.ChildNodes[0];

      while Assigned(xmlTarieven) do
      begin
        xmlTariefkop := xmlTarieven.ChildNodes[0];

        while Assigned(xmlTariefkop) do
        begin
          if xmlTariefkop.NodeName = 'Factuurnetto' then Memo1.Lines.Add(xmlTariefkop.Text);
          //....
          xmlTariefkop := xmlTariefkop.NextSibling;
        end; //xmlTariefkop

        xmlTarieven := xmlTarieven.NextSibling;
      end; //xmlTarieven

      xmlOpdracht := xmlOpdracht.NextSibling;
    end; //xmlOpdracht

  finally
    XMLDocument1.Active := false;
  end;


end;

end.
included the xml-file

greetings,
Henk van den Boogaard
TEST.XML
BoogaardAsked:
Who is Participating?
 
ebob42Connect With a Mentor Commented:
Also, the following line is incorrect:

  if xmlTariefkop.NodeName = 'Factuurnetto' then Memo1.Lines.Add(xmlTariefkop.Text);

xmlTariefKop will always be the <Tariefkop> node, and never the Factuurnetto. It you want to see the contents of the Factuurnetto, you need to find that node instead.

Here's some working code (see also my Delphi 2010 XML, SOAP and Web Services manual for more examples on XML Document Programming).
procedure TExpertExchangeForm.Button6Click(Sender: TObject);
var
  xmlRoot: IXMLNode;
  xmlOpdracht, xmlTarieven, xmlTariefkop,
  xmlFactuurNetto: IXMLNode;
begin
  XMLDocument1.FileName := 'C:\TEST.XML';
  XMLDocument1.Active := True;
  try
    xmlRoot := XMLDocument1.DocumentElement;
    xmlOpdracht := xmlRoot.ChildNodes.FindNode('Opdracht');
    xmlTarieven := xmlOpdracht.ChildNodes.FindNode('Tarieven');
    xmlTariefkop := xmlTarieven.ChildNodes.FindNode('Tariefkop');

    while Assigned(xmlOpdracht) do
    begin
      xmlTarieven := xmlOpdracht.ChildNodes.FindNode('Tarieven'); // [0];

      while Assigned(xmlTarieven) do
      begin
        xmlTariefkop := xmlTarieven.ChildNodes.FindNode('Tariefkop'); // [0];

        while Assigned(xmlTariefkop) do
        begin
          xmlFactuurNetto := xmlTariefKop.ChildNodes.FindNode('Factuurnetto');
          if Assigned(xmlFactuurNetto) then
            Memo1.Lines.Add(xmlFactuurNetto.Text);
          //....
          xmlTariefkop := xmlTariefkop.NextSibling;
        end; //xmlTariefkop

        xmlTarieven := xmlTarieven.NextSibling;
      end; //xmlTarieven

      xmlOpdracht := xmlOpdracht.NextSibling;
    end; //xmlOpdracht

  finally
    XMLDocument1.Active := false;
  end
end;

Open in new window

0
 
ebob42Commented:
What is the problem exactly? I didn't download the XML file to try, yet, but it might help to tell us what's going wrong. Do you get no information from the XML file?

Waar gaat het fout? (of wat gaat er precies mis?)
0
 
ebob42Commented:
xmlTarieven := xmlOpdracht.ChildNodes[0];

will result in the first childnode, being the SoortOpdracht, to be assigned to xmlTarieven.

Next, the line

xmlTariefkop := xmlTarieven.ChildNodes[0];

will fail, since SoortOpdracht has no childnodes.
0
 
ebob42Commented:
In the above source code, you may need to change C:\TEST.XML back to TEST.XML
0
 
BoogaardAuthor Commented:
Thanks for the quick answer.

In dutch:
Bedankt voor het snelle antwoord.
Ik zal werkelijk vast in dit probleem.

gr. Henk
0
All Courses

From novice to tech pro — start learning today.