Recent

Author Topic: [Solved] Read a child and remplace it in a XML file  (Read 920 times)

folkeu08

  • Full Member
  • ***
  • Posts: 106
[Solved] Read a child and remplace it in a XML file
« on: April 05, 2020, 01:54:42 am »
Hi,
At the first start of the software, I detert the language of the environment and save it in 2 childs (OS and SOFT) to obtain this XML file below:
Code: Pascal  [Select][+][-]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TX-FM1>
  3.   <Language>
  4.     <OS>fr</OS>
  5.     <SOFT>fr</SOFT>
  6.   </Language>
  7. </TX-FM1>
  8.  
At the second start of the software, I want to read the value of SOFT to assign it to a variable 'lang' which corresponds to the translation of the character strings.
Do I have to award an item to "Language"?
I can't do it despite my research of examples on the net.
Thank for your Help.
François
« Last Edit: April 09, 2020, 03:34:47 pm by folkeu08 »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Read a child in a XML file
« Reply #1 on: April 05, 2020, 10:11:06 am »
Something like this:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   Classes,DOM,XMLRead;
  5.  
  6. const
  7.   XMLString = '<?xml version="1.0" encoding="utf-8"?>'
  8.             + '<tx-fm1>'
  9.             + '  <language>'
  10.             + '    <os>fr</os>'
  11.             + '    <soft>fr</soft>'
  12.             + '  </language>'
  13.             + '</tx-fm1>'
  14.             ;
  15.  
  16. var
  17.   XMLStringStream: TStream;
  18.   LanguageNode: TDOMNode;
  19.   Doc: TXMLDocument;
  20. begin
  21.   try
  22.     XMLStringStream := TStringStream.Create(XMLString);
  23.     ReadXMLFile(Doc, XMLStringStream);
  24.     LanguageNode := Doc.DocumentElement.FindNode('language');
  25.     WriteLn(LanguageNode.FindNode('os').TextContent);
  26.     WriteLn(LanguageNode.FindNode('soft').TextContent);
  27.   finally
  28.     Doc.Free;
  29.     XMLStringStream.Free;
  30.   end;
  31. end.
  32.  
?

folkeu08

  • Full Member
  • ***
  • Posts: 106
Re: Read a child in a XML file
« Reply #2 on: April 05, 2020, 01:06:38 pm »
Hi Leledumbo,

That's right and I did it a little differently but it works too, here is my solution :
Code: Pascal  [Select][+][-]
  1. procedure TMainform.ReadLanguageXML;
  2. //https://wiki.freepascal.org/XML_Tutorial#Create_a_TXMLDocument_from_a_string
  3. var
  4.  PassNode: TDOMNode;
  5.  Doc: TXMLDocument;
  6.  
  7.   begin
  8.   try
  9.     // Read in xml file from disk
  10.     ReadXMLFile(Doc, 'config.xml');
  11.  
  12.     // Retrieve the "Language" node
  13.     PassNode := Doc.DocumentElement.FindNode('Language');
  14.     lang := PassNode.LastChild.TextContent;
  15.   finally
  16.     // finally, free the document
  17.     Doc.Free;
  18.     //ReadLn;
  19.   end;
  20. end;            
  21.  
The next step for this afternoon is to replace the value if the language choice is changed.
I have confidence, I will find too.
François

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Read a child in a XML file
« Reply #3 on: April 05, 2020, 01:37:46 pm »
That's right and I did it a little differently but it works too, here is my solution :
Code: Pascal  [Select][+][-]
  1.   lang := PassNode.LastChild.TextContent;
And what if the provider of this file decides to write the child nodes of 'Language' in a different order or to include a new node after the currently last one?
« Last Edit: April 05, 2020, 01:57:38 pm by wp »

folkeu08

  • Full Member
  • ***
  • Posts: 106
Re: Read a child and remplace it in a XML file
« Reply #4 on: April 05, 2020, 02:44:31 pm »
Hi wp,
you are right but the provider of this file its'me, In my source I have modified to :
Code: Pascal  [Select][+][-]
  1. lang := PassNode.FindNode('soft').TextContent);
  2.  
And now a search to remplace this value by another when the choice of the langage is modified.
Thanks
François

folkeu08

  • Full Member
  • ***
  • Posts: 106
Re: Read a child and remplace it in a XML file
« Reply #5 on: April 05, 2020, 04:09:28 pm »
Hi,
Yes, I found it and it was too simple. I made life difficult for nothing:
Code: Pascal  [Select][+][-]
  1. procedure TMainform.MofdifieLanguageXML;
  2.  var
  3.    XMLStringStream: TStream;
  4.    LanguageNode: TDOMNode;
  5.    Doc: TXMLDocument;
  6.   Begin
  7.     try
  8.     //XMLStringStream := TStringStream.Create(Config.xml);
  9.     ReadXMLFile(Doc, 'config.xml');
  10.     LanguageNode := Doc.DocumentElement.FindNode('Language');
  11.     //LanguageNode.FindNode('SOFT').TextContent;
  12.     LanguageNode.FindNode('SOFT').TextContent := Lang;
  13.     writeXMLFile(Doc, 'config.xml');;
  14.   finally
  15.     Doc.Free;                                          // free memory
  16.   end;
  17. end;          
I close the post
I make a soft to drive by a USB Port a FM transmitter.
Usually, I use Delphi but I want to publish my source.
Thanks for all.
François

 

TinyPortal © 2005-2018