Lazarus

Programming => General => Topic started by: rcmz on March 03, 2025, 12:53:05 am

Title: using xml
Post by: rcmz on March 03, 2025, 12:53:05 am
hi,

What units or components can I use to read write xml files ?

Sample code is welcome.

TIA
Ramiro
Title: Re: using xml
Post by: TRon on March 03, 2025, 12:56:59 am
lmddgtfy (https://lmddgtfy.net/?q=freepascal%20xml)
Title: Re: using xml
Post by: dsiders on March 03, 2025, 01:07:11 am
lmddgtfy (https://lmddgtfy.net/?q=freepascal%20xml)

 ;D
Title: Re: using xml
Post by: TRon on March 03, 2025, 01:17:24 am
Yes, I know dsiders. It is shameful  :-[

On the other hand these kind of open ended questions which usually can be answered with exactly that search to end up in the wiki which explains everything in detail of what was asked for confuses me. Without the intent to discredit OP but lately it seems like a lot of people are broken or something ? e.g, not knowing how to search for things ?

Perhaps this (https://wiki.freepascal.org/index.php?title=Special%3ASearch&search=xml+tutorial&go=Go) is better ?
Title: Re: using xml
Post by: rcmz on March 03, 2025, 02:16:34 am
hi,

I now how to do this in delphi, and I have the code in Lazarus, but the nodes in delphi the order is displayed as is, in Lazarus the nodes dont have the same order.

Before asking here, I searched and used DeepSeek, in fact I put my delphicode in to deepseek and it gave me a working code.

but with the error.

The question remains why in delphi i get the correcto order and lazarus i dont
Title: Re: using xml
Post by: TRon on March 03, 2025, 02:34:08 am
That's at least something we can work with  :)


I now how to do this in delphi, and I have the code in Lazarus, but the nodes in delphi the order is displayed as is, in Lazarus the nodes dont have the same order.
Do you use the same components, classes and/or code for both Delphi and Lazarus ? Where/how are you able to display/verify that difference in node ordering ?

Quote
The question remains why in delphi i get the correcto order and lazarus i dont
The quickest way to have that answered is by an example that we are able to compile that is able to show this difference in behaviour.

Note that different solutions/implementations for reading/writing XML can use other internal solutions (though XML should in theory follow the way the nodes are added/inserted and/or loaded/saved).
Title: Re: using xml
Post by: rcmz on March 03, 2025, 04:41:18 am
Hi,

No they use different units

Im attaching a file with the code in delphi and lazarus

also the xml files that where build with delphi and lazarus

TIA
Ramiro
Title: Re: using xml
Post by: dsiders on March 03, 2025, 08:19:51 am
No they use different units

Im attaching a file with the code in delphi and lazarus

also the xml files that where build with delphi and lazarus

Since your example has database code that is not included... no one can execute it.

I did however recreate the file using:

Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. uses
  4.   Laz2_DOM, Laz2_XMLWrite;
  5.  
  6. procedure TForm1.ShowInfo;
  7. var
  8.   XmlDoc: TXMLDocument;
  9.   Root, Child: TDOMElement;
  10.   StrStr: TStringStream;
  11. begin
  12.   XmlDoc := TXMLDocument.Create;
  13.  
  14.   Root := XmlDoc.CreateElement('catalogocuentas:Catalogo');
  15.   Root.AttribStrings['xmlns:catalogocuentas'] := 'http://www.sat.gob.mx/esquemas/ContabilidadE/1_3/CatalogoCuentas';
  16.   Root.AttribStrings['xmlns:xsi'] := 'http://www.w3.org/2001/XMLSchema-instance';
  17.   Root.AttribStrings['xsi:schemaLocation'] := 'http://www.sat.gob.mx/esquemas/ContabilidadE/1_3/CatalogoCuentas http://www.sat.gob.mx/esquemas/ContabilidadE/1_3/CatalogoCuentas/CatalogoCuentas_1_3.xsd';
  18.   Root.AttribStrings['Version'] := '1.3';
  19.   Root.AttribStrings['RFC'] := 'ROAP640729JZ5';
  20.   Root.AttribStrings['Mes'] := '02';
  21.   Root.AttribStrings['Anio'] := '2025';
  22.   XmlDoc.AppendChild(Root);
  23.  
  24.   Child := XmlDoc.CreateElement('catalogocuentas:Ctas');
  25.   Child.AttribStrings['CodAgrup'] := '101.01';
  26.   Child.AttribStrings['NumCta'] := '100-000-000';
  27.   Child.AttribStrings['Desc'] := 'FONDO FIJO DE CAJA';
  28.   Child.AttribStrings['Nivel'] := '1';
  29.   Child.AttribStrings['Natur'] := 'D';
  30.   Root.AppendChild(Child);
  31.  
  32.   Child := XmlDoc.CreateElement('catalogocuentas:Ctas');
  33.   Child.AttribStrings['CodAgrup'] := '102.01';
  34.   Child.AttribStrings['NumCta'] := '100-000-000';
  35.   Child.AttribStrings['Desc'] := 'BANCOMER SA DE CV';
  36.   Child.AttribStrings['Nivel'] := '1';
  37.   Child.AttribStrings['Natur'] := 'D';
  38.   Root.AppendChild(Child);
  39.  
  40.   StrStr := TStringStream.Create;
  41.   WriteXmlFile(XmlDoc, StrStr);
  42.   MemoXML.Text := StrStr.DataString;
  43.  
  44.   StrStr.Free;
  45.   XmlDoc.Free;
  46. end;
  47.  

That code produces output that looks like this:

Code: XML  [Select][+][-]
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <catalogocuentas:Catalogo xmlns:catalogocuentas="http://www.sat.gob.mx/esquemas/ContabilidadE/1_3/CatalogoCuentas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/esquemas/ContabilidadE/1_3/CatalogoCuentas http://www.sat.gob.mx/esquemas/ContabilidadE/1_3/CatalogoCuentas/CatalogoCuentas_1_3.xsd" Version="1.3" RFC="ROAP640729JZ5" Mes="02" Anio="2025">
  3.   <catalogocuentas:Ctas CodAgrup="101.01" NumCta="100-000-000" Desc="FONDO FIJO DE CAJA" Nivel="1" Natur="D"/>
  4.   <catalogocuentas:Ctas CodAgrup="102.01" NumCta="100-000-000" Desc="BANCOMER SA DE CV" Nivel="1" Natur="D"/>
  5. </catalogocuentas:Catalogo>
  6.  

As you can see, it writes the attribute values in the order in which they are added to the DOM element.



Title: Re: using xml
Post by: rcmz on March 03, 2025, 04:08:38 pm
hi,

I will give it a try

thanks
Ramiro
Title: Re: using xml
Post by: rcmz on March 03, 2025, 04:51:29 pm
hi,

what package or where to get Laz2_DOM, Laz2_XMLWrite ?

TIA
Ramiro
Title: Re: using xml
Post by: korba812 on March 03, 2025, 05:07:21 pm
You need LazUtils package. It is installed by default in Lazarus or you can find it in the "components" directory.
Title: Re: using xml
Post by: rcmz on March 03, 2025, 06:06:27 pm
hi,

already cheked that but still having problems with

Root.AttribStrings (AttribStrings is not referenced)

TIA
Ramiro
Title: Re: using xml
Post by: korba812 on March 03, 2025, 06:24:23 pm
Show us your code.
Title: Re: using xml
Post by: Thaddy on March 03, 2025, 06:47:28 pm
There are many ways to use the olden xml.
First:
Two forms:
1. SAX based, stream oriented
2. DOM based, document oriented
and all implementations rely on one of those two.
So if you do not provide code on what you try to achieve, we really have no clue on how you want to parse or write your xml?
(XML is really already becoming a legacy format, but fpc contains the correct support for both options)
Try to describe what you want to achieve.
We want example code of course, working or not,  but if your description is right, most of us can point you in the right direction.
Title: Re: using xml
Post by: rcmz on March 03, 2025, 07:07:41 pm
hi,

here is my .pas file

tia
ramiro
Title: Re: using xml
Post by: BlueIcaro on March 03, 2025, 07:34:55 pm
Hello, I tested in Lazarus 4.0RC, and I have not problem
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
var
  XmlDoc: TXMLDocument;
  Root: TDOMElement;
begin
  XmlDoc := TXMLDocument.Create;
  Root := XmlDoc.CreateElement('catalogocuentas:Catalogo');
  Root.AttribStrings['aa']:='ggg';     

Which Lazarus did use? and Operating System?
/BlueIcaro
Title: Re: using xml
Post by: rcmz on March 03, 2025, 08:38:42 pm
Hi,

Im using Lazarus 3.0

TIA
Ramiro
Title: Re: using xml
Post by: rcmz on March 03, 2025, 09:07:43 pm
hi, again

I just installed version 3.8

Im still having the problem with

Root.AttribStrings  "error: identifier idents no member AttribStrings"

how can i correct this error.

TIA
Ramiro
Title: Re: using xml
Post by: VisualLab on March 03, 2025, 09:54:46 pm
Hmm... The documentation says that "AttribStrings" does exist as an array property on the TDOMElement class. This documentation:

TDOMElement (https://dsiders.gitlab.io/lazdocsnext/lazutils/laz2_dom/tdomelement.html)
TDOMElement.AttribStrings (https://dsiders.gitlab.io/lazdocsnext/lazutils/laz2_dom/tdomelement.attribstrings.html)

The simple example provided by BlueIcaro compiles for me (Windows 10 64-bit, Lazarus 3.4). Maybe you declared something incorrectly?
Title: Re: using xml
Post by: TRon on March 03, 2025, 10:32:12 pm
In case rcmz is using his last posted example without any modification then that is no wonder. The rootnode in that code is a TDomNode (which does not have attribstrings property).

BTW the attribstrings property is default so
Code: Pascal  [Select][+][-]
  1. MyElement['name'] := 'value';
  2.  
Should suffice as well.

PS: @rcmz even works as expected on Lazarus 3.4 (for me the quickest older version I was able to grab) so you should have no issues. Tried and tested with dsider's example.
Code: XML  [Select][+][-]
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <catalogocuentas:Catalogo xmlns:catalogocuentas="http://www.sat.gob.mx/esquemas/ContabilidadE/1_3/CatalogoCuentas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/esquemas/ContabilidadE/1_3/CatalogoCuentas http://www.sat.gob.mx/esquemas/ContabilidadE/1_3/CatalogoCuentas/CatalogoCuentas_1_3.xsd" Version="1.3" RFC="ROAP640729JZ5" Mes="02" Anio="2025">
  3.   <catalogocuentas:Ctas CodAgrup="101.01" NumCta="100-000-000" Desc="FONDO FIJO DE CAJA" Nivel="1" Natur="D"/>
  4.   <catalogocuentas:Ctas CodAgrup="102.01" NumCta="100-000-000" Desc="BANCOMER SA DE CV" Nivel="1" Natur="D"/>
  5. </catalogocuentas:Catalogo>
  6.  

fwiw: the XML dom from FPC indeed seem to put/retrieve/store the attributes in another order.
Title: Re: using xml
Post by: rcmz on March 03, 2025, 11:18:18 pm
hi,

sorry I just change from TDOMNode to TDOMElement

the file is created as required.

Thx to all
Ramiro
TinyPortal © 2005-2018