Recent

Author Topic: Read XML  (Read 1221 times)

vàp

  • Newbie
  • Posts: 3
Read XML
« on: May 15, 2019, 11:59:21 am »
I am trying to read this XML file

Code: XML  [Select][+][-]
  1. <?xml version="1.0" standalone="yes"?>
  2. <brol>
  3.   <Result>
  4.     <username>123</username>
  5.     <password>abc</password>
  6.   </Result>
  7. </brol>

with this code

unit Unit1;

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. interface
  3. uses
  4.   Classes, SysUtils, CustApp, Dom, XmlRead;
  5. var
  6.   Doc: TXMLDocument;
  7.   Members: TDOMNodeList;
  8.   Member: TDOMNode;
  9.   i: integer;
  10.   Batch: text;
  11.  
  12. implementation
  13.  
  14. begin
  15.   AssignFile(Batch, 'output.txt');
  16.   ReWrite(Batch);
  17.   ReadXMLFile(Doc, 'A.xml');
  18.   Members:= Doc.GetElementsByTagName('password');
  19.   for i:= 0 to Members.Count - 1 do
  20.   begin
  21.     Member:= Members[i];
  22.     WriteLn(Batch, Member.TextContent);
  23.   end;
  24.   Close(Batch);
  25. end.

Not only output.txt remains desperately empty, but if I watch Member.TextContent I get <Error: Type TDOMNODE has no component named TEXTCONTENT.> although TextContent is displayed when I enter
Code: Pascal  [Select][+][-]
  1. Member.


What is wrong here?

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Read XML
« Reply #1 on: May 15, 2019, 12:19:49 pm »
why not use TXMLConfig the unit XMLConf?
then you can do it like this:
Code: Pascal  [Select][+][-]
  1. . type     XMLConfig_DB: TXMLConfig;
  2.   var code: string;
  3.  
  4.  
  5.  
  6.   if FileExists(cfgFile) then    begin
  7.       XMLConfig_DB.Filename := cfgFile ;
  8.       // Check version
  9.       Code := AnsiString(Form_RefereeMain.XMLConfig_DB.GetValue ('/RefereeDatabase/Version/Value', ''));
  10.       k := StrToInt(Code);
  11.       Code := AnsiString(XMLConfig_DB.GetValue ('/RefereeDatabase/Setup/Base_MS/Value', ''));
  12.       smtpHost := AnsiString(XMLConfig_DB.GetValue ('/RefereeDatabase/Setup/Base_MP/Value', ''));
  13.       smtpPort := AnsiString(XMLConfig_DB.GetValue ('/RefereeDatabase/Setup/Base_UN/Value', ''));
  14.       smtpUser := AnsiString(XMLConfig_DB.GetValue ('/RefereeDatabase/Setup/Base_UP/Value', ''));
  15.       smtpPassword := AnsiString(XMLConfig_DB.GetValue ('/RefereeDatabase/Setup/Base_NM/Value', ''));
  16.     end
  17.  
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

martijnn

  • New Member
  • *
  • Posts: 16
Re: Read XML
« Reply #2 on: May 15, 2019, 12:43:31 pm »
Your code works fine for me. Also tested with additional "password" nodes. Tested on (Lazarus 2.0.2 on x86_64 / win64 -- Win 10)

I guess you should check first if it is possible to write anything to the output file.

Peter H

  • Sr. Member
  • ****
  • Posts: 272
Re: Read XML
« Reply #3 on: May 15, 2019, 01:17:19 pm »
if I watch Member.TextContent I get <Error: Type TDOMNODE has no component named TEXTCONTENT.> although TextContent is displayed when I enter
Code: Pascal  [Select][+][-]
  1. Member.


What is wrong here?
Probably this is a debugger limitation.
The gdb debugger cannot display properties, when these are mapped to procedures.
Only properties that map to data are supported.

To debug, best copy the text to a stringvariable, this can be watched.

vàp

  • Newbie
  • Posts: 3
Re: Read XML
« Reply #4 on: May 15, 2019, 01:37:18 pm »
madref: sorry but I failed to see how this could help, and a bit puzzled by the smtp references

martijnn: THANK YOU ! I was still using 1.8.4, I uninstalled it, got 2.0.2, tried again and it works fine

 

TinyPortal © 2005-2018