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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.