Recent

Author Topic: Problem reading XML file.  (Read 17195 times)

T-bear

  • Full Member
  • ***
  • Posts: 160
Problem reading XML file.
« on: February 04, 2011, 02:23:12 pm »
Im trying to make a little applikation that reads something out of a XML-File. I dont know much about it and tried the first example at http://wiki.lazarus.freepascal.org/XML_Tutorial to learn, but it doesn't work. I get this errormessage:

In 'file:///L:/Programmieren/Lazarus/XML/2.XML'(line1 pos 4): XML declaration is not allowed here.

Press OK to ignore and risk data corruption.
Press Cancel to kill program.


Code:
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, DOM, XMLRead, XMLWrite;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
 PassNode: TDOMNode;
 Doc:      TXMLDocument;


begin
  ReadXMLFile(Doc, 'L:\Programmieren\Lazarus\XML\2.XML');
  PassNode := Doc.DocumentElement.FindNode('password');
  WriteLn(PassNode.FirstChild.NodeValue);
end;

initialization
  {$I unit1.lrs}

end.                       

XML File:
<?xml version="1.0"?>
 <request>
   <request_type>PUT_FILE</request_type>
   <username>123</username>
   <password>abc</password>
 </request>

Like you see its nearly the same as in the wiki, but it doesn't work...  %)  :(  :(
Can u help me?
Thanks!!  :)

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Problem reading XML file.
« Reply #1 on: February 04, 2011, 02:43:34 pm »
Works for me; do replace the writeln(...) with something like ShowMessage(...).
I'm assuming there is something wrong with your XML file.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: Problem reading XML file.
« Reply #2 on: February 04, 2011, 02:51:15 pm »
But what should be wrong? It's exactly the same as in the example...  :(

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Problem reading XML file.
« Reply #3 on: February 04, 2011, 03:17:38 pm »
Is 'L:\Programmieren\Lazarus\XML\2.XML' correctly filled?
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: Problem reading XML file.
« Reply #4 on: February 04, 2011, 03:19:38 pm »
Yeah. It's the file i posted in my first post. It's exactly like in the example at the wiki, and the path is also correct.

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Problem reading XML file.
« Reply #5 on: February 04, 2011, 03:24:57 pm »
Maybe an old Lazarus/fp version? (See my sig for what I'm using.)
Your posted code works and your posted xml also works.

If all else fails:
1. Create a new project.
2. Create a new xml file and copy/paste the xml text from this thread in there.
3. Do not put the testcode in the FormCreate method but drop a button on the form and put the code on the OnClick for that button. That gives you a little more control over the whole process.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: Problem reading XML file.
« Reply #6 on: February 04, 2011, 03:31:44 pm »
Thanks now i get it working and it writes 'abc' as it should.  :D

cdbc

  • Hero Member
  • *****
  • Posts: 2787
    • http://www.cdbc.dk
Re: Problem reading XML file.
« Reply #7 on: February 04, 2011, 06:55:41 pm »
Hi

have you tried looking at "2.xml" in a hex-editor?!? Maybe it contains an UTF-8 BOM in th first 2 bytes... (see post by martin_fr or google it, very informative)
http://www.lazarus.freepascal.org/index.php/topic,12008.msg60867.html#msg60867

Just a thought :)

Regards Benny
« Last Edit: February 04, 2011, 06:57:35 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: Problem reading XML file.
« Reply #8 on: February 04, 2011, 11:12:42 pm »
@cdbc:
Yeah we solved that problem. It worked when i just copied the text into a new XML-file.


But now i have got another problem...     %)  :(  :o  :P  :-[  :'(  :'(  :'(  >:D


I try to make a program with 5 checkboxes that uses a xml-file to save wich checkboxes are checked, when it closes. I working on the part where it reads when the program is opened.
Code:
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, XMLRead, XMLWrite, DOM,
  ExtCtrls, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckGroup1: TCheckGroup;
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
 PassNode: TDOMNode;
 Doc: TXMLDocument;
begin
  ReadXMLFile(Doc, 'cb.xml');

  begin
    PassNode := Doc.DocumentElement.FindNode('Checkbox1');
    WriteLn(PassNode.NodeValue);
    If PassNode.TextContent = 'False' Then Checkbox1.checked:=False
    Else Checkbox1.Checked:=True;
  end;
end;

initialization
  {$I unit1.lrs}

end.       
Here is the XML-File:
Code: [Select]
<?xml version="1.0"?>
<status>
<Checkbox1> False </Checkbox1>
<Checkbox2> False </Checkbox2>
<Checkbox3> False </Checkbox3>
<Checkbox4> False </Checkbox4>
<Checkbox5> False </Checkbox5>
</status>
Now i have the problem that wenn i run it i get runerror(103) wich means that the file isn't open. Anyone who can help?

Thanks!!  ::)

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Problem reading XML file.
« Reply #9 on: February 05, 2011, 10:16:32 am »
What part of
do replace the writeln(...) with something like ShowMessage(...)
in my 1st post didn't you understand  >:(

Also: there is a bug in the XML parser which removes whitespace.
So you should change your XML into (no space before and after False):

   <Checkbox1>False</Checkbox1>
   <Checkbox2>False</Checkbox2>
etc.
« Last Edit: February 05, 2011, 10:21:17 am by eny »
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

vvzh

  • Jr. Member
  • **
  • Posts: 58
Re: Problem reading XML file.
« Reply #10 on: February 05, 2011, 01:28:55 pm »
Also: there is a bug in the XML parser which removes whitespace.
Is it reported somewhere? I have not found it in Mantis.

julia789

  • Newbie
  • Posts: 1
Re: Problem reading XML file.
« Reply #11 on: February 06, 2011, 05:33:05 am »
This is a nifty little piece of programming. I used it for my own site, if you don't mind.

vvzh

  • Jr. Member
  • **
  • Posts: 58
Re: Problem reading XML file.
« Reply #12 on: February 21, 2011, 05:37:40 pm »
Also: there is a bug in the XML parser which removes whitespace.
Is it reported somewhere? I have not found it in Mantis.
I've just discovered it is not a bug. To preserve whitespace we need to specify PreserveWhitespace option for XML parser. The following example code was posted on Russian FPC forum:
Code: [Select]
procedure DOMFromStream(AStream: TStream);
var
  Parser: TDOMParser;
  Src: TXMLInputSource;
  TheDoc: TXMLDocument;
begin
  try
    Parser := TDOMParser.Create;
    Src := TXMLInputSource.Create(AStream);
    Parser.Options.PreserveWhitespace := True;
    Parser.Parse(Src, TheDoc);
  finally
    Src.Free;
    Parser.Free;
  end;
end;

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Problem reading XML file.
« Reply #13 on: February 22, 2011, 11:53:16 am »
I've just discovered it is not a bug. To preserve whitespace ...
This is the hint of the day  :D  :)
I've updated the wiki with this info.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

 

TinyPortal © 2005-2018