Recent

Author Topic: Add a node in a XML file  (Read 1494 times)

folkeu08

  • Full Member
  • ***
  • Posts: 117
Add a node in a XML file
« on: April 10, 2022, 02:07:28 pm »
Hi

I create an xml file on the "OnCreate" event of a form :
Code: Pascal  [Select][+][-]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TPC100>
  3.   <RS232>
  4.     <Baud>0</Baud>
  5.     <Parity>0</Parity>
  6.     <Bits>0</Bits>
  7.     <Stream>0</Stream>
  8.   </RS232>
  9.   <LowBand>
  10.     <Dens>50</Dens>
  11.     <Gate>1</Gate>
  12.     <AGC>32</AGC>
  13.     <QPoint>80</QPoint>
  14.   </LowBand>
  15. </TPC100>
  16.  

I am looking to add a "default" node between TPC100 and LowBand to obtain this type of XML file :
Code: Pascal  [Select][+][-]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TPC100>
  3.   <RS232>
  4.     <Baud>0</Baud>
  5.     <Parity>0</Parity>
  6.     <Bits>0</Bits>
  7.     <Stream>0</Stream>
  8.   </RS232>
  9.   <Default>
  10.    <LowBand>
  11.     <Dens>50</Dens>
  12.     <Gate>1</Gate>
  13.     <AGC>32</AGC>
  14.     <QPoint>80</QPoint>
  15.    </LowBand>
  16.   </Default>
  17. </TPC100>
  18.  

So is it possible to add a level?
I add my source which works for my first type of XML file.
Thanks


AlexTP

  • Hero Member
  • *****
  • Posts: 2680
    • UVviewsoft
Re: Add a node in a XML file
« Reply #1 on: April 10, 2022, 05:08:48 pm »

dseligo

  • Hero Member
  • *****
  • Posts: 1674
Re: Add a node in a XML file
« Reply #2 on: April 10, 2022, 06:00:30 pm »
You could do it in different way (maybe easier), but here it is minimal change to your code that do what you want.
This is from 110 of Main_unit file:
Code: Pascal  [Select][+][-]
  1.     //********************************************************************************************************
  2.     //********                   Config Low Band                                                  ************
  3.     //********************************************************************************************************
  4.     // Create a parent node
  5.     RootNode:= Doc.DocumentElement;
  6.     parentNode := Doc.CreateElement('default');  // **** new line
  7.     RootNode.Appendchild(parentNode);                          // save parent node **** moved line
  8.     parentNode := Doc.CreateElement('LowBand');
  9.     RootNode.ChildNodes.Item[1].AppendChild(parentNode);       // insert child node in respective parent node **** new line
  10.  
  11.     // Create a child node
  12.     parentNode := Doc.CreateElement('Dens');                // create a child node
  13.     //nofilho := Doc.CreateTextNode('S:\Database\cellule_archeologique.db');         // insert a value to node
  14.     nofilho := Doc.CreateTextNode('50');
  15.     parentNode.Appendchild(nofilho);                         // save node
  16.     RootNode.ChildNodes.Item[1].ChildNodes.Item[0].AppendChild(parentNode);       // insert child node in respective parent node **** changed
  17.  
  18.     parentNode := Doc.CreateElement('Gate');                // create a child node
  19.     nofilho := Doc.CreateTextNode('1');         // insert a value to node
  20.     parentNode.Appendchild(nofilho);                         // save node
  21.     RootNode.ChildNodes.Item[1].ChildNodes.Item[0].AppendChild(parentNode);       // insert child node in respective parent node **** changed
  22.  
  23.     parentNode := Doc.CreateElement('AGC');                // create a child node
  24.     nofilho := Doc.CreateTextNode('32');         // insert a value to node
  25.     parentNode.Appendchild(nofilho);                         // save node
  26.     RootNode.ChildNodes.Item[1].ChildNodes.Item[0].AppendChild(parentNode);       // insert child node in respective parent node **** changed
  27.  
  28.     parentNode := Doc.CreateElement('QPoint');                // create a child node
  29.     nofilho := Doc.CreateTextNode('80');         // insert a value to node
  30.     parentNode.Appendchild(nofilho);                         // save node
  31.     RootNode.ChildNodes.Item[1].ChildNodes.Item[0].AppendChild(parentNode);       // insert child node in respective parent node **** changed

I marked with **** new and changed lines.

folkeu08

  • Full Member
  • ***
  • Posts: 117
Re: Add a node in a XML file
« Reply #3 on: April 10, 2022, 06:23:01 pm »
Hi
Thanks for your help, I test your solution after.

I find that now :

Code: Pascal  [Select][+][-]
  1. procedure TCreateXML_Form.InitialiseXML;
  2.  //Varialbles pour la création du fichier config.XML
  3.   var
  4.   Doc: TXMLDocument;                         // variable to document
  5.   Racine, RootNode, Default, Funky, Jazz,  LowBand, MidBand, nofilho: TDOMNode;   // variable to nodes
  6.   begin
  7.   try
  8.     // Create a document
  9.     Doc := TXMLDocument.Create;
  10.  
  11.     // Create a root node
  12.     Racine := Doc.CreateElement('TPC100');
  13.     Doc.Appendchild(Racine);                           // save root node
  14.  
  15.  
  16.  
  17.     //********************************************************************************************************
  18.     //********                   Config Low Band                                                  ************
  19.     //********************************************************************************************************
  20.     // Create a parent node
  21.     Default := Doc.DocumentElement;
  22.     Default := Doc.CreateElement('Default');
  23.     Racine.Appendchild(Default);                          // save parent node
  24.  
  25.     // Create a Child node
  26.     LowBand := Doc.DocumentElement;
  27.     LowBand := Doc.CreateElement('LowBand');
  28.     Default.Appendchild(LowBand);
  29.  
  30.     // Create a Sub Subchild node
  31.     LowBand := Doc.CreateElement('Dens');                // create a child node
  32.     nofilho := Doc.CreateTextNode('50');
  33.     LowBand.Appendchild(nofilho);                         // save node
  34.     Default.ChildNodes.Item[0].AppendChild(LowBand);       // insert child node in respective parent node
  35.     // Create a Sub Subchild node
  36.     LowBand := Doc.CreateElement('Gate');                // create a child node
  37.     nofilho := Doc.CreateTextNode('1');         // insert a value to node
  38.     LowBand.Appendchild(nofilho);                         // save node
  39.     Default.ChildNodes.Item[0].AppendChild(LowBand);       // insert child node in respective parent node
  40.  
  41.     LowBand := Doc.CreateElement('AGC');                // create a child node
  42.     nofilho := Doc.CreateTextNode('32');         // insert a value to node
  43.     LowBand.Appendchild(nofilho);                         // save node
  44.     Default.ChildNodes.Item[0].AppendChild(LowBand);       // insert child node in respective parent node
  45.  
  46.     LowBand := Doc.CreateElement('QPoint');                // create a child node
  47.     nofilho := Doc.CreateTextNode('80');         // insert a value to node
  48.     LowBand.Appendchild(nofilho);                         // save node
  49.     Default.ChildNodes.Item[0].AppendChild(LowBand);       // insert child node in respective parent node
  50.  
  51.  
  52.     // Create a Child node
  53.     MidBand := Doc.DocumentElement;
  54.     MidBand := Doc.CreateElement('MidBand');
  55.     Default.Appendchild(MidBand);
  56.  
  57.     // Create a Sub Subchild node
  58.     MidBand := Doc.CreateElement('Dens');                // create a child node
  59.     nofilho := Doc.CreateTextNode('50');
  60.     MidBand.Appendchild(nofilho);                         // save node
  61.     Default.ChildNodes.Item[1].AppendChild(MidBand);       // insert child node in respective parent node
  62.     // Create a Sub Subchild node
  63.     MidBand := Doc.CreateElement('Gate');                // create a child node
  64.     nofilho := Doc.CreateTextNode('1');         // insert a value to node
  65.     MidBand.Appendchild(nofilho);                         // save node
  66.     Default.ChildNodes.Item[1].AppendChild(MidBand);       // insert child node in respective parent node
  67.  
  68.     MidBand := Doc.CreateElement('AGC');                // create a child node
  69.     nofilho := Doc.CreateTextNode('32');         // insert a value to node
  70.     MidBand.Appendchild(nofilho);                         // save node
  71.     Default.ChildNodes.Item[1].AppendChild(MidBand);       // insert child node in respective parent node
  72.  
  73.     MidBand := Doc.CreateElement('QPoint');                // create a child node
  74.     nofilho := Doc.CreateTextNode('80');         // insert a value to node
  75.     MidBand.Appendchild(nofilho);                         // save node
  76.     Default.ChildNodes.Item[1].AppendChild(MidBand);       // insert child node in respective parent node
  77.  
  78.  
  79.     writeXMLFile(Doc, 'config.xml');                     // write to XML
  80.   finally
  81.     Doc.Free;                                          // free memory
  82.   end;
  83. end;              
  84.  

It create what i want too.
Thanks
François

folkeu08

  • Full Member
  • ***
  • Posts: 117
Re: Add a node in a XML file
« Reply #4 on: April 11, 2022, 11:00:40 am »
Hi,
I manage to create properly from the "dseligo" example of my XML file with the 4 presets (default, rock, jazz, funky) for the values ​​of each band (low, mid, pres and hight).

The next step is to read the values ​​of the 4 bands for the default preset.

I was able to do this with this code without the default preset :
Code: Pascal  [Select][+][-]
  1. procedure TMain_Form.ReadBaseDonneesXML;
  2. //https://wiki.freepascal.org/XML_Tutorial#Create_a_TXMLDocument_from_a_string
  3. var
  4.  BaseNode: 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 "LowBand" node
  13.     BaseNode := Doc.DocumentElement.FindNode('LowBand');
  14.     LowDens_Track.Value := StrToFloat(BaseNode.FindNode('Dens').TextContent);
  15.     LowGate_Track.Value := StrToFloat(BaseNode.FindNode('Gate').TextContent);
  16.     LowAGC_Track.Value := StrToFloat(BaseNode.FindNode('AGC').TextContent);
  17.     LowQPoint_Track.Value := StrToFloat(BaseNode.FindNode('QPoint').TextContent);
  18.     // Retrieve the "MidBand" node
  19.     BaseNode := Doc.DocumentElement.FindNode('MidBand');
  20.     MidDens_Track.Value := StrToFloat(BaseNode.FindNode('Dens').TextContent);
  21.     MidGate_Track.Value := StrToFloat(BaseNode.FindNode('Gate').TextContent);
  22.     MidAGC_Track.Value := StrToFloat(BaseNode.FindNode('AGC').TextContent);
  23.     MidQPoint_Track.Value := StrToFloat(BaseNode.FindNode('QPoint').TextContent);
  24.     // Retrieve the "PresBand" node
  25.     BaseNode := Doc.DocumentElement.FindNode('PresBand');
  26.     PresDens_Track.Value := StrToFloat(BaseNode.FindNode('Dens').TextContent);
  27.     PresGate_Track.Value := StrToFloat(BaseNode.FindNode('Gate').TextContent);
  28.     PresAGC_Track.Value := StrToFloat(BaseNode.FindNode('AGC').TextContent);
  29.     PresQPoint_Track.Value := StrToFloat(BaseNode.FindNode('QPoint').TextContent);
  30.     // Retrieve the "HighBand" node
  31.     BaseNode := Doc.DocumentElement.FindNode('HighBand');
  32.     HighDens_Track.Value := StrToFloat(BaseNode.FindNode('Dens').TextContent);
  33.     HighGate_Track.Value := StrToFloat(BaseNode.FindNode('Gate').TextContent);
  34.     HighAGC_Track.Value := StrToFloat(BaseNode.FindNode('AGC').TextContent);
  35.     HighQPoint_Track.Value := StrToFloat(BaseNode.FindNode('QPoint').TextContent);
  36.   finally
  37.     // finally, free the document
  38.     Doc.Free;
  39.     //ReadLn;
  40.   end;
  41. end;                                                                                    
  42.  

I try to introduce the item as in the creation but it does not read anything in fact.
I'm lost again!
Thanks for your new help.
I added as an attachment, the file that is created

François

 

TinyPortal © 2005-2018